From 4778f86b8c27b3f05bec680c9ffdbe22a1dfdd9d Mon Sep 17 00:00:00 2001 From: Mark Suckerberg Date: Tue, 24 Oct 2023 18:09:23 -0500 Subject: [PATCH] Small opts and tweaks --- Blocktest/Code/Block System/BlockSprites.cs | 14 +++---------- .../Code/Block System/BlockSpritesManager.cs | 2 +- Blocktest/Code/Block System/RenderableTile.cs | 21 +++++++++++++------ Blocktest/Code/Rendering/Drawable.cs | 2 ++ Blocktest/Code/Rendering/SpriteSheet.cs | 2 ++ Shared/Code/Math.cs | 12 +++++------ 6 files changed, 29 insertions(+), 24 deletions(-) diff --git a/Blocktest/Code/Block System/BlockSprites.cs b/Blocktest/Code/Block System/BlockSprites.cs index 45c167d..868fafe 100644 --- a/Blocktest/Code/Block System/BlockSprites.cs +++ b/Blocktest/Code/Block System/BlockSprites.cs @@ -19,16 +19,8 @@ public sealed class BlockSprites { public BlockSprites(BlockShared newBlockShared) { _blockShared = newBlockShared; - LoadSprite(); - } - - /// - /// Called when the block is created by the block sprites manager. - /// - /// - /// DO NOT FORGET TO CALL THE BASE METHOD IF YOU OVERRIDE THIS. - /// - public void LoadSprite() { + SpriteSheet = SpriteSheet.ErrorSpriteSheet; + string path = @"Graphics\Blocks\" + _blockShared.BlockName.ToLower().Replace(" ", ""); try { BlockSprite = @@ -45,7 +37,7 @@ public void LoadSprite() { } } catch (ContentLoadException) { - BlockSprite = new Drawable(@"Graphics\Blocks\error"); + BlockSprite = Drawable.ErrorDrawable; Console.WriteLine($"Block {this} does not have a sprite at {path}!"); } } diff --git a/Blocktest/Code/Block System/BlockSpritesManager.cs b/Blocktest/Code/Block System/BlockSpritesManager.cs index 37e1436..15c94a1 100644 --- a/Blocktest/Code/Block System/BlockSpritesManager.cs +++ b/Blocktest/Code/Block System/BlockSpritesManager.cs @@ -3,7 +3,7 @@ namespace Blocktest.Block_System; public sealed class BlockSpritesManager { - /// Array which stores all blocksprites instances for referencing as if they were globals. + /// Array which stores all block sprites instances for referencing as if they were globals. public static Dictionary AllBlocksSprites { get; private set; } public static void LoadBlockSprites() { diff --git a/Blocktest/Code/Block System/RenderableTile.cs b/Blocktest/Code/Block System/RenderableTile.cs index 7094ecb..f7e14f2 100644 --- a/Blocktest/Code/Block System/RenderableTile.cs +++ b/Blocktest/Code/Block System/RenderableTile.cs @@ -5,6 +5,15 @@ namespace Blocktest.Block_System; public class RenderableTile : TileShared { public readonly Renderable Renderable; + + [Flags] + private enum DirectionalBitmask { + None = 0, + Up = 1, + Down = 2, + Right = 4, + Left = 8 + } public RenderableTile(TileShared tile, bool background) : base(tile.SourceBlock, tile.Transform.Position / GlobalsShared.GridSize) { @@ -23,23 +32,23 @@ public void UpdateAdjacencies(Vector2Int position, TilemapShared tilemap) { return; } // If the tile doesn't smooth, don't even try - int bitmask = 0; // Uses bitmask smoothing, look it up + var dirBitmask = DirectionalBitmask.None; if (HasSmoothableTile(position + Vector2Int.Up, tilemap)) { - bitmask += 1; + dirBitmask |= DirectionalBitmask.Up; } if (HasSmoothableTile(position + Vector2Int.Down, tilemap)) { - bitmask += 2; + dirBitmask |= DirectionalBitmask.Down; } if (HasSmoothableTile(position + Vector2Int.Right, tilemap)) { - bitmask += 4; + dirBitmask |= DirectionalBitmask.Right; } if (HasSmoothableTile(position + Vector2Int.Left, tilemap)) { - bitmask += 8; + dirBitmask |= DirectionalBitmask.Left; } Renderable.Appearance = BlockSpritesManager.AllBlocksSprites[SourceBlock.BlockUid].SpriteSheet - .OrderedSprites[bitmask]; + .OrderedSprites[(int) dirBitmask]; } /// diff --git a/Blocktest/Code/Rendering/Drawable.cs b/Blocktest/Code/Rendering/Drawable.cs index ca10608..9c1bccc 100644 --- a/Blocktest/Code/Rendering/Drawable.cs +++ b/Blocktest/Code/Rendering/Drawable.cs @@ -10,4 +10,6 @@ public Drawable(string fileName, Rectangle? bounds = null) { throw new Exception($"Could not load drawable {fileName}, content manager not initialized."); Bounds = bounds ?? Texture.Bounds; } + + public static Drawable ErrorDrawable { get; } = new(@"Graphics\Blocks\error"); } \ No newline at end of file diff --git a/Blocktest/Code/Rendering/SpriteSheet.cs b/Blocktest/Code/Rendering/SpriteSheet.cs index 0f86a49..5177e7c 100644 --- a/Blocktest/Code/Rendering/SpriteSheet.cs +++ b/Blocktest/Code/Rendering/SpriteSheet.cs @@ -22,4 +22,6 @@ public SpriteSheet(string filename, int frameColumns, int frameRows, int padding OrderedSprites[frameColumn + frameRow * frameColumns] = newDrawable; } } + + public static SpriteSheet ErrorSpriteSheet { get; } = new(@"Graphics\Blocks\error", 1, 1); } \ No newline at end of file diff --git a/Shared/Code/Math.cs b/Shared/Code/Math.cs index 4dfecd9..9c5e149 100644 --- a/Shared/Code/Math.cs +++ b/Shared/Code/Math.cs @@ -89,22 +89,22 @@ public static explicit operator Vector2Int(Vector2 vector2) => // Preset values /// Returns a with values (0, 0). - public static readonly Vector2Int Zero = new(0, 0); + public static Vector2Int Zero { get; } = new(0, 0); /// Returns a with values (1, 1). - public static readonly Vector2Int One = new(1, 1); + public static Vector2Int One { get; } = new(1, 1); /// Returns a with values (0, 1). - public static readonly Vector2Int Up = new(0, 1); + public static Vector2Int Up { get; } = new(0, 1); /// Returns a with values (0, -1). - public static readonly Vector2Int Down = new(0, -1); + public static Vector2Int Down { get; } = new(0, -1); /// Returns a with values (-1, 0). - public static readonly Vector2Int Left = new(-1, 0); + public static Vector2Int Left { get; } = new(-1, 0); /// Returns a with values (1, 0). - public static readonly Vector2Int Right = new(1, 0); + public static Vector2Int Right { get; } = new(1, 0); public void Serialize(NetDataWriter writer) { writer.Put(X);