-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPool.cpp
More file actions
389 lines (313 loc) · 8.95 KB
/
Pool.cpp
File metadata and controls
389 lines (313 loc) · 8.95 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include "../Include/defGameEngine.hpp"
#include <list>
constexpr float BALL_MASS = 284.0f;
constexpr float BALL_RADIUS = 20.0f;
constexpr float BORDER_WIDTH = 30.0f;
constexpr float POCKET_RADIUS = BORDER_WIDTH * 2.5f;
constexpr float FORCE_ACC = 5.0f;
struct Ball
{
enum Type
{
WHITE,
BLACK,
POCKET
};
uint8_t type;
uint8_t id;
def::Vector2f acc;
def::Vector2f vel;
def::Vector2f pos;
float mass;
float radius;
bool inHole = false;
bool IsOverlap(Ball& b)
{
float dist = radius + b.radius;
return fabs((pos - b.pos).Length2()) <= dist * dist;
}
bool IsInside(const def::Vector2f& p)
{
return fabs((pos - p).Length2()) <= radius * radius;
}
void Update(const float deltaTime, const def::Vector2f& tl, const def::Vector2f& br, const float bw)
{
acc = vel * -0.98f;
vel += acc * deltaTime;
pos += vel * deltaTime;
if (fabs(vel.Length2()) < 0.01f)
vel = { 0.0f, 0.0f };
}
};
struct Table
{
Table() = default;
Table(const def::Vector2f& tl, const def::Vector2f& br, const float brdWidth, const float pctRadius)
: boundary{ tl, br }, borderWidth(brdWidth) {}
void AddBall(const def::Vector2f& pos, const float radius, const uint8_t type = Ball::WHITE)
{
Ball b;
b.pos = pos;
b.radius = radius;
b.mass = BALL_MASS;
b.type = type;
b.id = balls.size();
if (type == Ball::BLACK)
balls.push_front(b);
else
balls.push_back(b);
}
void Clear()
{
balls.clear();
}
void UpdateBalls(const float deltaTime, int& score)
{
for (auto& ball : balls)
ball.Update(deltaTime, boundary.first, boundary.second, borderWidth);
auto removeAt = std::remove_if(balls.begin(), balls.end(), [](const Ball& b) { return b.inHole; });
if (removeAt != balls.end()) balls.erase(removeAt);
if (balls.empty())
{
// If there is no balls then assume, that they failed,
// because there must be at least a black ball
gameOver = true;
won = false;
}
else
{
// If there is only one ball...
if (balls.size() == 1)
{
gameOver = true;
// ...and it's black
won = (balls.back().type == Ball::BLACK);
}
else
{
if (balls.front().type != Ball::BLACK)
{
// They hit a black ball in a pocket
gameOver = true;
won = false;
}
}
// if there is more than one ball then we don't care
}
std::vector<std::pair<Ball*, Ball*>> collidingPairs;
StaticCollision(collidingPairs, score);
DynamicCollision(collidingPairs);
}
void UpdateCue(def::GameEngine* dge)
{
if (dge->GetInput()->GetButtonState(def::Button::LEFT).pressed)
{
selected = nullptr;
for (auto& ball : balls)
{
if (ball.type == Ball::BLACK && ball.IsInside(dge->GetInput()->GetMousePosition()))
{
selected = &ball;
break;
}
}
}
if (dge->GetInput()->GetButtonState(def::Button::LEFT).released)
{
if (selected)
selected->vel = FORCE_ACC * (selected->pos - def::Vector2f(dge->GetInput()->GetMousePosition()));
selected = nullptr;
}
}
void Update(def::GameEngine* dge, const float deltaTime, int& score)
{
UpdateBalls(deltaTime, score);
UpdateCue(dge);
}
void StaticCollision(std::vector<std::pair<Ball*, Ball*>>& collidingPairs, int& score)
{
for (auto& ball : balls)
{
for (auto& target : balls)
{
if (ball.id != target.id)
{
if (ball.IsOverlap(target))
{
if (ball.type == Ball::POCKET)
{
target.inHole = true;
score += (target.type == Ball::WHITE) ? 1 : -1;
if (score < 0) score = 0;
}
else if (target.type == Ball::POCKET) {}
else
{
collidingPairs.push_back({ &ball, &target });
float dist = (ball.pos - target.pos).Length();
float overlap = 0.5f * (dist - ball.radius - target.radius);
def::Vector2f vel = overlap * (ball.pos - target.pos) / dist;
ball.pos -= vel;
target.pos += vel;
}
}
}
}
if (ball.type != Ball::POCKET)
{
if (
ball.pos.y < boundary.first.y + borderWidth + ball.radius ||
ball.pos.y > boundary.second.y - borderWidth - ball.radius
)
ball.vel.y = -ball.vel.y;
if (
ball.pos.x < boundary.first.x + borderWidth + ball.radius ||
ball.pos.x > boundary.second.x - borderWidth - ball.radius
)
ball.vel.x = -ball.vel.x;
}
}
}
void DynamicCollision(const std::vector<std::pair<Ball*, Ball*>>& collidingPairs)
{
for (auto& pair : collidingPairs)
{
Ball* b1 = pair.first;
Ball* b2 = pair.second;
float dist = (b1->pos - b2->pos).Length();
def::Vector2f norm = (b2->pos - b1->pos) / dist;
def::Vector2f k = b1->vel - b2->vel;
float p = 2.0f * norm.DotProduct(k) / (b1->mass + b2->mass);
b1->vel -= p * b2->mass * norm;
b2->vel += p * b1->mass * norm;
}
}
void DrawBalls(def::GameEngine* dge)
{
for (const auto& ball : balls)
{
def::Pixel col;
switch (ball.type)
{
case Ball::WHITE: col = def::WHITE; break;
case Ball::BLACK: col = def::BLACK; break;
case Ball::POCKET: col = def::DARK_BROWN; break;
}
dge->FillCircle(ball.pos, ball.radius, col);
}
}
void DrawCue(def::GameEngine* dge, const def::Pixel& col = def::DARK_GREY)
{
if (selected)
dge->DrawLine(selected->pos, dge->GetInput()->GetMousePosition(), col);
}
void DrawBoundary(def::GameEngine* dge, const def::Pixel& col = def::BROWN)
{
auto& b = boundary;
dge->FillRectangle(b.first, def::Vector2i(b.second.x - b.first.x - borderWidth, borderWidth), col);
dge->FillRectangle(b.second.x - borderWidth, b.first.y, borderWidth, b.second.y - b.first.y - borderWidth, col);
dge->FillRectangle(b.first.x, b.first.x + borderWidth, borderWidth, b.second.y - b.first.y - borderWidth, col);
dge->FillRectangle(b.first.x + borderWidth, b.second.y - borderWidth, b.second.x - b.first.x - borderWidth, borderWidth, col);
}
void Draw(def::GameEngine* dge)
{
DrawBalls(dge);
DrawBoundary(dge);
DrawCue(dge);
}
Ball* selected = nullptr;
std::list<Ball> balls;
std::pair<def::Vector2f, def::Vector2f> cuePos;
std::pair<def::Vector2f, def::Vector2f> boundary;
float borderWidth;
bool won = false;
bool gameOver = false;
};
class Pool : public def::GameEngine
{
public:
Pool()
{
GetWindow()->SetTitle("Pool");
}
~Pool()
{
delete table;
}
Table* table = nullptr;
int score = 0;
def::Vector2f topLeft;
def::Vector2f bottomRight;
void ResetTable(const def::Vector2f& tl, const def::Vector2f& br, const float borderWidth = BORDER_WIDTH, const float ballRadius = BALL_RADIUS, const float pocketRadius = POCKET_RADIUS)
{
table->Clear();
table->AddBall(def::Vector2f(GetWindow()->GetScreenWidth() * 0.4f + ballRadius * 4 + 4, GetWindow()->GetScreenHeight() - borderWidth * 5), ballRadius, Ball::BLACK);
table->AddBall(def::Vector2f(GetWindow()->GetScreenWidth() * 0.4f, borderWidth * 5), ballRadius);
for (int i = 0; i < 4; i++)
table->AddBall(table->balls.back().pos + def::Vector2f(ballRadius * 2 + 2, 0), ballRadius);
table->AddBall(def::Vector2f(GetWindow()->GetScreenWidth() * 0.4f + ballRadius + 1, borderWidth * 5 + ballRadius * 2 + 2), ballRadius);
for (int i = 0; i < 3; i++)
table->AddBall(table->balls.back().pos + def::Vector2f(ballRadius * 2 + 2, 0), ballRadius);
table->AddBall(def::Vector2f(GetWindow()->GetScreenWidth() * 0.4f + ballRadius * 2 + 2, borderWidth * 5 + ballRadius * 4 + 4), ballRadius);
for (int i = 0; i < 2; i++)
table->AddBall(table->balls.back().pos + def::Vector2f(ballRadius * 2 + 2, 0), ballRadius);
table->AddBall(def::Vector2f(GetWindow()->GetScreenWidth() * 0.4f + ballRadius * 3 + 3, borderWidth * 5 + ballRadius * 6 + 6), ballRadius);
table->AddBall(table->balls.back().pos + def::Vector2f(ballRadius * 2 + 2, 0), ballRadius);
table->AddBall(def::Vector2f(GetWindow()->GetScreenWidth() * 0.4f + ballRadius * 4 + 4, borderWidth * 5 + ballRadius * 8 + 8), ballRadius);
AddPockets(tl, br, pocketRadius);
}
void AddPockets(const def::Vector2f& tl, const def::Vector2f& br, const float radius = POCKET_RADIUS)
{
table->AddBall(tl, radius, Ball::POCKET);
table->AddBall(br, radius, Ball::POCKET);
table->AddBall({ tl.x, br.y }, radius, Ball::POCKET);
table->AddBall({ br.x, tl.y }, radius, Ball::POCKET);
}
bool IsGameOver()
{
if (table->gameOver)
{
table->gameOver = false;
if (table->won)
{
table->won = false;
// TODO: Add activity
}
return true;
}
return false;
}
void DrawScore(const def::Pixel& col = def::GREY)
{
DrawString(2, 2, "Score: " + std::to_string(score), col, 3, 3);
}
private:
bool OnUserCreate() override
{
topLeft = { 0.0f, 0.0f };
bottomRight = GetWindow()->GetScreenSize();
table = new Table(topLeft, bottomRight, BORDER_WIDTH, POCKET_RADIUS);
ResetTable(topLeft, bottomRight);
return true;
}
bool OnUserUpdate(float deltaTime) override
{
if (IsGameOver())
{
ResetTable(topLeft, bottomRight);
score = 0;
}
table->Update(this, deltaTime, score);
Clear(def::DARK_GREEN);
table->Draw(this);
DrawScore();
return true;
}
};
int main()
{
Pool app;
app.Construct(1024, 960, 1, 1);
app.Run();
return 0;
}