三消游戏FSM状态机设计图

1) 设计FSM图

2) smc配置文件

///////////////////////////////////////////////////////////////////////
// ColorTable.sm
//   -- ColorTable State Map for C++ classes auto-generation
//
// 1) generate c++ classes:
//   $ java -jar Smc.jar -c++ ColorTable.sm
//
// 2) generate graphviz dot graph:
//   $ java -jar Smc.jar -graph -glevel 1 ColorTable.sm
//
// see also:
//   http://graphviz.org/
//
// Author: cheungmine
// Copyright 2015-?, All rights reserved.
//
///////////////////////////////////////////////////////////////////////
%class ColorTable
%header ColorTable.h
%start ColorTableMap::Vacancy
%map ColorTableMap
%%
/**
* State {
*     Transition [Guard Condition]
*		EndState {
*			Action(s)
*		}
*
*     Transition [context.getOwner().is_valid()]
*		EndState {
*			Action(s)
*		}
*     ...
* }
*/

//==> ColorTableMap::Vacancy
Vacancy
Entry {
	enterVacancy();
}
Exit {
	leaveVacancy();
}
{
	// Expose Transitions to other parts if necessary
	shuffle
		ShuffleFilled {
			shuffle();
		}
}

//==> ColorTableMap::ShuffleFilled
// only swappable state
ShuffleFilled
Entry {
	enterShuffleFilled();
}
Exit {
	leaveShuffleFilled();
}
{
	// !!can swap here!!
	swap [ context.getOwner().swap_done() ]
		Filled {
		}
	swap
		ShuffleFilled {
			// set stack top sub sm: SwapMap
		}
}

//==> ColorTableMap::FallenOver
FallenOver
Entry {
	enterFallenOver();
}
Exit {
    leaveFallenOver();
}
{
	// Expose Transitions to other parts if necessary
	fill [ context.getOwner().is_empty() ]
		Vacancy {
		}
	fill
		Filled {
			fill();
		}
}

//==> ColorTableMap::Filled
Filled
Entry {
	enterFilled();
}
Exit {
	leaveFilled();
}
{
	erase [ context.getOwner().can_erase() ]
		Unfilled {
			erase();
		}
	erase
		Vacancy {
			empty();
		}
}

//==> ColorTableMap::Unfilled
Unfilled
Entry {
	enterUnfilled();
}
Exit {
	leaveUnfilled();
}
{
	fall
		Fallen {
			fall();
		}
}

//==> ColorTableMap::Fallen
Fallen
Entry {
    enterFallen();
}
Exit {
	leaveFallen();
}
{
	erase [ context.getOwner().can_erase() ]
		Unfilled {
			erase();
		}

	erase
		FallenOver {
			empty();
		}
}
%%

3) ColorTable.h和ColorTable.cpp

/**
* ColorTable.h
* 2015-02-01
* cheungmine, all rights reserved.
*
*        col->
*      +-------+-------+-------+-------+-------+-------+-------+
*  row |00     |01     |02     |03     |04     |05     |06     |
*   |  |       |       |       |       |       |       |       |
*   v  |       |       |       |       |       |       |       |
*      +-------+-------+-------+-------+-------+-------+-------+
*      |10     |11     |12     |13     |14     |15     |16     |
*      |       |       |       |       |       |       |       |
*      |       |       |       |       |       |       |       |
*      +-------+-------+-------+-------+-------+-------+-------+
*      |20     |21     |22     |23     |24     |25     |26     |
*      |       |(xp,yp)|       |       |       |       |       | height
*      |       | .     |       |       |       |       |       |
*      +-------+-------+-------+-------+-------+-------+-------+
*      |30     |31     |32     |33     |34     |35     |36     |
*      |       |       |       |       |       |       |       |
*      |       |       |       |       |       |       |       |
*      +-------+-------+-------+-------+-------+-------+-------+
*  y   |40     |41     |42     |43     |44     |45     |46     |
*  ^   |       |       |       |       |       |       |       |
*  |   |       |       |       |       |       |       |       |
*  |   o-------+-------+-------+-------+-------+-------+-------+
*  |  origin                     width
*  +------------>x
* (0,0)
*
*/
#ifndef __COLORTABLE_H__
#define __COLORTABLE_H__

#include "cocos2d.h"
USING_NS_CC;

#include "ColorTable_sm.h"

class ColorTable : public Node
{
public:

    // Global Settings:

    static const int ColorTable_NumRows = 5;
    static const int ColorTable_NumCols = 7;
    static const int ColorTable_NumCells = ColorTable_NumRows*ColorTable_NumCols;
    static const int ColorTable_NumColors = 4;
    static const int ColorTable_ColorsPal[ColorTable_NumColors+1];    // colors palette

    // Construct and Deconstruct

    ColorTable() :
       swap_done_(false),
      _fsm(0),
      _DeltaTime(0.1f),
      _NumRows(ColorTable_NumCols),
      _NumCols(ColorTable_NumRows),
      _NumCells(ColorTable_NumCells)
    {
    }

    // Public Types:

    enum EnumColor
    {
        INVALID = 0,
        RED     = 1,
        GREEN   = 2,
        BLUE    = 3,
        YELLOW  = 4
    };

    // Public Inline Methods:

    inline int index(int row, int col) const
    {
        return (col*_NumCols + row);
    }

    inline int color(int row, int col) const
    {
        return table_[index(row, col)];
    }

    inline int result(int row, int col) const
    {
        return results_[index(row, col)];
    }

public:
    // Public Methods:
    CREATE_FUNC(ColorTable);

    virtual bool init();

    bool is_empty();

    bool can_erase();

    bool swap_done();

    void shuffle();    

    void empty();

    void erase();

    void fall();

    void fill();

    void print();

public:
    // Public Callbacks:
    void enterVacancy();
    void leaveVacancy();

    void enterShuffleFilled();
    void leaveShuffleFilled();

    void enterFallenOver();
    void leaveFallenOver();

    void enterFilled();
    void leaveFilled();

    void enterUnfilled();
    void leaveUnfilled();

    void enterFallen();
    void leaveFallen();

    // Public Events:
    void onIdleVacancy(float dt);
    void onIdleShuffleFilled(float dt);
    void onIdleFallenOver(float dt);
    void onIdleFilled(float dt);
    void onIdleUnfilled(float dt);
    void onIdleFallen(float dt);

private:
    // Private Methods:

    void init_table();

    void exit();

private:
    // Private Members:

    ColorTableContext * _fsm;

    const int _NumRows;
    const int _NumCols;
    const int _NumCells;
    const float _DeltaTime;

    bool swap_done_;

    int table_[ColorTable_NumCells];
    int results_[ColorTable_NumCells];
};

#endif // __COLORTABLE_H__

/**
* ColorTable.cpp
* 2015-02-01
* cheungmine, all rights reserved.
*
*/
#include "ColorTable.h"

const int ColorTable::ColorTable_ColorsPal[ColorTable_NumColors+1] = {'O', 'R', 'G', 'B', 'Y'};

/////////////////////////// Private Methods ////////////////////////////

void ColorTable::init_table()
{
    int fixed[] = {
        1,1,3,1,2,
        3,2,1,1,4,
        2,2,4,3,3,
        1,3,3,4,1,
        3,4,4,2,4,
        2,2,4,1,1,
        2,1,1,2,4
    };

    int at = 0;
    int * p = table_;

    while (p - table_ < _NumCells) {
        *p++ = fixed[at++];
    }
}

/////////////////////////// Public Callbacks ///////////////////////////

void ColorTable::enterVacancy()
{
    empty();

    this->schedule(schedule_selector(ColorTable::onIdleVacancy), _DeltaTime);
}

void ColorTable::leaveVacancy()
{
    exit();
}

void ColorTable::enterShuffleFilled()
{
    this->schedule(schedule_selector(ColorTable::onIdleShuffleFilled), _DeltaTime);
}

void ColorTable::leaveShuffleFilled()
{
    exit();
}

void ColorTable::enterFallenOver()
{
    this->schedule(schedule_selector(ColorTable::onIdleFallenOver), _DeltaTime);
}

void ColorTable::leaveFallenOver()
{
    exit();
}

void ColorTable::enterFilled()
{
    this->schedule(schedule_selector(ColorTable::onIdleFilled), _DeltaTime);
}

void ColorTable::leaveFilled()
{
    exit();
}

void ColorTable::enterUnfilled()
{
    this->schedule(schedule_selector(ColorTable::onIdleUnfilled), _DeltaTime);
}

void ColorTable::leaveUnfilled()
{
    exit();
}

void ColorTable::enterFallen()
{
    this->schedule(schedule_selector(ColorTable::onIdleFallen), _DeltaTime);
}

void ColorTable::leaveFallen()
{
    exit();
}

//////////////////////////// Public Events ////////////////////////////

void ColorTable::onIdleVacancy(float dt)
{
    _fsm->shuffle();
}

void ColorTable::onIdleShuffleFilled(float dt)
{
}

void ColorTable::onIdleFallenOver(float dt)
{
}

void ColorTable::onIdleFilled(float dt)
{
}

void ColorTable::onIdleUnfilled(float dt)
{
}

void ColorTable::onIdleFallen(float dt)
{
}

/////////////////////////// Public Methods ////////////////////////////

bool ColorTable::init()
{
    _fsm = new ColorTableContext(*this);
    _fsm->setDebugFlag(true);
    _fsm->enterStartState();

    return true;
}

void ColorTable::exit()
{
    this->unscheduleAllCallbacks();
}

bool ColorTable::is_empty()
{
    for (int index = 0; index < _NumCells; ++index) {
        if (table_[index] != INVALID) {
            return false;
        }
    }
    return true;
}

bool ColorTable::can_erase()
{
    return false;
}

bool ColorTable::swap_done()
{
    return swap_done_;
}

void ColorTable::shuffle()
{
    init_table();
}

void ColorTable::empty()
{
    for (int cell = 0; cell < _NumCells; ++cell) {
        table_[cell] = INVALID;
        results_[cell] = 0;
    }
}

void ColorTable::erase()
{
}

void ColorTable::fall()
{
}

void ColorTable::fill()
{
}

void ColorTable::print()
{
#if defined(COCOS2D_DEBUG) && defined(WIN32)
    FILE * fp = fopen("c:/temp/color-table.prt", "a+");
    if (fp) {
        fprintf(fp, "\n>>>> %s {", _fsm->getState().getName());
        fprintf(fp, "\n---- TABLE ----\n");
        for (int row = 0; row < ColorTable_NumRows; ++row) {
            for (int col = 0; col < ColorTable_NumCols; ++col) {
                int v = color(row, col);
                fprintf(fp, " %c", ColorTable_ColorsPal[v]);
            }
            fprintf(fp, "\n");
        }
        fprintf(fp, "\n---- RESULTS ----\n");
        for (int row = 0; row < ColorTable_NumRows; ++row) {
            for (int col = 0; col < ColorTable_NumCols; ++col) {
                int v = result(row, col);
                fprintf(fp, " %d", v);
            }
            fprintf(fp, "\n");
        }
        fprintf(fp, "}\n");
        fclose(fp);
    }
#endif
}

三消游戏FSM状态机设计图的更多相关文章

  1. C++ 三消游戏基本实现

    最近在研究三消算法,我想试试在完全不借助网络资源的情况下搞定这个东西,所以有些地方可能不是最优的. 代码留此备忘. 1. 3x_desk_event.h 1 #pragma once 2 3 #ifn ...

  2. 消消乐、candy crush类三消游戏程序逻辑分析

    最近在开发一款类似消消乐的三消游戏,在碰到实现斜方向下落的时候卡住了很长时间.好几天没有思路,原本的思路是一次性预判多个宝石的一连串运动路径,运用缓动运动队列来实现宝石运动路径,例如 下落->滑 ...

  3. cocos2d-x 3.2 它 三消游戏——万圣节大作战

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  4. Unity3d开发“类三消”游戏

    新建一个Project,导入图片素材和声音文件,把图片的Texture Type都修改为Sprite(2D and UI)[1].新建一个命名为Background的GameObject,为之添加背景 ...

  5. 最近用unity写三消游戏,mark一个准备用的unity插件,用来控制运动。

    http://www.pixelplacement.com/itween/index.php itween 听说还不错!

  6. [翻译]:Artificial Intelligence for games 5.3 STATE MACHINES:状态机

    目录 Chapter 5 Decision Making 5.3 STATE MACHINES:状态机 Chapter 5 Decision Making 5.3 STATE MACHINES:状态机 ...

  7. Creator开源游戏、插件、教程、视频汇总

    Creator开源游戏.插件.教程.视频汇总 来源 http://forum.cocos.com/t/creator/44782 王哲首席客服   17-03-17    4   史上最全,没有之一. ...

  8. UNITY 状态机 + SVN + 码云 下篇

    上篇说到自己写的一个FSM状态机,这篇写怎么把代码和码云联系在一起! 首先,我们应该知道为什么使用码云? 码云是开源中国社区2013年推出的基于 Git 的完全免费的代码托管服务,这个服务是基于 Gi ...

  9. unity 状态机 + svn + 码云 上篇

    最近刚找到在实习,忙于公司一个c++ 项目 ,一直想写博客来着,没时间写今天熬夜打算先献上自己前几天自己封装的一个fsm状态机 话不多说,直接上正题,这篇博客主要是在学校的时候状态机一直使用的是pla ...

随机推荐

  1. Xcode Organizational Identifiers

    操作系统(不管是iOS或是OS X)使用bundle标识去唯一标识你的应用.Bundle标识由一个组织id和你App的名字组成. 一般的,组织id是你域名的反转.如果你的域名是example.com那 ...

  2. shell编程--流程控制for,do-while,if-then,break,continue,case等

    2.5 流程控制 2.5.1 if语法 1.语法格式 if condition then     statements [elif condition     then statements. ..] ...

  3. Git 解决一个电脑多用户情况(win7)

    首先:在输入ssh-keygen -t rsa -C "注册邮箱"后不要急着按enter,此时输入秘钥对的文件名,不要跟默认文件重名(默认的是id_rsa) 划红线的地方就是新的文 ...

  4. [ExtJS5学习笔记]第七节 Extjs5的组件components及其模板事件方法学习

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/38487519 本文作者:sushengmiyan ------------------ ...

  5. Transform介绍(Unity3D开发之二)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=491 可能unity中接触较早的 ...

  6. 2. React JSX语法及特点介绍

    什么是JSX         JSX 是一种类 XML 语言,全称是 JavaScript XML .React 可以不使用 JSX来编写组件,但是使用JSX可以让代码可读性更高.语义更清晰.对 Re ...

  7. ubuntu14.04使用root用户登录桌面

    ubuntu安装好之后,默认是不能用root用户登录桌面的,只能使用普通用户或者访客登录.怎样开启root用户登录桌面呢? 先用普通用户登录,然后切换到root用户,然后执行如下命令: vi /usr ...

  8. Android动画深入分析

    动画分类 Android动画可以分3种:View动画,帧动画和属性动画:属性动画为API11的新特性,在低版本是无法直接使用属性动画的,但可以用nineoldAndroids来实现(但是本质还是vii ...

  9. iOS中 UISearchController 搜索栏 UI技术分享

    <p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; font-family: 'STHeiti Light' ...

  10. (NO.00003)iOS游戏简单的机器人投射游戏成形记(十九)

    如果看过前面博文的童鞋可能记得,我们在Level1中是通过写代码实现篮筐的走位.写代码不够直观,需要反复编译测试,有没有其他的方法呢? 答案自然是:大大的有 ;) SpriteBuilder宝贝自身已 ...