0

クラスエンティティがあるとします。
そして、エンティティから派生した n 個のクラスがあります

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

これで、エンティティであるクラス プレーヤーができました。
どのタイプのエンティティからも継承するクラス プレーヤーを作成できますか? 例:

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

これはできますか?
これはテンプレートを使用して達成されますか?
テンプレートを cpp ファイルで明示的に指定できることを読みました。

template class Entity<Snake>;

私は次のことを達成しようとしています

私のプレーヤークラスには、move 内に moveCamera 関数があります。これで、プレーヤーが移動したときにのみカメラが移動します..AI スネークが移動した場合、カメラは移動してはなりません。

これは、仮想エンティティ クラスのレンダリング関数です。

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);
  }
}

これは私の gameUpdate 関数であり、基本的にエンティティをそれぞれのワールド座標に移動します

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

したがって、私のプレーヤーの move 関数では、カメラの move 関数を呼び出してから、基本クラスの move 関数を呼び出します...これで、基本クラスの move 関数の拡張クラスを呼び出すことができます..
私の Move 関数は仮想であり、したがってヘビですマウスは別の方法で動かすことができます..

4

2 に答える 2

1

PlayerTemplate パラメーターから継承するテンプレート クラスを作成することができます。

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

}

具体的なタイプのプレーヤーを作成するときは、それをシーンに配置できます。

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 );
}

また、クラス メソッドの部分的なテンプレートの特殊化を記述する方法を知る必要がある場合もあります。また、強力なおもちゃとしてboost enable_ifを見つけることもできます。

于 2012-05-22T02:37:14.780 に答える
0

現在の設計のインターフェイス (クラス定義のみ) を公開できれば、人々の助けになるでしょう。プレイヤー自身のヘビとネズミを作る必要があるようです。アクションを他のアクションに関連付ける場合は、オブザーバーを使用します。

于 2012-05-22T04:21:37.377 に答える