Skip to content

Commit f816cf0

Browse files
committed
(#2) Finish tskypatrol
1 parent b7bca25 commit f816cf0

File tree

2 files changed

+75
-47
lines changed

2 files changed

+75
-47
lines changed

LCDonald.Core/Games/TailsSkyPatrol.cs

+74-46
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using LCDonald.Core.Model;
22
using System;
3-
using System.Collections.Concurrent;
43
using System.Collections.Generic;
54
using System.Linq;
65
using System.Text;
@@ -73,8 +72,8 @@ public override List<LCDGameInput> GetAvailableInputs()
7372
private int _ringsMissed;
7473
private int _level;
7574

76-
private ConcurrentBag<int> _ringPositions = new ConcurrentBag<int>();
77-
private bool _blockRingSpawn;
75+
private List<int> _ringPositions = new List<int>();
76+
private int _blockRingSpawnCount;
7877

7978
private Random _rng = new Random();
8079

@@ -91,7 +90,7 @@ protected override List<string> GetVisibleElements()
9190

9291
elements.Add(GetTailsElement());
9392

94-
foreach (var ringPos in _ringPositions)
93+
foreach (var ringPos in ThreadSafeRingList())
9594
elements.Add("ring-" + ringPos);
9695

9796
return elements;
@@ -116,14 +115,15 @@ public override void InitializeGameState()
116115
_ringsMissed = 0;
117116
_level = 0;
118117

119-
_ringPositions = new ConcurrentBag<int>();
118+
_ringPositions = new List<int>();
120119

121120
_customUpdateSpeed = 900;
122121
QueueSound(new LCDGameSound("../common/game_start.ogg"));
123122

124-
// TODO wait for sound to end before spawning rings (w/animation?)
125-
_blockRingSpawn = true;
126-
//_isInputBlocked = true;
123+
// Wait for sound to end before spawning rings
124+
// 2x custom loop is about the correct speed.
125+
_blockRingSpawnCount = 2;
126+
_isInputBlocked = true;
127127
}
128128

129129
public override void HandleInputs(List<LCDGameInput> pressedInputs)
@@ -146,15 +146,14 @@ public override void HandleInputs(List<LCDGameInput> pressedInputs)
146146
protected override void UpdateCore()
147147
{
148148
// Collect rings if tails is in front
149-
foreach (var ringPos in _ringPositions)
149+
foreach (var ringPos in ThreadSafeRingList())
150150
{
151151
var digit = ringPos % 10;
152152

153153
if (digit == _tailsPosition && ringPos > 40)
154154
{
155155
QueueSound(new LCDGameSound("../common/hit.ogg"));
156-
var takeout = ringPos;
157-
_ringPositions.TryTake(out takeout);
156+
_ringPositions.Remove(ringPos);
158157
_ringsCollected++;
159158
}
160159
}
@@ -163,7 +162,7 @@ protected override void UpdateCore()
163162

164163
public override void CustomUpdate()
165164
{
166-
if (_ringsCollected == 30)
165+
if (_ringsCollected >= 30)
167166
{
168167
QueueSound(new LCDGameSound("../common/level_up.ogg"));
169168
_ringsMissed = 0;
@@ -179,68 +178,80 @@ public override void CustomUpdate()
179178
return;
180179
}
181180

182-
if (_blockRingSpawn)
183-
_blockRingSpawn = false;
184-
else
181+
if (_blockRingSpawnCount > 0)
185182
{
183+
_blockRingSpawnCount--;
184+
_isInputBlocked = false;
185+
}
186+
else
187+
{
186188
// Move rings forward
187-
var movedRings = _ringPositions.Select(x => x += 10).ToList();
188-
_ringPositions.Clear();
189-
foreach (var i in movedRings)
190-
_ringPositions.Add(i);
189+
_ringPositions = ThreadSafeRingList().Select(x => x += 10).ToList();
191190

192-
foreach (var ringPos in _ringPositions)
191+
foreach (var ringPos in ThreadSafeRingList())
193192
{
194193
if (ringPos > 50)
195194
{
196195
QueueSound(new LCDGameSound("../common/miss.ogg"));
197-
var takeout = ringPos;
198-
_ringPositions.TryTake(out takeout);
196+
_ringPositions.Remove(ringPos);
199197
_ringsMissed++;
200198

201199
// Pause ring spawn/advance if there was a miss
202-
_blockRingSpawn = true;
200+
_blockRingSpawnCount = 1;
203201

204202
var verticalPos = ringPos % 10;
205203

206204
// Show miss indicators depending on which rings were missed
207205
if (verticalPos == 1)
208-
BlinkElement(MISS_CENTER, 2);
206+
BlinkElement(MISS_CENTER, 1);
209207
else if (verticalPos == 2)
210208
{
211-
BlinkElement(MISS_CENTER, 2);
212-
BlinkElement(MISS_BOTTOM, 2);
209+
BlinkElement(MISS_CENTER, 1);
210+
BlinkElement(MISS_BOTTOM, 1);
213211

214212
}
215213
else if (verticalPos == 3)
216-
BlinkElement(MISS_BOTTOM, 2);
214+
BlinkElement(MISS_BOTTOM, 1);
217215
}
218216
}
219217

220218
if (_ringsMissed == 10)
219+
{
221220
GameOver();
222-
223-
// Spawn new row of rings
224-
var firstRing = _rng.Next(11, 14);
225-
_ringPositions.Add(firstRing);
226-
227-
// 50% chance to add a second ring
228-
if (_rng.Next(0, 2) == 0)
221+
return;
222+
}
223+
224+
// If rings were missed, cancel the previous forward move
225+
if (_blockRingSpawnCount > 0)
226+
_ringPositions = ThreadSafeRingList().Select(x => x -= 10).ToList();
227+
else
229228
{
230-
var secondRing = firstRing;
231-
232-
do { secondRing = _rng.Next(11, 14); } while (secondRing == firstRing);
229+
// Spawn new row of rings
230+
var firstRing = _rng.Next(11, 14);
233231
_ringPositions.Add(firstRing);
232+
233+
// 50% chance to add a second ring
234+
if (_rng.Next(0, 2) == 0)
235+
{
236+
var secondRing = firstRing;
237+
238+
do { secondRing = _rng.Next(11, 14); } while (secondRing == firstRing);
239+
_ringPositions.Add(secondRing);
240+
}
234241
}
235242
}
236243
}
237244

238245
private void GameOver()
239246
{
247+
_tailsPosition = -1;
248+
_level = 0;
249+
_ringPositions.Clear();
250+
240251
QueueSound(new LCDGameSound("../common/game_over.ogg"));
241252

242-
var gameOverFrame1 = new List<string> { TAILS_CENTER, RING_31, RING_32, RING_33 };
243-
var gameOverFrame2 = new List<string> { TAILS_CENTER };
253+
var gameOverFrame1 = new List<string> { RING_11, RING_12, RING_13, RING_21, RING_22, RING_23, RING_31, RING_32, RING_33, RING_41, RING_42, RING_43 };
254+
var gameOverFrame2 = new List<string>();
244255

245256
// slow 4x blink
246257
var gameOverAnimation = new List<List<string>> { gameOverFrame1, gameOverFrame1, gameOverFrame1, gameOverFrame1, gameOverFrame2, gameOverFrame2, gameOverFrame2, gameOverFrame2,
@@ -249,22 +260,39 @@ private void GameOver()
249260
gameOverFrame1, gameOverFrame1, gameOverFrame1, gameOverFrame1, gameOverFrame2, gameOverFrame2, gameOverFrame2, gameOverFrame2,
250261
gameOverFrame1, gameOverFrame1, gameOverFrame1, gameOverFrame1, gameOverFrame2, gameOverFrame2, gameOverFrame2, gameOverFrame2};
251262
PlayAnimation(gameOverAnimation);
252-
_tailsPosition = -1;
253263
Stop();
254264
}
255265

256266
private void Victory()
257267
{
268+
_tailsPosition = -1;
269+
_level = 0;
270+
_ringPositions.Clear();
271+
258272
QueueSound(new LCDGameSound("../common/game_win.ogg"));
259273

260-
var victoryFrame1 = GetAllGameElements();
261-
var victoryFrame2 = new List<string> { };
262-
263-
// slow 4x sequence
264-
var victoryAnimation = new List<List<string>> { victoryFrame1, victoryFrame1, victoryFrame1, victoryFrame1, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2};
274+
// 3x levels + 10x all
275+
var victoryFrame1 = new List<string> { LEVEL_1, LEVEL_2, LEVEL_3};
276+
var victoryFrame2 = new List<string>();
277+
var victoryFrame3 = GetAllGameElements();
278+
279+
var victoryAnimation = new List<List<string>> { victoryFrame1, victoryFrame1, victoryFrame1, victoryFrame1, victoryFrame1, victoryFrame1, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2,
280+
victoryFrame1, victoryFrame1, victoryFrame1, victoryFrame1, victoryFrame1, victoryFrame1, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2,
281+
victoryFrame1, victoryFrame1, victoryFrame1, victoryFrame1, victoryFrame1, victoryFrame1, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2,
282+
victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2,
283+
victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2,
284+
victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2,
285+
victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2,
286+
victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2,
287+
victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2,
288+
victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2,
289+
victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2,
290+
victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2,
291+
victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame3, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2, victoryFrame2};
265292
PlayAnimation(victoryAnimation);
266-
_tailsPosition = -1;
267293
Stop();
268294
}
295+
296+
private List<int> ThreadSafeRingList() => new List<int>(_ringPositions);
269297
}
270298
}

LCDonald.Core/Model/LCDGameBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ protected void PlayAnimation(List<List<string>> animation)
9393

9494
public void Start()
9595
{
96+
_isInputBlocked = false;
9697
InitializeGameState();
9798

9899
_customTimer?.Stop();
@@ -102,7 +103,6 @@ public void Start()
102103
_customTimer.Elapsed += UpdateGameState;
103104
_customTimer?.Start();
104105

105-
_isInputBlocked = false;
106106
_isStopped = false;
107107
Started?.Invoke(this, new EventArgs());
108108
}

0 commit comments

Comments
 (0)