游戏状态机-有限模型1

梦想游戏人
目录:
游戏开发

该模型也是最简单的状态机模型

通过代理命令,解决互斥的状态问题,如果某一状态需要捕获上一个状态,那么这么模型就要稍加修改,

一直很机智的做法是利用状态stack 来记录

class ObjectState
{
public:
	virtual void update(void*host) = 0;
	virtual void onEnter(void*);
	virtual void onExit(void*);
	std::string getStateName(){ return _name; }
protected:
	std::string _name;
};



class Hero
{
public:

	void input(int c);

	void update();

private:
	ObjectState*_state;

};



class HeroState :public ObjectState
{
public:
	virtual void update(void*host)final
	{
		this->update(static_cast<Hero*>(host));
	}
	virtual void update(Hero*host) = 0;
};


class JumpState:public HeroState
{
public:
	virtual void update(Hero*host)override
	{
		cout << __FUNCTION__ << endl;
	}

};



class IdleState :public HeroState
{
public:
	virtual void update(Hero*host)override
	{
		cout << __FUNCTION__ << endl;
	}

};





void Hero::input(int c)
{


	if (c == 'W')	_state = new IdleState;
	if (c == 'S')	_state = new JumpState;


}

void Hero:: update()
{
	this->_state->update(this);
}

int main(int argc, char *argv[])
{

	Hero player;
	

	while (true)
	{
	//	Sleep(100);
		char c= getch();

		player.input(c);
		player.update();

	}


	system("pause");
	return 0;
}
Scroll Up