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. 谷歌浏览器,火狐浏览器,ie浏览器解析顺序
  2. Python忽略warning警告错误
  3. CSS样式使用
  4. Django项目中model增加了新字段怎样更新?
  5. Myeclipse 主题下载
  6. ASP.NET路由系统实现原理:HttpHandler的动态映射
  7. NodeJS V8 GC概览
  8. OpenWebFlow0.9用户手册与设计说明
  9. C#委托的语法
  10. ubuntu intelliJ IDEA 12.1.4 安装
  11. Java课程设计——学生成绩管理系统(201521123003 董美凤)
  12. 谈谈如何选择合适的MySQL数据类型
  13. Maven项目配置logback
  14. 第五周作业--测试与版本发布(Alpha版本)
  15. typescript枚举,类型推论,类型兼容性,高级类型,Symbols(学习笔记非干货)
  16. css scrollbar样式设置
  17. Oracel 中的分页
  18. configure new Linux/Mac
  19. 【转】微信jssdk录音功能开发记录
  20. mybatis 教程

热门文章

  1. Android Studio gradle 文件中 ${supportLibVersion} 用法
  2. 安装Struts2 类库
  3. dynamic_cast, RTTI, 整理
  4. 查看vnc server的日志
  5. hdu4847:Wow! Such Doge!(字符串匹配)
  6. Elasticsearch宕机问题
  7. 【BZOJ3331】[BeiJing2013]压力 Tarjan求点双
  8. 【BZOJ4177】Mike的农场 最小割
  9. 记录-spring MultipartFile 文件上传
  10. 我的Android进阶之旅------>Android之动画之Frame Animation实例