I am learning how to make a game for a school. In this game, I have three different types of ships: destroyer, carrier, and a speed boat. I will declare an entityType enum:
enum EntityType {DESTROYER, CARRIER, SPEEDBOAT};
I have to have an Entity class that looks like:
class Enitity
{
private:
float acceleration, turnRate, maxSpeed, minSpeed;
std::string entityType;
public:
Entity(EntityType type);
~Entity();
//Rest of class
};
For each ship type, they have different values. I have different values for each type of ship and I am trying to figure the best way to initialize each type of ship. I have thought of three ways of doing this. I am also trying to make it so that I can build on this in the future as I get more ship types.
Option 1: Create a file that holds a EntityData struct and creates data for each ship type like:
struct EntityData
{
float acceleration, turnRate, maxSpeed, minSpeed;
std::string entityType;
};
EntityData DestroyerData;
DestroyerData.acceleration = 5;
DestroyerData.turnRate = 2;
DestroyerData.maxSpeed = 35;
DestroyerData.minSpeed = 0;
DestroyerData.entityType = "Destroyer";
EntityData CarrierData;
CarrierData.acceleration =2;
.....
Then within the Entity class's constructor have a switch statement that if the entityType is DESTROYER set the class's data to the struct's data.
Entity::Entity(EntityType type)
{
EntityData data;
switch (type)
{
case EntityType::DESTROYER:
data = DestroyerData;
break;
case EntityType::CARRIER:
break;
default:
break;
}
acceleration = data.acceleration;
maxSpeed = data.maxSpeed;
//Rest of variables
}
Option 2: Use inheritance and make the Entity class the base class and have each ship type have it's own class. In it's class just have those values initialized in the constructor.
class Destroyer : public Entity
{
public:
Entity(EntityType type);
~Entity();
//Rest of class
};
Destroyer::Destroyer()
: acceleration(5),
maxSpeed(35),
//Rest of variables
{
}
Option 3: Have all the information set within the Entity class based off of the Entity Type by using a switch statement in Entity's constructor and not use the data structs or inherited classes?
Entity::Entity(EntityType type)
{
switch (type)
{
case EntityType::DESTROYER:
maxSpeed = 42;
//TODO: Set the rest of the variables for the DESTROYER
break;
case EntityType::CARRIER:
//TODO: Set the rest of the variables for the CARRIER
maxSpeed = 55;
break;
default:
break;
}
}
Which method is best and why?
If down voting - Please explain why you are down voting so I can improve my question.