Skip to content

Commit a5ae982

Browse files
committed
Refactoring
1 parent 5cd748f commit a5ae982

File tree

2 files changed

+56
-83
lines changed

2 files changed

+56
-83
lines changed

AoC.Games/Games/Deflectors/Actors/ArenaManager.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,18 @@ public class ArenaManager : IActor
1616

1717
private Texture2D _other;
1818

19-
private int _levelNumber;
20-
21-
private (int X, int Y) _mirrorPosition;
22-
2319
public Level Level { get; private set; }
2420

2521
public (int X, int Y) LastMirrorPosition { get; private set; } = (-1, -1);
2622

27-
public (int X, int Y) MirrorPosition => _mirrorPosition;
23+
public (int X, int Y) MirrorPosition { get; private set; }
2824

2925
public char Mirror { get; private set; }
3026

3127
public int LevelCount => _levelDataProvider.LevelCount;
3228

33-
public int LevelNumber => _levelNumber;
34-
29+
public int LevelNumber { get; private set; }
30+
3531
public ArenaManager(Input input)
3632
{
3733
_levelDataProvider = new LevelDataProvider();
@@ -50,7 +46,7 @@ public void LoadContent(ContentManager contentManager)
5046

5147
public void SetLevel(int levelNumber)
5248
{
53-
_levelNumber = levelNumber;
49+
LevelNumber = levelNumber;
5450

5551
Level = _levelDataProvider.GetLevel(levelNumber);
5652

@@ -61,19 +57,19 @@ public void SetLevel(int levelNumber)
6157

6258
public void ResetLevel()
6359
{
64-
SetLevel(_levelNumber);
60+
SetLevel(LevelNumber);
6561
}
6662

6763
public void NextLevel()
6864
{
69-
_levelNumber++;
65+
LevelNumber++;
7066

71-
if (_levelNumber > _levelDataProvider.LevelCount)
67+
if (LevelNumber > _levelDataProvider.LevelCount)
7268
{
73-
_levelNumber = 1;
69+
LevelNumber = 1;
7470
}
7571

76-
SetLevel(_levelNumber);
72+
SetLevel(LevelNumber);
7773
}
7874

7975
public void PlaceMirror()
@@ -111,19 +107,19 @@ public void PlaceMirror()
111107

112108
public void Update()
113109
{
114-
LastMirrorPosition = _mirrorPosition;
110+
LastMirrorPosition = MirrorPosition;
115111

116112
var scaleFactor = AppSettings.Instance.ScaleFactor;
117113

118114
var position = (X: (int) (_input.MouseX / scaleFactor), Y: (int) (_input.MouseY / scaleFactor));
119115

120-
if (position.X >= 0 && position.X < Constants.MapSize * Constants.TileSize && position.Y >= Constants.TopOffset && position.Y < Constants.MapSize * Constants.TileSize + Constants.TopOffset && Mirror != '\0')
116+
if (position.X is >= 0 and < Constants.MapSize * Constants.TileSize && position.Y is >= Constants.TopOffset and < Constants.MapSize * Constants.TileSize + Constants.TopOffset && Mirror != '\0')
121117
{
122-
_mirrorPosition = (position.X / Constants.TileSize, (position.Y - Constants.TopOffset) / Constants.TileSize);
118+
MirrorPosition = (position.X / Constants.TileSize, (position.Y - Constants.TopOffset) / Constants.TileSize);
123119
}
124120
else
125121
{
126-
_mirrorPosition = (-1, -1);
122+
MirrorPosition = (-1, -1);
127123
}
128124
}
129125

@@ -235,7 +231,7 @@ private void DrawBackground(SpriteBatch spriteBatch)
235231
: Color.FromNonPremultiplied(255, 255, 255, 25), 0, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
236232
}
237233

238-
if (y > 0 && y <= 10)
234+
if (y is > 0 and <= 10)
239235
{
240236
spriteBatch.Draw(_other,
241237
new Vector2((Constants.MapSize + 1) * Constants.TileSize, Constants.TopOffset + y * Constants.TileSize),

AoC.Games/Games/Deflectors/Actors/BeamSimulator.cs

Lines changed: 42 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ public class BeamSimulator : IActor
3131

3232
private bool _hitUnplaced;
3333

34-
private int _beamStrength;
35-
3634
private long _frame;
3735

38-
public int BeamStrength => _beamStrength;
39-
36+
public int BeamStrength { get; private set; }
37+
4038
public State State { private get; set; }
4139

4240
public bool IsComplete { get; private set; }
@@ -87,7 +85,7 @@ private void SimulateBeams(SpriteBatch spriteBatch)
8785

8886
_hitMirrors.Clear();
8987

90-
_beamStrength = 0;
88+
BeamStrength = 0;
9189

9290
_hitUnplaced = false;
9391

@@ -141,9 +139,9 @@ private void SimulateBeam(SpriteBatch spriteBatch, Start start, int beamSteps, i
141139
var oldDx = dX;
142140
var oldDy = dY;
143141

144-
while (x >= 0 && x < Constants.MapSize * BeamFactor && y >= 0 && y < Constants.MapSize * BeamFactor)
142+
while (x is >= 0 and < Constants.MapSize * BeamFactor && y is >= 0 and < Constants.MapSize * BeamFactor)
145143
{
146-
_beamStrength++;
144+
BeamStrength++;
147145

148146
beamSteps++;
149147

@@ -168,18 +166,12 @@ private void SimulateBeam(SpriteBatch spriteBatch, Start start, int beamSteps, i
168166

169167
if (result.Mirror != '\0')
170168
{
171-
switch (result.Mirror)
169+
(dX, dY) = result.Mirror switch
172170
{
173-
case '\\':
174-
(dX, dY) = (dY, dX);
175-
176-
break;
177-
178-
case '/':
179-
(dX, dY) = (-dY, -dX);
180-
181-
break;
182-
}
171+
'\\' => (dY, dX),
172+
'/' => (-dY, -dX),
173+
_ => (dX, dY)
174+
};
183175
}
184176
}
185177

@@ -226,32 +218,30 @@ private void SimulateBeam(SpriteBatch spriteBatch, Start start, int beamSteps, i
226218
}
227219
}
228220

229-
if (mirror == '|' || mirror == '-')
221+
if (mirror is '|' or '-')
230222
{
231-
if (mirror == '|' && dX != 0)
223+
switch (mirror)
232224
{
233-
_splitters.Enqueue((x / BeamFactor, y / BeamFactor, Direction.North, beamSteps, colorIndex, colorDirection));
234-
_splitters.Enqueue((x / BeamFactor, y / BeamFactor, Direction.South, beamSteps, colorIndex, colorDirection));
235-
236-
spriteBatch.Draw(_beams,
237-
new Vector2(x * BeamSize, Constants.TopOffset + y * BeamSize),
238-
new Rectangle(dX == 1 ? 3 * BeamSize : 2 * BeamSize, 0, 7, 7), _palette[colorIndex],
239-
0, Vector2.Zero, Vector2.One, SpriteEffects.None, .2f);
240-
241-
return (mirror, true);
242-
}
243-
244-
if (mirror == '-' && dY != 0)
245-
{
246-
_splitters.Enqueue((x / BeamFactor, y / BeamFactor, Direction.East, beamSteps, colorIndex, colorDirection));
247-
_splitters.Enqueue((x / BeamFactor, y / BeamFactor, Direction.West, beamSteps, colorIndex, colorDirection));
248-
249-
spriteBatch.Draw(_beams,
250-
new Vector2(x * BeamSize, Constants.TopOffset + y * BeamSize),
251-
new Rectangle(dY == 1 ? 2 * BeamSize : 3 * BeamSize, 0, 7, 7), _palette[colorIndex],
252-
0, Vector2.Zero, Vector2.One, SpriteEffects.None, .2f);
253-
254-
return (mirror, true);
225+
case '|' when dX != 0:
226+
_splitters.Enqueue((x / BeamFactor, y / BeamFactor, Direction.North, beamSteps, colorIndex, colorDirection));
227+
_splitters.Enqueue((x / BeamFactor, y / BeamFactor, Direction.South, beamSteps, colorIndex, colorDirection));
228+
229+
spriteBatch.Draw(_beams,
230+
new Vector2(x * BeamSize, Constants.TopOffset + y * BeamSize),
231+
new Rectangle(dX == 1 ? 3 * BeamSize : 2 * BeamSize, 0, 7, 7), _palette[colorIndex],
232+
0, Vector2.Zero, Vector2.One, SpriteEffects.None, .2f);
233+
234+
return (mirror, true);
235+
case '-' when dY != 0:
236+
_splitters.Enqueue((x / BeamFactor, y / BeamFactor, Direction.East, beamSteps, colorIndex, colorDirection));
237+
_splitters.Enqueue((x / BeamFactor, y / BeamFactor, Direction.West, beamSteps, colorIndex, colorDirection));
238+
239+
spriteBatch.Draw(_beams,
240+
new Vector2(x * BeamSize, Constants.TopOffset + y * BeamSize),
241+
new Rectangle(dY == 1 ? 2 * BeamSize : 3 * BeamSize, 0, 7, 7), _palette[colorIndex],
242+
0, Vector2.Zero, Vector2.One, SpriteEffects.None, .2f);
243+
244+
return (mirror, true);
255245
}
256246
}
257247

@@ -317,30 +307,17 @@ private void DrawBeam(SpriteBatch spriteBatch, int x, int y, int dX, int dY, int
317307
}
318308
else
319309
{
320-
switch (oldDx)
310+
beam = oldDx switch
321311
{
322-
case 1:
323-
beam = dY == 1 ? 3 : 5;
324-
break;
325-
326-
case -1:
327-
beam = dY == 1 ? 4 : 2;
328-
break;
329-
330-
default:
331-
switch (oldDy)
332-
{
333-
case 1:
334-
beam = dX == 1 ? 2 : 5;
335-
break;
336-
337-
case -1:
338-
beam = dX == 1 ? 4 : 3;
339-
break;
340-
}
341-
342-
break;
343-
}
312+
1 => dY == 1 ? 3 : 5,
313+
-1 => dY == 1 ? 4 : 2,
314+
_ => oldDy switch
315+
{
316+
1 => dX == 1 ? 2 : 5,
317+
-1 => dX == 1 ? 4 : 3,
318+
_ => beam
319+
}
320+
};
344321
}
345322

346323
spriteBatch.Draw(_beams,

0 commit comments

Comments
 (0)