1_Command 游戏开发命令模式
2024-09-28 07:46:28
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_;
};
```
最新文章
- 谷歌浏览器,火狐浏览器,ie浏览器解析顺序
- Python忽略warning警告错误
- CSS样式使用
- Django项目中model增加了新字段怎样更新?
- Myeclipse 主题下载
- ASP.NET路由系统实现原理:HttpHandler的动态映射
- NodeJS V8 GC概览
- OpenWebFlow0.9用户手册与设计说明
- C#委托的语法
- ubuntu intelliJ IDEA 12.1.4 安装
- Java课程设计——学生成绩管理系统(201521123003 董美凤)
- 谈谈如何选择合适的MySQL数据类型
- Maven项目配置logback
- 第五周作业--测试与版本发布(Alpha版本)
- typescript枚举,类型推论,类型兼容性,高级类型,Symbols(学习笔记非干货)
- css scrollbar样式设置
- Oracel 中的分页
- configure new Linux/Mac
- 【转】微信jssdk录音功能开发记录
- mybatis 教程
热门文章
- Android Studio gradle 文件中 ${supportLibVersion} 用法
- 安装Struts2 类库
- dynamic_cast, RTTI, 整理
- 查看vnc server的日志
- hdu4847:Wow! Such Doge!(字符串匹配)
- Elasticsearch宕机问题
- 【BZOJ3331】[BeiJing2013]压力 Tarjan求点双
- 【BZOJ4177】Mike的农场 最小割
- 记录-spring MultipartFile 文件上传
- 我的Android进阶之旅------>Android之动画之Frame Animation实例