-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ball.cs
538 lines (459 loc) · 18.6 KB
/
Ball.cs
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//-----------------------------------------------------------------------
// Ball.cs
//
// Copyright 2020 Lee Nayes, Logikos, Inc.
//
// This file is part of Solo Pong.
//
// Solo Pong is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Solo Pong is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Solo Pong (in COPYING.txt). If not,
// see <https://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------
namespace SoloPong
{
using System;
using System.Threading;
using System.Timers;
using Meadow.Foundation;
/// <summary>
/// Class representing the ball.
/// </summary>
public class Ball
{
/// <summary>
/// Milliseconds between ball position updates.
/// </summary>
private const int MOVE_INTERVAL = 200;
/// <summary>
/// Reference to the graphics interface
/// </summary>
private readonly AsyncGraphics asyncGraphics;
/// <summary>
/// Timer used for ball position updates
/// </summary>
private readonly System.Timers.Timer moveTimer = new System.Timers.Timer(Ball.MOVE_INTERVAL);
/// <summary>
/// Reference to the game's paddle object. Needed for collision detection.
/// </summary>
private readonly Paddle paddle;
/// <summary>
/// Random number generator
/// </summary>
private readonly Random random = new Random();
/// <summary>
/// Reference to the sound generator object
/// </summary>
private readonly ISounds soundGenerator;
/// <summary>
/// Reference to the score-keeper object
/// </summary>
private readonly ScoreKeeper scoreKeeper;
/// <summary>
/// Background color of the display
/// </summary>
private readonly Color backgroundColor;
/// <summary>
/// Maximum x coordinate for the upper left of the ball's enclosing rectangle
/// </summary>
private readonly int maxX;
/// <summary>
/// Maximum y coordinate for the upper left of the ball's enclosing rectangle
/// </summary>
private readonly int maxY;
/// <summary>
/// Minimum y coordinate for the upper left of the ball's enclosing rectangle
/// </summary>
private readonly int minY;
/// <summary>
/// Width of the ball
/// </summary>
private readonly int width = 10;
/// <summary>
/// Height of the ball
/// </summary>
private readonly int height = 10;
/// <summary>
/// Width of the display
/// </summary>
private readonly int displayWidth;
/// <summary>
/// Color to use for the ball
/// </summary>
private readonly Color color = Color.Red;
/// <summary>
/// Current x coordinate of the ball's enclosing rectangle
/// </summary>
private int xPosition = 5;
/// <summary>
/// Current y coordinate of the ball's enclosing rectangle
/// </summary>
private int yPosition;
/// <summary>
/// X increment to use for the next ball position update
/// </summary>
private int xIncrement;
/// <summary>
/// Y increment to use for the next ball position update
/// </summary>
private int yIncrement;
/// <summary>
/// Value indicating whether the current move is complete
/// </summary>
private bool isMoveComplete = true;
/// <summary>
/// Number of locks currently active for this object instance. Used for deadlock protection.
/// Since ball movement is initiated is initiated by a timer it might be possible for calls
/// to the elapsed handler to "stack up", which could lead to a crash due to deadlock,
/// especially if .net garbage collection occurrs and messes with the timing.
/// </summary>
private int lockCount = 0;
/// <summary>
/// Initializes a new instance of the Ball class.
/// </summary>
/// <param name="asyncGraphics">Graphics object reference</param>
/// <param name="displayWidth">Width of the display</param>
/// <param name="displayHeight">Height of the display</param>
/// <param name="backgroundColor">Background color of the display</param>
/// <param name="paddle">Reference to the game's paddle object</param>
/// <param name="soundGenerator">Reference to the game's sound generator object</param>
/// <param name="minimumY">Minimum y coordinate to use the the ball's enclosing rectangle</param>
/// <param name="scoreKeeper">Reference to the game's scorekeeper object</param>
public Ball(
AsyncGraphics asyncGraphics,
int displayWidth,
int displayHeight,
Color backgroundColor,
Paddle paddle,
ISounds soundGenerator,
int minimumY,
ScoreKeeper scoreKeeper)
{
this.asyncGraphics = asyncGraphics;
this.soundGenerator = soundGenerator;
this.scoreKeeper = scoreKeeper;
this.backgroundColor = backgroundColor;
this.paddle = paddle;
this.displayWidth = displayWidth;
this.maxX = displayWidth - this.width;
this.maxY = displayHeight - this.height - Paddle.HEIGHT;
this.minY = minimumY;
this.yPosition = this.minY + 5;
this.moveTimer.AutoReset = true;
this.moveTimer.Elapsed += this.MoveTimer_Elapsed;
}
/// <summary>
/// Delegate used for game-over notification
/// </summary>
/// <param name="sender">Object that initiated the event</param>
/// <param name="args">Event arguments</param>
public delegate void NotifyGameOver(object sender, EventArgs args);
/// <summary>
/// Event used for game-over notification
/// </summary>
public event NotifyGameOver ExplosionOccurred;
/// <summary>
/// Reset the ball position for the start of a new game instance
/// </summary>
public void Reset()
{
this.xPosition = this.random.Next(5, this.displayWidth - 5 - this.width);
this.yPosition = this.minY + 5;
int xDirectionRandom = this.random.Next(0, 1);
int directionMultiplier = xDirectionRandom > 0 ? -1 : 1;
int incrementRandom = this.random.Next(6, 10);
this.xIncrement = incrementRandom * directionMultiplier;
this.yIncrement = 20 - incrementRandom;
// draw the ball in the starting position
this.Draw(this.xPosition, this.yPosition, this.color);
}
/// <summary>
/// Start periodic ball movement
/// </summary>
public void StartMoving()
{
this.moveTimer.Start();
}
/// <summary>
/// Stop periodic ball movement
/// </summary>
public void StopMoving()
{
this.moveTimer.Stop();
}
/// <summary>
/// Event handler called when the movement timer elapses
/// </summary>
/// <param name="sender">Event sender</param>
/// <param name="e">Event arguments</param>
private void MoveTimer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
MeadowApp.DebugWriteLine("Move timer elapsed");
if (this.isMoveComplete)
{
this.isMoveComplete = false;
if (!this.CheckForCollision())
{
int oldX = this.xPosition;
int oldY = this.yPosition;
MeadowApp.DebugWriteLine($"Move timer elapsed: {oldX},{oldY} {xIncrement},{yIncrement}");
this.xPosition += this.xIncrement;
if (this.xPosition > this.maxX)
{
this.xPosition = this.maxX;
}
MeadowApp.DebugWriteLine($"new x = {this.xPosition}");
if (this.xPosition < 0)
{
this.xPosition = 0;
}
this.yPosition += this.yIncrement;
MeadowApp.DebugWriteLine($"new y = {this.yPosition}");
if (this.yPosition > this.maxY)
{
this.yPosition = this.maxY;
}
if (this.yPosition < this.minY)
{
this.yPosition = this.minY;
}
MeadowApp.DebugWriteLine($"new x,y = {this.xPosition},{this.yPosition}");
if (!(this.xPosition == oldX && this.yPosition == oldY))
{
if (this.lockCount < 1)
{
++this.lockCount;
// Lock to ensure that draw calls in the main thread do not interfere
// with the show calls in the async-graphics thread.
lock (this.asyncGraphics.LockObject)
{
if (this.moveTimer.Enabled)
{
this.Draw(oldX, oldY, this.backgroundColor);
this.Draw(this.xPosition, this.yPosition, this.color);
}
}
--this.lockCount;
}
}
}
this.isMoveComplete = true;
}
MeadowApp.DebugWriteLine("leaving move timer elapsed");
}
catch (Exception ex)
{
MeadowApp.DebugWriteLine($"Exception in MoveTimer_Elapsed: {ex}");
this.isMoveComplete = true;
}
}
/// <summary>
/// Modify the X increment to use for the next ball position move.
/// Adjusting the x and y speed on border and paddle collisions
/// makes the game more interesting. :)
/// </summary>
/// <param name="extraX">Additional value to add to the x increment</param>
private void ChangeXIncrement(int extraX)
{
this.xIncrement += this.random.Next(-1, 1);
this.xIncrement += extraX;
if (this.xIncrement < 0)
{
if (this.xIncrement > -7)
{
this.xIncrement = -7;
}
}
else
{
if (this.xIncrement < 7)
{
this.xIncrement = 7;
}
}
}
/// <summary>
/// Modify the Y increment to use for the next ball position move.
/// Adjusting the x and y speed on border and paddle collisions
/// makes the game more interesting. :)
/// </summary>
/// <param name="extraY">Additional value to add to the y increment</param>
private void ChangeYIncrement(int extraY)
{
this.yIncrement += this.random.Next(-1, 1) + extraY;
if (this.yIncrement < 0)
{
if (this.yIncrement > -7)
{
this.yIncrement = -7;
}
}
else
{
if (this.yIncrement < 7)
{
this.yIncrement = 7;
}
}
}
/// <summary>
/// Get the adjustments to use for the x and y increments based on the position
/// at which the ball strikes the paddle. Changing the x and y velocity like
/// this makes the game more interesting. :)
/// </summary>
/// <param name="ballCenterX">Current x coordinate of the bottom of the ball</param>
/// <param name="extraX">Out: Extra value to add to the x increment</param>
/// <param name="extraY">Out: Extra value to add to the y increment</param>
private void GetVelocityChangeAdjustments(int ballCenterX, out int extraX, out int extraY)
{
float oneThirdPaddleWidth = this.paddle.Width / 3;
if (this.xIncrement > 0)
{
// traveling right
if (ballCenterX < this.paddle.Left + oneThirdPaddleWidth)
{
extraX = -2; // a bit less horizontal velocity
extraY = -2; // a bit more vertical velocity
}
else if (ballCenterX > this.paddle.Left + (oneThirdPaddleWidth * 2))
{
extraX = 2; // a bit more horizontal velocity
extraY = 2; // a bit less vertical velocity
}
else
{
extraX = 0;
extraY = 0;
}
}
else
{
// traveling left
if (ballCenterX < this.paddle.Left + oneThirdPaddleWidth)
{
extraX = -2; // a bit more horizontal velocity
extraY = 2; // a bit less vertical velocity
}
else if (ballCenterX > this.paddle.Left + (oneThirdPaddleWidth * 2))
{
extraX = 2; // a bit less horizontal velocity
extraY = -2; // a bit more vertical velocity
}
else
{
extraX = 0;
extraY = 0;
}
}
}
/// <summary>
/// Check for collisions between the ball and the sides, top and bottom of the display,
/// also checking for collisions with the paddle.
/// </summary>
/// <returns>Value indicating that the paddle was missed, signaling game-over</returns>
private bool CheckForCollision()
{
bool isPaddleMissed = false;
bool isBorderHit = false;
int ballCenterX = this.xPosition + (this.width / 2);
MeadowApp.DebugWriteLine($"checkForCollision: {ballCenterX},{this.yPosition}");
if (this.yPosition >= this.maxY)
{
if (ballCenterX >= this.paddle.Left && ballCenterX < this.paddle.Right)
{
// Paddle hit!
this.soundGenerator.PlayPaddleHitSound();
this.scoreKeeper.Increment();
this.GetVelocityChangeAdjustments(ballCenterX, out int extraX, out int extraY);
this.yIncrement = -this.yIncrement;
// Apply the x and y increments
this.ChangeXIncrement(extraX);
this.ChangeYIncrement(extraY);
// Make the paddle smaller
this.paddle.Shrink();
}
else
{
// Missed the paddle. Time to explode...
this.asyncGraphics.Stop();
this.StopMoving();
this.soundGenerator.PlayGameOverSound();
this.Explode();
isPaddleMissed = true;
}
}
else
{
if (this.xPosition >= this.maxX || this.xPosition <= 0)
{
MeadowApp.DebugWriteLine("x border hit");
isBorderHit = true;
this.xIncrement = -this.xIncrement;
this.ChangeYIncrement(0);
}
if (this.yPosition <= this.minY)
{
MeadowApp.DebugWriteLine("y border hit");
isBorderHit = true;
this.yIncrement = -this.yIncrement;
this.ChangeXIncrement(0);
}
if (isBorderHit)
{
this.soundGenerator.PlayBorderHitSound();
}
}
MeadowApp.DebugWriteLine($"leaving checkForCollision: {isPaddleMissed}");
return isPaddleMissed;
}
/// <summary>
/// Display the game-over animation and trigger the game-over event.
/// </summary>
private void Explode()
{
int x = this.xPosition + (this.width / 2);
int y = this.yPosition - Paddle.HEIGHT;
int radius = 4;
int blackRadius = 0;
for (int ii = 0; ii < 4; ++ii)
{
this.asyncGraphics.DrawCircle(x, y, radius, Color.Yellow);
this.asyncGraphics.DrawCircle(x, y, (int)(radius * 0.7), Color.Orange);
this.asyncGraphics.DrawCircle(x, y, (int)(radius * 0.4), Color.Red);
blackRadius = (int)(radius * 0.2);
this.asyncGraphics.DrawCircle(x, y, blackRadius, Color.Black);
this.asyncGraphics.ShowDirect();
Thread.Sleep(100);
radius *= 2;
}
this.ExplosionOccurred?.Invoke(this, null);
}
/// <summary>
/// Draw the ball in the current position. This is used for both erasing the ball at the
/// previous position and drawing the ball at the new position.
/// </summary>
/// <param name="x">x coordinate of the upper left of the ball's enclosing rectangle</param>
/// <param name="y">y coordinate of the upper left of the ball's enclosing rectangle</param>
/// <param name="color">Color used to draw the ball</param>
private void Draw(int x, int y, Color color)
{
this.asyncGraphics.DrawCircle(
x: x + (this.width / 2),
y: y + (this.height / 2),
radius: this.width / 2,
color: color);
}
}
}