forked from munificent/game-programming-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubclass-sandbox.h
More file actions
330 lines (282 loc) · 5.14 KB
/
Copy pathsubclass-sandbox.h
File metadata and controls
330 lines (282 loc) · 5.14 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
typedef int SoundId;
typedef int ParticleType;
const SoundId SOUND_SPROING = 1;
const SoundId SOUND_SWOOP = 1;
const SoundId SOUND_DIVE = 1;
const ParticleType PARTICLE_DUST = 1;
const ParticleType PARTICLE_SPARKLES = 1;
namespace SimpleExample
{
//^1
class Superpower
{
public:
virtual ~Superpower() {}
protected:
virtual void activate() = 0;
void move(double x, double y, double z)
{
// Code here...
}
void playSound(SoundId sound, double volume)
{
// Code here...
}
void spawnParticles(ParticleType type, int count)
{
// Code here...
}
};
//^1
//^2
class SkyLaunch : public Superpower
{
protected:
virtual void activate()
{
// Spring into the air.
playSound(SOUND_SPROING, 1.0f);
spawnParticles(PARTICLE_DUST, 10);
move(0, 0, 20);
}
};
//^2
}
namespace Elaborated
{
//^3
class Superpower
{
protected:
//^omit
virtual void activate() = 0;
void move(double x, double y, double z) {}
void playSound(SoundId sound, double volume) {}
void spawnParticles(ParticleType type, int count) {}
//^omit
double getHeroX()
{
// Code here...
//^omit
return 0;
//^omit
}
double getHeroY()
{
// Code here...
//^omit
return 0;
//^omit
}
double getHeroZ()
{
// Code here...
//^omit
return 0;
//^omit
}
// Existing stuff...
};
//^3
//^4
class SkyLaunch : public Superpower
{
protected:
virtual void activate()
{
if (getHeroZ() == 0)
{
// On the ground, so spring into the air.
playSound(SOUND_SPROING, 1.0f);
spawnParticles(PARTICLE_DUST, 10);
move(0, 0, 20);
}
else if (getHeroZ() < 10.0f)
{
// Near the ground, so do a double jump.
playSound(SOUND_SWOOP, 1.0f);
move(0, 0, getHeroZ() + 20);
}
else
{
// Way up in the air, so do a dive attack.
playSound(SOUND_DIVE, 0.7f);
spawnParticles(PARTICLE_SPARKLES, 1);
move(0, 0, -getHeroZ());
}
}
};
//^4
}
namespace Forwarding
{
struct SoundEngine
{
void play(SoundId sound, double volume) {}
};
SoundEngine soundEngine_;
//^5
void playSound(SoundId sound, double volume)
{
soundEngine_.play(sound, volume);
}
//^5
}
namespace HelperClassBefore
{
//^6
class Superpower
{
protected:
void playSound(SoundId sound, double volume)
{
// Code here...
}
void stopSound(SoundId sound)
{
// Code here...
}
void setVolume(SoundId sound)
{
// Code here...
}
// Sandbox method and other operations...
};
//^6
};
namespace HelperClassAfter
{
//^7
class SoundPlayer
{
void playSound(SoundId sound, double volume)
{
// Code here...
}
void stopSound(SoundId sound)
{
// Code here...
}
void setVolume(SoundId sound)
{
// Code here...
}
};
//^7
//^8
class Superpower
{
protected:
SoundPlayer& getSoundPlayer()
{
return soundPlayer_;
}
// Sandbox method and other operations...
private:
SoundPlayer soundPlayer_;
};
//^8
}
namespace PassToConstructor
{
class ParticleSystem {};
//^pass-to-ctor-base
class Superpower
{
public:
Superpower(ParticleSystem* particles)
: particles_(particles)
{}
// Sandbox method and other operations...
private:
ParticleSystem* particles_;
};
//^pass-to-ctor-base
//^pass-to-ctor-sub
class SkyLaunch : public Superpower
{
public:
SkyLaunch(ParticleSystem* particles)
: Superpower(particles)
{}
};
//^pass-to-ctor-sub
}
namespace TwoStageInit
{
class ParticleSystem {};
class Superpower
{
public:
void init(ParticleSystem* particles) {}
};
ParticleSystem* particles;
class SkyLaunch : public Superpower {};
void foo()
{
//^9
Superpower* power = new SkyLaunch();
power->init(particles);
//^9
}
}
namespace TwoStageInitEncapsulated
{
class ParticleSystem {};
class Superpower
{
public:
void init(ParticleSystem* audio) {}
};
class SkyLaunch : public Superpower {};
//^10
Superpower* createSkyLaunch(ParticleSystem* particles)
{
Superpower* power = new SkyLaunch();
power->init(particles);
return power;
}
//^10
}
namespace StaticState
{
class ParticleSystem {};
//^11
class Superpower
{
public:
static void init(ParticleSystem* particles)
{
particles_ = particles;
}
// Sandbox method and other operations...
private:
static ParticleSystem* particles_;
};
//^11
}
namespace UseServiceLocator
{
struct ParticleSystem
{
void spawn(ParticleType type, int count);
};
ParticleSystem particles;
class Locator
{
public:
static ParticleSystem& getParticles() { return particles; }
};
//^12
class Superpower
{
protected:
void spawnParticles(ParticleType type, int count)
{
ParticleSystem& particles = Locator::getParticles();
particles.spawn(type, count);
}
// Sandbox method and other operations...
};
//^12
}