forked from munificent/game-programming-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflyweight.h
More file actions
264 lines (234 loc) · 4.93 KB
/
Copy pathflyweight.h
File metadata and controls
264 lines (234 loc) · 4.93 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
//
// flyweight.h
// cpp
//
// Created by Bob Nystrom on 9/21/13.
// Copyright (c) 2013 Bob Nystrom. All rights reserved.
//
#ifndef cpp_flyweight_h
#define cpp_flyweight_h
namespace Flyweight
{
class Mesh {};
class Skeleton {};
class Texture {};
class Pose {};
class Vector {};
class Color {};
static const int WIDTH = 1024;
static const int HEIGHT = 1024;
static Texture GRASS_TEXTURE;
static Texture HILL_TEXTURE;
static Texture RIVER_TEXTURE;
int random(int max) { return 0; }
namespace HeavyTree
{
//^heavy-tree
class Tree
{
private:
Mesh mesh_;
Texture bark_;
Texture leaves_;
Vector position_;
double height_;
double thickness_;
Color barkTint_;
Color leafTint_;
};
//^heavy-tree
}
namespace SplitTree
{
//^tree-model
class TreeModel
{
private:
Mesh mesh_;
Texture bark_;
Texture leaves_;
};
//^tree-model
//^split-tree
class Tree
{
private:
TreeModel* model_;
Vector position_;
double height_;
double thickness_;
Color barkTint_;
Color leafTint_;
};
//^split-tree
}
namespace TerrainEnum
{
//^terrain-enum
enum Terrain
{
TERRAIN_GRASS,
TERRAIN_HILL,
TERRAIN_RIVER
// Other terrains...
};
//^terrain-enum
//^enum-world
class World
{
private:
Terrain tiles_[WIDTH][HEIGHT];
//^omit
int getMovementCost(int x, int y);
bool isWater(int x, int y);
//^omit
};
//^enum-world
//^enum-data
int World::getMovementCost(int x, int y)
{
switch (tiles_[x][y])
{
case TERRAIN_GRASS: return 1;
case TERRAIN_HILL: return 3;
case TERRAIN_RIVER: return 2;
// Other terrains...
}
}
bool World::isWater(int x, int y)
{
switch (tiles_[x][y])
{
case TERRAIN_GRASS: return false;
case TERRAIN_HILL: return false;
case TERRAIN_RIVER: return true;
// Other terrains...
}
}
//^enum-data
}
namespace TerrainClass
{
//^terrain-class
class Terrain
{
public:
Terrain(int movementCost,
bool isWater,
Texture texture)
: movementCost_(movementCost),
isWater_(isWater),
texture_(texture)
{}
int getMovementCost() const { return movementCost_; }
bool isWater() const { return isWater_; }
const Texture& getTexture() const { return texture_; }
private:
int movementCost_;
bool isWater_;
Texture texture_;
};
//^terrain-class
//^world-terrain-pointers
class World
{
//^omit
public:
World()
: grassTerrain_(1, false, GRASS_TEXTURE),
hillTerrain_(3, false, HILL_TEXTURE),
riverTerrain_(2, true, RIVER_TEXTURE)
{}
const Terrain& getTile(int x, int y) const;
//^omit
private:
Terrain* tiles_[WIDTH][HEIGHT];
// Other stuff...
//^omit
Terrain grassTerrain_;
Terrain hillTerrain_;
Terrain riverTerrain_;
void generateTerrain();
//^omit
};
//^world-terrain-pointers
//^generate
void World::generateTerrain()
{
// Fill the ground with grass.
for (int x = 0; x < WIDTH; x++)
{
for (int y = 0; y < HEIGHT; y++)
{
// Sprinkle some hills.
if (random(10) == 0)
{
tiles_[x][y] = &hillTerrain_;
}
else
{
tiles_[x][y] = &grassTerrain_;
}
}
}
// Lay a river.
int x = random(WIDTH);
for (int y = 0; y < HEIGHT; y++) {
tiles_[x][y] = &riverTerrain_;
}
}
//^generate
//^get-tile
const Terrain& World::getTile(int x, int y) const
{
return *tiles_[x][y];
}
//^get-tile
void foo()
{
World world;
//^use-get-tile
int cost = world.getTile(2, 3).getMovementCost();
//^use-get-tile
use(cost);
}
}
namespace WorldTerrain
{
class Terrain
{
public:
Terrain(int movementCost,
bool isWater,
Texture texture)
: movementCost_(movementCost),
isWater_(isWater),
texture_(texture)
{}
int getMovementCost() const { return movementCost_; }
bool isWater() const { return isWater_; }
const Texture& getTexture() const { return texture_; }
private:
int movementCost_;
bool isWater_;
Texture texture_;
};
//^world-terrain
class World
{
public:
World()
: grassTerrain_(1, false, GRASS_TEXTURE),
hillTerrain_(3, false, HILL_TEXTURE),
riverTerrain_(2, true, RIVER_TEXTURE)
{}
private:
Terrain grassTerrain_;
Terrain hillTerrain_;
Terrain riverTerrain_;
// Other stuff...
};
//^world-terrain
}
}
#endif