PlayerState --- State模式

PlayerState --- State模式
嗯.. SVN上上传PlayerState.cbp
该工程实现 State 设计模式

具体如下...

 1  // forward decl
 2  class  PlayerState;
 3 
 4  class  Player
 5  {
 6  public :
 7      Player(PlayerState *  initState);
 8 
 9       void  walk();
10       void  stand();
11       void  jump();
12 
13       void  setState(PlayerState *  state);
14  protected :
15      PlayerState *  mState;
16 
17       // decl uncopyable
18      Player( const  Player &  player);
19      Player &   operator = ( const  Player &  rhs);
20  };
21 

玩家的行为转由状态对象处理

// forward decl
class  Player;

class  PlayerState
{
public :
    
virtual   void  walk(Player *  player)  =   0 ;
    
virtual   void  stand(Player *  player)  =   0 ;
    
virtual   void  jump(Player *  player)  =   0 ;
};

class  Walking :  public  PlayerState
{
public :
    
void  walk(Player *  player);
    
void  stand(Player *  player);
    
void  jump(Player *  player);

    
static  Walking *  getInstancePtr();
protected :
    
// singleton
    Walking();

    
static  Walking *  mInstance;
};

class  Standing :  public  PlayerState
{
public :
    
void  walk(Player *  player);
    
void  stand(Player *  player);
    
void  jump(Player *  player);

    
static  Standing *  getInstancePtr();
protected :
    Standing();

    
static  Standing *  mInstance;
};

class  Jumping :  public  PlayerState
{
public :
    
void  walk(Player *  player);
    
void  stand(Player *  player);
    
void  jump(Player *  player);

    
static  Jumping *  getInstancePtr();
protected :
    Jumping();

    
static  Jumping *  mInstance;
};

相对比较简单... 可以当一个非常无聊的文字MUD来玩..

具体见
http://code.google.com/p/charlib/source/browse/trunk/
下的 PlayerState 文件夹

你可能感兴趣的:(PlayerState --- State模式)