前端之家收集整理的这篇文章主要介绍了
Cocos2dx_有限状态机_2,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#ifndef __STATE_H__
#define __STATE_H__
class Monkey;
class State
{
public:
virtual void execute(Monkey *mk) = 0;
};
#endif
#ifndef __STOPSTATE_H__
#define __STOPSTATE_H__
#include "State.h"
#include "Monkey.h"
#include "TrunState.h"
class StopState : public State
{
public:
virtual void execute(Monkey *mk);
};
#endif
#include "StopState.h"
void StopState::execute(Monkey *mk)
{
if (mk->isStopTimeOut())
{
mk->changeState(new TrunState());
mk->walk();
}
}
#ifndef __TRUNSTATE_H__
#define __TRUNSTATE_H__
#include "State.h"
#include "Monkey.h"
#include "WalkState.h"
class TrunState : public State
{
virtual void execute(Monkey *mk);
};
#endif
#include "TrunState.h"
void TrunState::execute(Monkey *mk)
{
mk->changeState(new WalkState());
mk->walk();
}
#ifndef __WALKSTATE_H__
#define __WALKSTATE_H__
#include "State.h"
#include "Monkey.h"
#include "StopState.h"
#include "TrunState.h"
class WalkState : public State
{
virtual void execute(Monkey *mk);
};
#endif
#include "WalkState.h"
void WalkState::execute(Monkey *mk)
{
mk->walk();
if (mk->isWalkOutBorder())
{
mk->changeState(new TrunState());
mk->trun();
}
else if (mk->isWalkTimeOut())
{
mk->changeState(new StopState());
mk->stop();
}
}
#ifndef __MONKEY_H__
#define __MONKEY_H__
#include "cocos2d.h"
#include <time.h>
#include "State.h"
#include "StopState.h"
#include "WalkState.h"
#include "TrunState.h"
using namespace std;
using namespace cocos2d;
#define MAX_STOP_TIME 10
#define MAX_WALK_TIME 20
#define MAX_WALK_DIST 100
enum MonkeyState
{
stStop,stWalk,stTrun,};
class Monkey : public Node
{
public:
Monkey();
CREATE_FUNC(Monkey);
//override
virtual bool init();
virtual void update(float delta);
void changeState(State *newState);
void stop();
void walk();
void trun();
bool isStopTimeOut();
bool isWalkTimeOut();
bool isWalkOutBorder();
private:
Sprite* sp;
State *_curState;
time_t _curTime;
int _curPos;
int _curStep;
};
#endif
#include "Monkey.h"
Monkey::Monkey()
{
}
bool Monkey::init()
{
if (!Node::init())
{
return false;
}
this->_curPos = 0;
this->_curStep = 1;
this->_curState = NULL;
this->changeState(new WalkState());
this->scheduleUpdate();
this->sp = Sprite::create("eraser.png");
sp:setPosition(Vec2(0,0));
this->addChild(this->sp);
return true;
}
void Monkey::update(float delta)
{
if (this->_curState)
{
this->_curState->execute(this);
}
}
void Monkey::changeState(State *newState)
{
State *oldState = this->_curState;
this->_curState = newState;
if (oldState)
{
delete oldState;
}
this->_curTime = time(0);
}
void Monkey::stop()
{
log("stop()");
}
void Monkey::walk()
{
this->_curPos += this->_curStep;
this->sp->setPosition(Vec2(this->_curPos*0.5,0));
log("walk(): pos=%d",this->_curPos);
}
void Monkey::trun()
{
this->_curStep *= -1;
log("turn(): step=%d",this->_curStep);
}
bool Monkey::isStopTimeOut()
{
return (time(0) - this->_curTime > MAX_STOP_TIME);
}
bool Monkey::isWalkTimeOut()
{
return (time(0) - this->_curTime > MAX_WALK_TIME);
}
bool Monkey::isWalkOutBorder()
{
return (this->_curPos > MAX_WALK_DIST || this->_curPos < -MAX_WALK_DIST);
}
原文链接:https://www.f2er.com/cocos2dx/341484.html