A dead simple implementation looks like:

```
// simple
void InputHandler::handleInput()
{
if (isPressed(BUTTON_X)) jump();
else if (isPressed(BUTTON_Y)) fireGun();
else if (isPressed(BUTTON_A)) swapWeapon();
else if (isPressed(BUTTON_B)) lurchIneffectively();
} // command pattern
class Command
{
public:
virtual ~Command() {}
virtual void execute() = ;
//virtual void execute(Actor *) = 0;
}; class JumpCommand : public Command
{
public:
virtual void execute() { jump(); }
}; class FireCommand : public Command
{
public:
virtual void execute() { fireGun(); }
}; class InputHandler
{
public:
void handleInput(); // Methods to bind commands... private:
Command* buttonX_;
Command* buttonY_;
Command* buttonA_;
Command* buttonB_;
}; Command* InputHandler::handleInput()
{
if (isPressed(BUTTON_X)) return buttonX_;
if (isPressed(BUTTON_Y)) return buttonY_;
if (isPressed(BUTTON_A)) return buttonA_;
if (isPressed(BUTTON_B)) return buttonB_; // Nothing pressed, so do nothing.
return NULL;
} Command* command = inputHandler.handleInput();
if (command)
{
command->execute(actor);
}
``` #### use command stream, decoupled producer and consumer ## undo and redo ``` class Command
{
public:
virtual ~Command() {}
virtual void execute() = ;
virtual void undo() = ;
}; class MoveUnitCommand : public Command
{
public:
MoveUnitCommand(Unit* unit, int x, int y)
: unit_(unit),
xBefore_(),
yBefore_(),
x_(x),
y_(y)
{} virtual void execute()
{
// Remember the unit's position before the move
// so we can restore it.
xBefore_ = unit_->x();
yBefore_ = unit_->y(); unit_->moveTo(x_, y_);
} virtual void undo()
{
unit_->moveTo(xBefore_, yBefore_);
} private:
Unit* unit_;
int xBefore_, yBefore_;
int x_, y_;
};
```

1_Command 游戏开发命令模式的更多相关文章

  1. iOS开发-命令模式

    命令模式算是设计模式中比较简单的,最常见的例子是工作任务安排下来进行编程,如果工作任务不需要完成,我们可以取消我们之前完成的代码,也可以理解为回滚撤销操作.这里面涉及到命令模式中的两个对象,一个是动作 ...

  2. 14_ Component 游戏开发组件模式

    # component 不同作用的程序需要保持互相隔离 我们不想ai 物理 渲染 声音 等等功能 耦合在一起,像下面这样 ``` //bad if (collidingWithFloor() & ...

  3. 游戏开发设计模式之命令模式(unity3d 示例实现)

    博主才学尚浅,难免会有错误,尤其是设计模式这种极富禅意且需要大量经验的东西,如果哪里书写错误或有遗漏,还请各位前辈指正. 打 算写设计模式的目的就是,首先自己可以理清思路,还有就是国内的设计模式资料很 ...

  4. 游戏开发设计模式之状态模式 & 有限状态机 & c#委托事件(unity3d 示例实现)

    命令模式:游戏开发设计模式之命令模式(unity3d 示例实现) 对象池模式:游戏开发设计模式之对象池模式(unity3d 示例实现) 原型模式:游戏开发设计模式之原型模式 & unity3d ...

  5. 游戏开发设计模式之原型模式 & unity3d JSON的使用(unity3d 示例实现)

    命令模式:游戏开发设计模式之命令模式(unity3d 示例实现) 对象池模式:游戏开发设计模式之对象池模式(unity3d 示例实现) 实现原型模式 原型模式带来的好处就是,想要构建生成任意独特对象的 ...

  6. 游戏开发设计模式之对象池模式(unity3d 示例实现)

    前篇:游戏开发设计模式之命令模式(unity3d 示例实现) 博主才学尚浅,难免会有错误,尤其是设计模式这种极富禅意且需要大量经验的东西,如果哪里书写错误或有遗漏,还请各位前辈指正. 原理:从一个固定 ...

  7. javascript设计模式与开发实践阅读笔记(9)——命令模式

    命令模式:有时候需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是什么,此时希望用一种松耦合的方式来设计软件,使得请求发送者和请求接收者能够消除彼此之间的耦合关系. 说法很复 ...

  8. MVC模式在游戏开发的应用

    原地址: http://www.cocoachina.com/gamedev/2012/1129/5212.html MVC是三个单词的缩写,分别为:模型(Model).视图(View)和控制Cont ...

  9. javascript开发HTML5游戏--斗地主(单机模式part3)

    最近学习使用了一款HTML5游戏引擎(青瓷引擎),并用它尝试做了一个斗地主的游戏,简单实现了单机对战和网络对战,代码可已放到github上,在此谈谈自己如何通过引擎来开发这款游戏的. 客户端代码 服务 ...

随机推荐

  1. 速记const 指针与指向const的指针

    指向const的指针.它的意思是指针指向的内容是不能被改动的.它有两种写法. ` const int* p; (推荐) int const* p;` 再说const指针.它的意思是指针本身的值是不能被 ...

  2. java 对象占用内存查看 以及JVM级别 方法修改等

    public interface Instrumentation 此类提供检测 Java 编程语言代码所需的服务.检测是向方法中添加字节码,以搜集各种工具所使用的数据.由于更改完全是进行添加,所以这些 ...

  3. shader一些语义或术语的解释

    1.unity内置的摄像机和屏幕参数: 2.unity中一些常用的包含文件: 3.unityCG.cginc中一些常用的结构体: 4.unityCG.cginc中一些常用的帮助函数: 5.从应用阶段传 ...

  4. F - 数论

    F - 数论 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description ...

  5. Springboot整合日志时候出现的问题

    上图是问题,按照路径去找下,发现其实是jar包重复导致的! 在对应的项目上,右键--->属性(Properties)--->JavaBuild Path  然后选择Libraries 页签 ...

  6. This instability is a fundamental problem for gradient-based learning in deep neural networks. vanishing exploding gradient problem

    The unstable gradient problem: The fundamental problem here isn't so much the vanishing gradient pro ...

  7. php, tp5, 选中导航菜单

    1. 首先定义一个函数: function nav_select($navindex){ $nav_arr = [ 1 => ['index',], 2 => ['mei',], 3 =& ...

  8. 微信H5支付开发步骤总结

    * 开发步骤: * 1.在微信公众号平台设置授权目录,即jsapi.php所在的目录 * 2.在微信支付平台下载证书,放到cert目录 * 3.在微信支付平台设置API秘钥,同时在WxPay.Conf ...

  9. js滚动到指定位置显示或隐藏元素

    $(function(){ $(window).scroll(function(){ var scroll_top=$(window).scrollTop(); console.log(scroll_ ...

  10. mydql练习答案

    .查询“生物”课程比“物理”课程成绩高的所有学生的学号: 思路: 获取所有有生物课程的人(学号,成绩) - 临时表 获取所有有物理课程的人(学号,成绩) - 临时表 根据[学号]连接两个临时表: 学号 ...