0
votes

Suppose I have a class Entity.
And then I have n number of classes which is derived from Entity
eg :

class Snake : public Entity{...};  
class Mouse : public Entity{...};

Now I have a class player that is an entity.
Can I create a class player that inherits from any type of entity? eg :

class Player : public Entity -->(but instead of entity be any type of entity)  

Can this be done?
Is this achieved by using templates?
I've read that the templates can be explicitly specified in a cpp file i.e

template class Entity<Snake>;

I am trying to achieve the following

In my player class I have a moveCamera function inside move Now only when a player moves, the camera moves .. If an AI Snake moves the camera should not move.

this is my render function in the entity class which is virtual

void Entity::Render(float interpolation)
{
  if(currentAnimation != 0){
    float x = this->currLocation.x - (this->currentVelocity.x * (1.0f - interpolation)) - camera->getLocation(L_FACTOR_CURRENT).x;
    float y = this->currLocation.y - (this->currentVelocity.y * (1.0f - interpolation)) - camera->getLocation(L_FACTOR_CURRENT).y;
    currentAnimation->Render(x,y);
  }
}

This is my gameUpdate function and basically moves an entity to its respective world co-ordinates

void Entity::GameUpdate(float gameUpdateDelta)
{
  this->Move();
}

Therefore for my player's move function I will call the camera's move function and then call the base class's move function... Now is it possible to call the extended class of the base's class move function..
My Move function is virtual and therefore a snake and mouse can move differently..

2
Why would you do this? Seems like you need composition, not inheritance? Take a look at Decorator pattern - may help - Maxim Krizhanovsky
Make Player owns an Entity (Snake/Mouse). In the function that handles player input, you will call function in Player (e.g. move), which will take care of camera movement and call the movement function in Entity. For AI, just call the movement function directly. In general, abstract away anything that is specific to human player (or AI implementation), but leaving the similar interfaces (and implementation) in Entity. Implement those specific thing in the Player class (or AI class). - nhahtdh
how would i do the same using decorator patter? - Jeffrey Chen
It is possible using templates, but in that case what you're looking for is a concept, which is not a part of C++. Instead, Entity becomes an ad-hoc set of member functions that you call inside the template - it isn't codified in the language. But composition is a better solution in the majority of cases. - Robert Mason
what are the cons of this implementation to what I am trying ? - Jeffrey Chen

2 Answers

1
votes

You may want to write a template class Player that inherit from Template parameter.

template< typename Derived >
class Player: 
    public Derived, // we believe that Derived inherits Entity
    public IPlayer  // makes sense if Player is not a Entity only interface
{
   ... some declaration here ...

   void update(); // some virtual method from Entity interaface
   void player_action(); // some virtual method from IPlayer interaface

}

When create a concrete type of player, you may place it one your scene.

IPlayer* player1 = new  Player<Snake>("Player1");

Entity* playerEntity = dynamic_cast< Entity* >( player1 );
if( playerEntity ) // test if object can be placed on scene
{
    scene->add( playerEntity );
}

You may also need to know how to write partial template specialization of class methods. Also you may find boost enable_if as a powerful toy.

0
votes

It would be more helpful for people to help if you can publish the interface (just the class definition) of your current design. It looks like you need to make player own snake and mouse. Use observers if you want some action to be associated with other actions.