#pragma once #include "cocos2d.h" USING_NS_CC; enum EnumState{ enStateWriteCode, /* 状态:写代码 */ enStateWriteArticle, /* 状态:写教程 */ enStateRest, /* 状态:休息 */ }; class Mutou:public CCNode { public: CREATE_FUNC(Mutou); virtual bool init(); /* 当前状态 */ EnumState enCurState; /* 判断是否写代码写累了 */ bool isTire(); /* 是否想写教程 */ bool isWantToWriteArticle(); /* 写代码 */ void writeCode(); /* 写教程 */ void writeArticle(); /* 休息 */ void rest(); /* 切换状态 */ void changeState(EnumState enState); virtual void update(float dt); };
#include "Mutou.h" bool Mutou::init(){ this->scheduleUpdate(); return true; } void Mutou::changeState( EnumState enState ) { this->enCurState = enState; } bool Mutou::isTire() { /* 每次问木头累不累,他都会说:累~ */ return true; } bool Mutou::isWantToWriteArticle() { /* 有10%的概率想写教程(好懒~!) */ float ran = CCRANDOM_0_1(); if(ran < 0.1f) { return true; } return false; } void Mutou::writeCode() { CCLOG("mutou is wirting Code."); } void Mutou::writeArticle() { CCLOG("mutou is writing article."); } void Mutou::rest() { CCLOG("mutou is resting."); } void Mutou::update( float dt ) { /* 判断在每一种状态下应该做什么事情 */ switch(enCurState) { case enStateWriteCode: /* 如果累了就休息,并且切换到休息状态 */ if(isTire()) { rest(); changeState(enStateRest); } break; case enStateWriteArticle: /* 如果累了就休息,并且切换到休息状态 */ if(isTire()) { rest(); changeState(enStateRest); } break; case enStateRest: /* 一定的概率写代码,一定的概率写教程,并且切换到相应的状态 */ if(isWantToWriteArticle()) { writeArticle(); changeState(enStateWriteArticle); } else { writeCode(); changeState(enStateWriteCode); } break; } }