码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2dx有限状态机

时间:2015-03-28 19:03:52      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:游戏开发   public   include   做什么   

在游戏开发中,状态机几乎是必不可少的,简单地说,状态机就是控制游戏对象在不同的状态下应该做什么事情的一个机制。



  1. #ifndef __MUTOU_H__  

  2. #define __MUTOU_H__  

  3.   

  4. #include "cocos2d.h"  

  5. USING_NS_CC;  

  6.   

  7. enum EnumState{  

  8.     enStateWriteCode,       /* 状态:写代码 */  

  9.     enStateWriteArticle,    /* 状态:写教程 */  

  10.     enStateRest,            /* 状态:休息 */  

  11. };  

  12.   

  13. class Mutou : public CCNode {  

  14. public:  

  15.     CREATE_FUNC(Mutou);  

  16.     virtual bool init();  

  17.   

  18.     /* 当前状态 */  

  19.     EnumState enCurState;  

  20.   

  21.     /* 判断是否写代码写累了 */  

  22.     bool isTire();  

  23.   

  24.     /* 是否想写教程 */  

  25.     bool isWantToWriteArticle();  

  26.   

  27.     /* 写代码 */  

  28.     void writeCode();  

  29.   

  30.     /* 写教程 */  

  31.     void writeArticle();  

  32.   

  33.     /* 休息 */  

  34.     void rest();  

  35.   

  36.     /* 切换状态 */  

  37.     void changeState(EnumState enState);  

  38. virtual void update(float dt);  

  39. };  

  40.   

  41. #endif  




  42. 输出:

    mutou is wirting Code.

    mutou is resting.

    mutou is wirting Code.

    mutou is resting.

    mutou is writing article.

    mutou is resting.

    mutou is wirting Code.

    mutou is resting.

    mutou is wirting Code.


    有限状态机的运用可以极大的简化程序 让代码更加的清晰易懂 便于阅读 当游戏主角能根据当前状态进行动作自动切换执行的时候可以极大的减少代码量

    1. bool HelloWorld::init()  

    2. {  

    3.     bool bRet = false;  

    4.     do   

    5.     {  

    6.         CC_BREAK_IF(! CCLayer::init());  

    7.   

    8.         /* 新建木头角色 */  

    9.         mMutou = Mutou::create();  

    10.   

    11.         /* 初始化木头的状态为休息 */  

    12.         mMutou->changeState(enStateRest);  

    13.   

    14.         this->addChild(mMutou);  

    15.         bRet = true;  

    16.     } while (0);  

    17.   

    18.     return bRet;  

    19. }  

    20. #include "Mutou.h"  

    21.   

    22. bool Mutou::init(){  

    23.     this->scheduleUpdate();  

    24.     return true;  

    25. }  

    26.   

    27. void Mutou::changeState( EnumState enState ) {  

    28.     this->enCurState = enState;  

    29. }  

    30.   

    31. bool Mutou::isTire() {  

    32.     /* 每次问木头累不累,他都会说:累~ */  

    33.     return true;  

    34. }  

    35.   

    36. bool Mutou::isWantToWriteArticle() {  

    37.     /* 有10%的概率想写教程(好懒~!) */  

    38.     float ran = CCRANDOM_0_1();  

    39.     if(ran < 0.1f) {  

    40.         return true;  

    41.     }  

    42.   

    43.     return false;  

    44. }  

    45.   

    46. void Mutou::writeCode() {  

    47.     CCLOG("mutou is wirting Code.");  

    48. }  

    49.   

    50. void Mutou::writeArticle() {  

    51.     CCLOG("mutou is writing article.");  

    52. }  

    53.   

    54.   

    55. void Mutou::rest() {  

    56.     CCLOG("mutou is resting.");  

    57. }  

    58.   

    59. void Mutou::update( float dt ) {  

    60.     /* 判断在每一种状态下应该做什么事情 */  

    61.     switch(enCurState) {  

    62.     case enStateWriteCode:  

    63.         /* 如果累了就休息,并且切换到休息状态 */  

    64.         if(isTire()) {  

    65.             rest();  

    66.             changeState(enStateRest);  

    67.         }  

    68.         break;  

    69.     case enStateWriteArticle:  

    70.         /* 如果累了就休息,并且切换到休息状态 */  

    71.         if(isTire()) {  

    72.             rest();  

    73.             changeState(enStateRest);  

    74.         }  

    75.         break;  

    76.     case enStateRest:  

    77.         /* 一定的概率写代码,一定的概率写教程,并且切换到相应的状态 */  

    78.         if(isWantToWriteArticle()) {  

    79.             writeArticle();  

    80.             changeState(enStateWriteArticle);  

    81.         }  

    82.         else {  

    83.             writeCode();  

    84.             changeState(enStateWriteCode);  

    85.         }  

    86.         break;  

    87.     }  

    88. }  



本文出自 “学习心得与笔记” 博客,请务必保留此出处http://cxdbk.blog.51cto.com/8808491/1625856

cocos2dx有限状态机

标签:游戏开发   public   include   做什么   

原文地址:http://cxdbk.blog.51cto.com/8808491/1625856

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!