-
Notifications
You must be signed in to change notification settings - Fork 1
/
Paddle.cs
266 lines (232 loc) · 8.89 KB
/
Paddle.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
//-----------------------------------------------------------------------
// Paddle.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 Meadow.Foundation;
/// <summary>
/// Class representing the paddle
/// </summary>
public class Paddle
{
/// <summary>
/// Height of the paddle
/// </summary>
public const int HEIGHT = 3;
/// <summary>
/// How far to move the paddle left or right for one rotation instance
/// </summary>
private const int INCREMENT = 30;
/// <summary>
/// Amount to shrink the paddle width after each paddle hit. Shrinking
/// stops once the minimum width is reached.
/// </summary>
private const int SHRINK_AMOUNT = 3;
/// <summary>
/// Width of the display in pixels
/// </summary>
private readonly int displayWidth;
/// <summary>
/// Color of the paddle
/// </summary>
private readonly Color paddleColor = Color.White;
/// <summary>
/// Display background color
/// </summary>
private readonly Color backgroundColor;
/// <summary>
/// Reference to the graphics object
/// </summary>
private readonly AsyncGraphics asyncGraphics;
/// <summary>
/// Y-coordinate of the top of the paddle
/// </summary>
private readonly int y;
/// <summary>
/// Current x-coordinate of the left of the paddle
/// </summary>
private int position;
/// <summary>
/// Number of locks currently active for this object instance. Used for deadlock protection.
/// Since paddle movement is initiated asynchronously by turning the encoder knob it might
/// be possible for calls to the rotation 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 Paddle class
/// </summary>
/// <param name="asyncGraphics">Reference to the graphics object</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>
public Paddle(AsyncGraphics asyncGraphics, int displayWidth, int displayHeight, Color backgroundColor)
{
this.asyncGraphics = asyncGraphics;
this.displayWidth = displayWidth;
this.backgroundColor = backgroundColor;
this.y = displayHeight - (Paddle.HEIGHT / 2);
}
/// <summary>
/// Gets or sets the width of the paddle in pixels
/// </summary>
public int Width { get; set; }
/// <summary>
/// Gets the left x-coordinate of the paddle position.
/// </summary>
public int Left
{
get { return this.position; }
}
/// <summary>
/// Gets the x-coordinate of the right side of the paddle
/// </summary>
public int Right
{
get { return this.position + this.Width; }
}
/// <summary>
/// Gets the maximum left x-coordinate for the paddle
/// </summary>
public int MaxRight
{
get { return this.displayWidth - this.Width; }
}
/// <summary>
/// Reset the paddle, drawing it in the game-start position
/// </summary>
public void Reset()
{
this.position = this.displayWidth / 3;
this.Width = this.displayWidth / 3;
// draw the paddle in the starting position
this.Draw(this.position, this.position + this.Width, this.paddleColor);
}
/// <summary>
/// Move the paddle, making sure it stays between minimum and maximum
/// display limits, then erase the old paddle position and draw the
/// new paddle position.
/// </summary>
/// <param name="increment">How much to move the paddle.</param>
public void Move(int increment)
{
// calculate the new position
int newPosition = this.position + increment;
if (newPosition > this.MaxRight)
{
newPosition = this.MaxRight;
}
else if (newPosition < 0)
{
newPosition = 0;
}
else
{
// do nothing
}
if (this.position != newPosition)
{
int right = this.position + this.Width;
int delta = newPosition - this.position;
int oldPosition = this.position;
this.position = newPosition;
if (delta > 0)
{
if (this.lockCount < 1)
{
++this.lockCount;
// Moving right
// 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)
{
// Erase from the old left x-coordinate to the new left x-coordinate
this.Draw(oldPosition, oldPosition + delta, this.backgroundColor);
// Draw from the old right x-coordinate to the new right x-coordinate
this.Draw(right, right + delta, this.paddleColor);
}
--this.lockCount;
}
}
else if (delta < 0)
{
if (this.lockCount < 1)
{
++this.lockCount;
// Moving left
// 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)
{
// Draw from the old left x-coordinate to the new left x-coordinate
this.Draw(oldPosition, newPosition, this.paddleColor);
// Erase from the old right x-coordinate to the new right x-coordinate
this.Draw(right, right + delta, this.backgroundColor);
}
--this.lockCount;
}
}
else
{
// do nothing
}
}
}
/// <summary>
/// Move the paddle to the right
/// </summary>
public void MoveRight()
{
this.Move(Paddle.INCREMENT);
}
/// <summary>
/// Move the paddle to the left
/// </summary>
public void MoveLeft()
{
this.Move(-Paddle.INCREMENT);
}
/// <summary>
/// Shrink the paddle unless the minimum width has been reached
/// </summary>
public void Shrink()
{
if (this.Width > this.displayWidth / 5)
{
int oldRight = this.Right;
this.Width -= Paddle.SHRINK_AMOUNT;
this.Draw(oldRight, this.Right, this.backgroundColor);
}
}
/// <summary>
/// Draw the paddle. Used for both erasing the paddle at the old position
/// and drawing paddle at the new position.
/// </summary>
/// <param name="left">Left x-coordinate</param>
/// <param name="right">Right x-coordinate</param>
/// <param name="color">Paddle color</param>
private void Draw(int left, int right, Color color)
{
this.asyncGraphics.DrawLine(left, this.y, right, this.y, color);
}
}
}