Skip to content

Commit

Permalink
Some code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kubagdynia committed Oct 25, 2023
1 parent 9e8ed16 commit c62ce33
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 73 deletions.
6 changes: 3 additions & 3 deletions Spacetris/BackgroundEffects/Starfield.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class Starfield : Transformable, Drawable
private readonly uint _maxMediumStars;
private readonly uint _maxLargeStars;

private readonly List<Point2> _smallStars = new List<Point2>();
private readonly List<Point2> _mediumStars = new List<Point2>();
private readonly List<Point2> _largeStars = new List<Point2>();
private readonly List<Point2> _smallStars = new();
private readonly List<Point2> _mediumStars = new();
private readonly List<Point2> _largeStars = new();

private readonly Random _randomX;

Expand Down
2 changes: 1 addition & 1 deletion Spacetris/DataStructures/Point2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public struct Point2 : IEquatable<Point2>
/// <summary>
/// A point at the origin (0, 0)
/// </summary>
public static readonly Point2 Zero = new Point2(0, 0);
public static readonly Point2 Zero = new(0, 0);

/// <summary>
/// The X component of the Point
Expand Down
2 changes: 1 addition & 1 deletion Spacetris/Extensions/SfmlGraphicsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static Vector2f Center(this Text text, float x, float y)

public static Text Shadow(this Text text, int offset = 2, bool center = true)
{
Text shadowText = new Text()
Text shadowText = new Text
{
DisplayedString = text.DisplayedString,
Font = text.Font,
Expand Down
2 changes: 1 addition & 1 deletion Spacetris/GameStates/BaseGameState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected static void DrawText(RenderTarget target, Font font, string value, flo
return;
}

var text = new Text()
var text = new Text
{
DisplayedString = value,
Font = font,
Expand Down
10 changes: 4 additions & 6 deletions Spacetris/GameStates/Menu/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,25 +335,23 @@ public bool EnableMenuItem(MenuItemType menuItem, bool enable, bool select = tru
}
else
{
_selectedMenuItem = _menuItems.Where(c => c.Enable).OrderBy(c => c.Position).FirstOrDefault();
_selectedMenuItem = _menuItems.Where(c => c.Enable).MinBy(c => c.Position);
}

return true;
}

public void KeyPressed(RenderWindow target, object sender, KeyEventArgs e)
{
if (e.Code == Keyboard.Key.Down || e.Code == Keyboard.Key.S ||
e.Code == Keyboard.Key.Up || e.Code == Keyboard.Key.W ||
e.Code == Keyboard.Key.Escape)
if (e.Code is Keyboard.Key.Down or Keyboard.Key.S or Keyboard.Key.Up or Keyboard.Key.W or Keyboard.Key.Escape)
{
MenuItem nextSelectedMenuItem = _selectedMenuItem;

if (e.Code == Keyboard.Key.Down || e.Code == Keyboard.Key.S)
if (e.Code is Keyboard.Key.Down or Keyboard.Key.S)
{
nextSelectedMenuItem = GetMenuItems().FirstOrDefault(c => c.Enable && c.FunctionType != MenuItemFunctionType.CustomPage && c.Position > _selectedMenuItem.Position);
}
else if (e.Code == Keyboard.Key.Up || e.Code == Keyboard.Key.W)
else if (e.Code is Keyboard.Key.Up or Keyboard.Key.W)
{
nextSelectedMenuItem = GetMenuItems().OrderByDescending(c => c.Position).FirstOrDefault(c => c.Enable && c.FunctionType != MenuItemFunctionType.CustomPage && c.Position < _selectedMenuItem.Position);
}
Expand Down
20 changes: 10 additions & 10 deletions Spacetris/GameStates/Worlds/BaseWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public abstract class BaseWorld : BaseGameState, IWorld
private Timer _timer;
private int _tickTimer;

public Sprite GameControllerSprite;
protected Sprite GameControllerSprite;

public Sound GameSoundMoveTetromino;
public Sound GameSoundDropTetromino;
public Sound GameSoundRemoveLine;
public Sound GameSoundGameOver;
public Sound GameSoundLevelUp;
protected Sound GameSoundMoveTetromino;
protected Sound GameSoundDropTetromino;
protected Sound GameSoundRemoveLine;
protected Sound GameSoundGameOver;
protected Sound GameSoundLevelUp;

private WorldState _worldState;
public WorldState WorldState
Expand Down Expand Up @@ -72,8 +72,6 @@ public WorldState WorldState

private bool _readyForRotate = true;

private Point2 _offsetMove = Point2.Zero;

private Sprite _blockSprite;
public Sprite BlockSprite
{
Expand Down Expand Up @@ -136,7 +134,9 @@ public virtual void Update(RenderWindow target, float deltaTime)
public virtual void UpdateDrawOffset(RenderWindow target, int offsetX = 0, int offsetY = 0)
{
DrawOffset = new Point2(
// ReSharper disable once PossibleLossOfFraction
(target.Size.X - BackgroundSprite.Texture.Size.X) / 2 + offsetX,
// ReSharper disable once PossibleLossOfFraction
(target.Size.Y - BackgroundSprite.Texture.Size.Y) / 2 + offsetY);

BackgroundSprite.Position = new Vector2f(DrawOffset.X, DrawOffset.Y);
Expand Down Expand Up @@ -814,9 +814,9 @@ public virtual void JoystickMoved(RenderWindow target, object sender, JoystickMo
#if DEBUG
$"Controller ({arg.JoystickId}) Moved: Axis({arg.Axis}), Position({arg.Position})".Log();
#endif
if (arg.Axis == Joystick.Axis.PovX || arg.Axis == Joystick.Axis.PovY)
if (arg.Axis is Joystick.Axis.PovX or Joystick.Axis.PovY)
{
if (WorldState == WorldState.NewGame || WorldState == WorldState.Continue)
if (WorldState is WorldState.NewGame or WorldState.Continue)
{
WorldState = WorldState.Playing;
}
Expand Down
100 changes: 50 additions & 50 deletions Spacetris/GameStates/Worlds/World01.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,73 +10,73 @@ public class World01 : BaseWorld
{
private Starfield _starfield;

public override int WorldColumns => 10;
public override int WorldRows => 20;
public override int WorldColumns => 10;
public override int WorldRows => 20;

public override int SpriteBlockSize => 20; // Size in pixels
public override int SpriteBlockSize => 20; // Size in pixels

public override Point2 WorldDrawOffset => new Point2(20, 20);
public override Point2 WorldDrawOffset => new(20, 20);

public override Point2 NextTetrominoPreviewPosition => new Point2(229, 20);
public override Point2 NextTetrominoPreviewPosition => new(229, 20);

protected override string BackgroundTexturePath => Path.Combine(GameSettings.BackgroundPath, "background.png");
protected override string BackgroundTexturePath => Path.Combine(GameSettings.BackgroundPath, "background.png");

protected override string BlocksTexturePath => Path.Combine(GameSettings.TilesetsPath, "tilesets.png");
protected override string BlocksTexturePath => Path.Combine(GameSettings.TilesetsPath, "tilesets.png");

protected override AssetManagerItemName FontName => AssetManagerItemName.ArialFont;
protected override AssetManagerItemName FontName => AssetManagerItemName.ArialFont;

protected override AssetManagerItemName CounterFontName => AssetManagerItemName.TetrisFont;
protected override AssetManagerItemName CounterFontName => AssetManagerItemName.TetrisFont;

public override void DrawScore(RenderWindow target, int x, int y, byte alpha = 255)
{
DrawInfo(target, GameState.Score.ToString(), x + DrawOffset.X, y + DrawOffset.Y, alpha);
}
public override void DrawScore(RenderWindow target, int x, int y, byte alpha = 255)
{
DrawInfo(target, GameState.Score.ToString(), x + DrawOffset.X, y + DrawOffset.Y, alpha);
}

public override void DrawLevel(RenderWindow target, int x, int y, byte alpha = 255)
{
DrawInfo(target, GameState.Level.ToString(), x + DrawOffset.X, y + DrawOffset.Y, alpha);
}
public override void DrawLevel(RenderWindow target, int x, int y, byte alpha = 255)
{
DrawInfo(target, GameState.Level.ToString(), x + DrawOffset.X, y + DrawOffset.Y, alpha);
}

public override void DrawLines(RenderWindow target, int x, int y, byte alpha = 255)
{
DrawInfo(target, GameState.Lines.ToString(), x + DrawOffset.X, y + DrawOffset.Y, alpha);
}
public override void DrawLines(RenderWindow target, int x, int y, byte alpha = 255)
{
DrawInfo(target, GameState.Lines.ToString(), x + DrawOffset.X, y + DrawOffset.Y, alpha);
}

public override void Initialize(RenderWindow target)
{
base.Initialize(target);
public override void Initialize(RenderWindow target)
{
base.Initialize(target);

_starfield = new Starfield(target.Size.X, target.Size.Y);
}
_starfield = new Starfield(target.Size.X, target.Size.Y);
}

public override void Update(RenderWindow target, float deltaTime)
{
base.Update(target, deltaTime);
public override void Update(RenderWindow target, float deltaTime)
{
base.Update(target, deltaTime);

_starfield.UpdateStarfield(deltaTime);
}
_starfield.UpdateStarfield(deltaTime);
}

public override void DrawBackground(RenderWindow target, byte alpha = 255)
{
target.Draw(_starfield);
public override void DrawBackground(RenderWindow target, byte alpha = 255)
{
target.Draw(_starfield);

base.DrawBackground(target, alpha);
}
base.DrawBackground(target, alpha);
}

private void DrawInfo(RenderWindow target, string value, int x, int y, byte alpha = 189)
{
DrawText(target, Font, value, x, y, new Color(242, 51, 51, alpha), 20, true, true);
}
private void DrawInfo(RenderWindow target, string value, int x, int y, byte alpha = 189)
{
DrawText(target, Font, value, x, y, new Color(242, 51, 51, alpha), 20, true, true);
}

protected override void LoadContent()
{
GameSoundMoveTetromino = LoadSound("move.ogg");
GameSoundDropTetromino = LoadSound("drop.ogg");
GameSoundRemoveLine = LoadSound("removeline.ogg");
GameSoundGameOver = LoadSound("gameover.ogg");
GameSoundLevelUp = LoadSound("levelup.ogg");
protected override void LoadContent()
{
GameSoundMoveTetromino = LoadSound("move.ogg");
GameSoundDropTetromino = LoadSound("drop.ogg");
GameSoundRemoveLine = LoadSound("removeline.ogg");
GameSoundGameOver = LoadSound("gameover.ogg");
GameSoundLevelUp = LoadSound("levelup.ogg");

// Load sprites
GameControllerSprite = LoadGameControllerSprite();
}
// Load sprites
GameControllerSprite = LoadGameControllerSprite();
}
}
2 changes: 1 addition & 1 deletion Spacetris/Settings/GameSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static string ImagesPath
}
}

private static Settings _settings = new Settings();
private static Settings _settings = new();

public static void Save()
{
Expand Down

0 comments on commit c62ce33

Please sign in to comment.