Skip to content

Commit

Permalink
Small opts and tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkSuckerberg committed Oct 24, 2023
1 parent 8a2a31d commit 4778f86
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
14 changes: 3 additions & 11 deletions Blocktest/Code/Block System/BlockSprites.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,8 @@ public sealed class BlockSprites {

public BlockSprites(BlockShared newBlockShared) {
_blockShared = newBlockShared;
LoadSprite();
}

/// <summary>
/// Called when the block is created by the block sprites manager.
/// </summary>
/// <remarks>
/// DO NOT FORGET TO CALL THE BASE METHOD IF YOU OVERRIDE THIS.
/// </remarks>
public void LoadSprite() {
SpriteSheet = SpriteSheet.ErrorSpriteSheet;

string path = @"Graphics\Blocks\" + _blockShared.BlockName.ToLower().Replace(" ", "");
try {
BlockSprite =
Expand All @@ -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}!");
}
}
Expand Down
2 changes: 1 addition & 1 deletion Blocktest/Code/Block System/BlockSpritesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Blocktest.Block_System;

public sealed class BlockSpritesManager {
/// <summary> Array which stores all blocksprites instances for referencing as if they were globals. </summary>
/// <summary> Array which stores all block sprites instances for referencing as if they were globals. </summary>
public static Dictionary<string, BlockSprites> AllBlocksSprites { get; private set; }

public static void LoadBlockSprites() {
Expand Down
21 changes: 15 additions & 6 deletions Blocktest/Code/Block System/RenderableTile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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];
}

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions Blocktest/Code/Rendering/Drawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
2 changes: 2 additions & 0 deletions Blocktest/Code/Rendering/SpriteSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
12 changes: 6 additions & 6 deletions Shared/Code/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,22 @@ public static explicit operator Vector2Int(Vector2 vector2) =>

// Preset values
/// <summary>Returns a <see cref="Vector2Int" /> with values (0, 0).</summary>
public static readonly Vector2Int Zero = new(0, 0);
public static Vector2Int Zero { get; } = new(0, 0);

/// <summary>Returns a <see cref="Vector2Int" /> with values (1, 1).</summary>
public static readonly Vector2Int One = new(1, 1);
public static Vector2Int One { get; } = new(1, 1);

/// <summary>Returns a <see cref="Vector2Int" /> with values (0, 1).</summary>
public static readonly Vector2Int Up = new(0, 1);
public static Vector2Int Up { get; } = new(0, 1);

/// <summary>Returns a <see cref="Vector2Int" /> with values (0, -1).</summary>
public static readonly Vector2Int Down = new(0, -1);
public static Vector2Int Down { get; } = new(0, -1);

/// <summary>Returns a <see cref="Vector2Int" /> with values (-1, 0).</summary>
public static readonly Vector2Int Left = new(-1, 0);
public static Vector2Int Left { get; } = new(-1, 0);

/// <summary>Returns a <see cref="Vector2Int" /> with values (1, 0).</summary>
public static readonly Vector2Int Right = new(1, 0);
public static Vector2Int Right { get; } = new(1, 0);

public void Serialize(NetDataWriter writer) {
writer.Put(X);
Expand Down

0 comments on commit 4778f86

Please sign in to comment.