-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathcommand.h
More file actions
288 lines (248 loc) · 5.35 KB
/
Copy pathcommand.h
File metadata and controls
288 lines (248 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//
// command.h
// cpp
//
// Created by Bob Nystrom on 10/7/13.
// Copyright (c) 2013 Bob Nystrom. All rights reserved.
//
#ifndef cpp_command_h
#define cpp_command_h
namespace CommandPattern
{
enum Button
{
BUTTON_UP,
BUTTON_DOWN,
BUTTON_LEFT,
BUTTON_RIGHT,
BUTTON_X,
BUTTON_Y,
BUTTON_A,
BUTTON_B
};
bool isPressed(Button button) { return false; }
void jump() {}
void fireGun() {}
void swapWeapon() {}
void lurchIneffectively() {}
namespace BeforeCommand
{
class InputHandler
{
public:
void handleInput();
};
//^handle-input
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();
}
//^handle-input
}
namespace InputHandlingCommand
{
//^command
class Command
{
public:
virtual ~Command() {}
virtual void execute() = 0;
};
//^command
//^command-classes
class JumpCommand : public Command
{
public:
virtual void execute() { jump(); }
};
class FireCommand : public Command
{
public:
virtual void execute() { fireGun(); }
};
// 你知道思路了吧
//^command-classes
//^input-handler-class
class InputHandler
{
public:
void handleInput();
// 绑定命令的方法……
private:
Command* buttonX_;
Command* buttonY_;
Command* buttonA_;
Command* buttonB_;
};
//^input-handler-class
//^handle-input-commands
void InputHandler::handleInput()
{
if (isPressed(BUTTON_X)) buttonX_->execute();
else if (isPressed(BUTTON_Y)) buttonY_->execute();
else if (isPressed(BUTTON_A)) buttonA_->execute();
else if (isPressed(BUTTON_B)) buttonB_->execute();
}
//^handle-input-commands
}
namespace CommandedActors
{
class GameActor
{
public:
void jump() {}
};
//^actor-command
class Command
{
public:
virtual ~Command() {}
virtual void execute(GameActor& actor) = 0;
};
//^actor-command
//^jump-actor
class JumpCommand : public Command
{
public:
virtual void execute(GameActor& actor)
{
actor.jump();
}
};
//^jump-actor
class InputHandler
{
public:
Command* handleInput();
private:
Command* buttonX_;
Command* buttonY_;
Command* buttonA_;
Command* buttonB_;
};
//^handle-input-return
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_;
// 没有按下任何按键,就什么也不做
return NULL;
}
//^handle-input-return
void executeCommand()
{
InputHandler inputHandler;
GameActor actor;
//^call-actor-command
Command* command = inputHandler.handleInput();
if (command)
{
command->execute(actor);
}
//^call-actor-command
use(actor);
}
}
namespace Undo
{
class Unit {
public:
int x() { return 0; }
int y() { return 0; }
void moveTo(int x, int y) {}
};
namespace UndoBefore
{
class Command
{
public:
virtual ~Command() {}
virtual void execute() = 0;
};
//^move-unit
class MoveUnitCommand : public Command
{
public:
MoveUnitCommand(Unit* unit, int x, int y)
: unit_(unit),
x_(x),
y_(y)
{}
virtual void execute()
{
unit_->moveTo(x_, y_);
}
private:
Unit* unit_;
int x_, y_;
};
//^move-unit
Unit* getSelectedUnit() { return NULL; }
//^get-move
Command* handleInput()
{
Unit* unit = getSelectedUnit();
if (isPressed(BUTTON_UP)) {
// 向上移动单位
int destY = unit->y() - 1;
return new MoveUnitCommand(unit, unit->x(), destY);
}
if (isPressed(BUTTON_DOWN)) {
// 向下移动单位
int destY = unit->y() + 1;
return new MoveUnitCommand(unit, unit->x(), destY);
}
// 其他的移动……
return NULL;
}
//^get-move
}
namespace UndoAfter
{
//^undo-command
class Command
{
public:
virtual ~Command() {}
virtual void execute() = 0;
virtual void undo() = 0;
};
//^undo-command
//^undo-move-unit
class MoveUnitCommand : public Command
{
public:
MoveUnitCommand(Unit* unit, int x, int y)
: unit_(unit),
xBefore_(0),
yBefore_(0),
x_(x),
y_(y)
{}
virtual void execute()
{
// 保存移动之前的位置
// 这样之后可以复原。
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_;
};
//^undo-move-unit
}
}
}
#endif