@H_502_0@
@H_502_0@
@H_502_0@
- @H_502_0@#ifndef__MUTOU_H__
- @H_502_0@#define__MUTOU_H__
- @H_502_0@
- @H_502_0@#include"cocos2d.h"
- @H_502_0@USING_NS_CC;
- @H_502_0@
- @H_502_0@enumEnumState{
- @H_502_0@enStateWriteCode,/*状态:写代码*/
- @H_502_0@enStateWriteArticle,0);">/*状态:写教程*/
- @H_502_0@enStateRest,0);">/*状态:休息*/
- @H_502_0@};
- @H_502_0@
- @H_502_0@classMutou:publicCCNode{
- @H_502_0@public:
- @H_502_0@CREATE_FUNC(Mutou);
- @H_502_0@virtualboolinit();
- @H_502_0@
- @H_502_0@/*当前状态*/
- @H_502_0@EnumStateenCurState;
- @H_502_0@
- @H_502_0@/*判断是否写代码写累了*/
- @H_502_0@boolisTire();
- @H_502_0@
- @H_502_0@/*是否想写教程*/
- @H_502_0@boolisWantToWriteArticle();
- @H_502_0@
- @H_502_0@/*写代码*/
- @H_502_0@voidwriteCode();
- @H_502_0@
- @H_502_0@/*写教程*/
- @H_502_0@voidwriteArticle();
- @H_502_0@
- @H_502_0@/*休息*/
- @H_502_0@voidrest();
- @H_502_0@
- @H_502_0@/*切换状态*/
- @H_502_0@voidchangeState(EnumStateenState);
- @H_502_0@
- @H_502_0@virtualvoidupdate(floatdt);
- @H_502_0@};
- @H_502_0@
- @H_502_0@#endif
- @H_502_0@
- @H_502_0@
- @H_502_0@@H_502_0@
@H_502_0@输出:mutouiswirtingCode.
mutouisresting.
mutouiswritingarticle.
mutouiswirtingCode.
@H_502_0@
@H_502_0@有限状态机的运用可以极大的简化程序 让代码更加的清晰易懂 便于阅读 当游戏主角能根据当前状态进行动作自动切换执行的时候可以极大的减少代码量- @H_502_0@boolHelloWorld::init()
- @H_502_0@{
- @H_502_0@boolbRet=false;
- @H_502_0@do
- @H_502_0@{
- @H_502_0@CC_BREAK_IF(!CCLayer::init());
- @H_502_0@
- @H_502_0@/*新建木头角色*/
- @H_502_0@mMutou=Mutou::create();
- @H_502_0@
- @H_502_0@/*初始化木头的状态为休息*/
- @H_502_0@mMutou->changeState(enStateRest);
- @H_502_0@
- @H_502_0@this->addChild(mMutou);
- @H_502_0@bRet=true;
- @H_502_0@}while(0);
- @H_502_0@
- @H_502_0@returnbRet;
- @H_502_0@}
- @H_502_0@#include"Mutou.h"
- @H_502_0@
- @H_502_0@boolMutou::init(){
- @H_502_0@this->scheduleUpdate();
- @H_502_0@returntrue;
- @H_502_0@}
- @H_502_0@
- @H_502_0@voidMutou::changeState(EnumStateenState){
- @H_502_0@this->enCurState=enState;
- @H_502_0@}
- @H_502_0@
- @H_502_0@boolMutou::isTire(){
- @H_502_0@/*每次问木头累不累,他都会说:累~*/
- @H_502_0@returntrue;
- @H_502_0@}
- @H_502_0@
- @H_502_0@boolMutou::isWantToWriteArticle(){
- @H_502_0@/*有10%的概率想写教程(好懒~!)*/
- @H_502_0@floatran=CCRANDOM_0_1();
- @H_502_0@if(ran<0.1f){
- @H_502_0@returntrue;
- @H_502_0@}
- @H_502_0@
- @H_502_0@returnfalse;
- @H_502_0@}
- @H_502_0@
- @H_502_0@voidMutou::writeCode(){
- @H_502_0@CCLOG("mutouiswirtingCode.");
- @H_502_0@}
- @H_502_0@
- @H_502_0@voidMutou::writeArticle(){
- @H_502_0@CCLOG("mutouiswritingarticle.");
- @H_502_0@}
- @H_502_0@
- @H_502_0@
- @H_502_0@voidMutou::rest(){
- @H_502_0@CCLOG("mutouisresting.");
- @H_502_0@}
- @H_502_0@
- @H_502_0@voidMutou::update(floatdt){
- @H_502_0@/*判断在每一种状态下应该做什么事情*/
- @H_502_0@switch(enCurState){
- @H_502_0@caseenStateWriteCode:
- @H_502_0@/*如果累了就休息,并且切换到休息状态*/
- @H_502_0@if(isTire()){
- @H_502_0@rest();
- @H_502_0@changeState(enStateRest);
- @H_502_0@}
- @H_502_0@break;
- @H_502_0@caseenStateWriteArticle:
- @H_502_0@
- @H_502_0@if(isTire()){
- @H_502_0@rest();
- @H_502_0@changeState(enStateRest);
- @H_502_0@}
- @H_502_0@break;
- @H_502_0@caseenStateRest:
- @H_502_0@/*一定的概率写代码,一定的概率写教程,并且切换到相应的状态*/
- @H_502_0@if(isWantToWriteArticle()){
- @H_502_0@writeArticle();
- @H_502_0@changeState(enStateWriteArticle);
- @H_502_0@}
- @H_502_0@else{
- @H_502_0@writeCode();
- @H_502_0@changeState(enStateWriteCode);
- @H_502_0@}
- @H_502_0@break;
- @H_502_0@}
- @H_502_0@}