cocos:有限状态机(消息驱动)

cocos:有限状态机(消息驱动),第1张

概述State.h #pragma once#ifndef __STATE__H_#define __STATE__H_#include "MessageEnum.h"class GameObj;class State{public: virtual void execuete(GameObj*gameObj, MessageEnum messageEnum)=0;};#endi

State.h

#pragma once#ifndef __STATE__H_#define __STATE__H_#include "MessageEnum.h"class GameObj;class State{public:	virtual voID execuete(GameObj*gameObj,MessageEnum messageEnum)=0;};#endif

RestState.h
#pragma once#ifndef __REST_STATE_H_#define __REST_STATE_H_#include "State.h"class RestState :public State{public:	virtual voID execuete(GameObj*gameObj,MessageEnum messageEnum);};#endif

CodingState.h
#pragma once#include "MessageEnum.h"#include "State.h"class GameObj;class CodingState:public State{public :	virtual voID execuete(GameObj*gameObj,MessageEnum messageEnum);};


GameObj.h
#pragma once#ifndef __GAME_OBJ__H#define __GAME_OBJ__H#include "cocos2d.h"USING_NS_CC;class FiniteStateMachine;class GameObj :public Node{public:	CREATE_FUNC(GameObj);	virtual bool init();	voID rest();	voID Coding();	virtual voID update(float dt);	FiniteStateMachine*getFsm();private:	FiniteStateMachine*mFsm;};#endif


FiniteStateMachine.h
#pragma once#ifndef __FINITY_STATE_MACHINE_H#define __FINITY_STATE_MACHINE_H#include "cocos2d.h"#include "State.h"USING_NS_CC;class GameObj;class FiniteStateMachine :public Node{public:	~FiniteStateMachine();	static FiniteStateMachine*createWithGameObj(GameObj*gameObj);	bool initWithGameObj(GameObj*gameObj);	voID changeState(State *state);private:	State*mState;	GameObj*mGameObj;	voID onRecvWantoRest(Ref*obj);	voID onRecvWantoCoding(Ref*obj);};#endif

MessageEnum.h
#pragma once#ifndef __MESSAGE_ENUM__H#define __MESSAGE_ENUM__Henum MessageEnum{	EN_WantToCoding,EN_WantToRest,};#endif

RestState.cpp
#include "RestState.h"#include "FiniteStateMachine.h"#include "CodingState.h"#include "GameObj.h"voID RestState::execuete(GameObj*gameObj,MessageEnum messageEnum) {	switch (messageEnum)	{	case EN_WantToCoding:		gameObj->Coding();		gameObj->getFsm()->changeState(new CodingState());		break;	case EN_WantToRest:		break;	default:		break;	}}
CodingState.cpp
#include "CodingState.h"#include "GameObj.h"#include "RestState.h"#include "FiniteStateMachine.h"#include "MessageEnum.h"voID CodingState::execuete(GameObj*gameObj,MessageEnum messageEnum) {	switch (messageEnum)	{	case EN_WantToCoding:		break;	case EN_WantToRest:		gameObj->rest();		gameObj->getFsm()->changeState(new RestState());		break;	default:		break;	}}

GameObj.cpp
#include "GameObj.h"#include "FiniteStateMachine.h"bool GameObj::init() {	mFsm = FiniteStateMachine::createWithGameObj(this);	return true;}voID GameObj::rest() {	log("rest");	MessageBox("rest","msg");}voID GameObj::Coding() {	log("Coding");	MessageBox("Coding","msg");}voID GameObj::update(float dt) {}FiniteStateMachine* GameObj::getFsm() {	return mFsm;}

FiniteStateMachine.cpp
#include "FiniteStateMachine.h"#include "GameObj.h"#include "MessageEnum.h"#define NOTIFY NotificationCenter::getInstance()FiniteStateMachine:: ~FiniteStateMachine() {	NOTIFY->removeAllObservers(this);}FiniteStateMachine*FiniteStateMachine::createWithGameObj(GameObj*gameObj) {		FiniteStateMachine *fsm = new FiniteStateMachine();	if (fsm&&fsm->initWithGameObj(gameObj)) {		fsm->autorelease();	}	else {		CC_SAFE_DELETE(fsm);		fsm = NulL;	}	return fsm;}bool FiniteStateMachine::initWithGameObj(GameObj*gameObj) {	mState = NulL;	mGameObj = gameObj;		NOTIFY->addobserver(		this,callfuncO_selector(FiniteStateMachine::onRecvWantoCoding),StringUtils::toString(EN_WantToCoding),NulL);	NOTIFY->addobserver(		this,callfuncO_selector(FiniteStateMachine::onRecvWantoRest),StringUtils::toString(EN_WantToRest),NulL);	return true;}voID FiniteStateMachine::changeState(State *state) {	CC_SAFE_DELETE(mState);	mState = state;}voID FiniteStateMachine::onRecvWantoRest(Ref*obj) {	if (mState == NulL||mGameObj==NulL) {		return;	}	mState->execuete(mGameObj,EN_WantToRest);}voID FiniteStateMachine::onRecvWantoCoding(Ref*obj) {	if (mState == NulL||mGameObj==NulL) {		return;	}	mState->execuete(mGameObj,EN_WantToCoding);}


使用:

        auto player = GameObj::create();	player->getFsm()->changeState(new RestState());	NotificationCenter::getInstance()->postNotification(StringUtils::toString(EN_WantToCoding),NulL);	addChild(player);
总结

以上是内存溢出为你收集整理的cocos:有限状态机(消息驱动)全部内容,希望文章能够帮你解决cocos:有限状态机(消息驱动)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1078844.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存