Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkSuckerberg committed Oct 6, 2023
1 parent 947161b commit af23326
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 70 deletions.
1 change: 1 addition & 0 deletions .idea/.idea.Blocktest/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Blocktest/BlocktestGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected override void Initialize() {
/// <inheritdoc />
protected override void LoadContent() {
ContentManager = Content;
BlockSpritesManager.LoadBlockSprites(Content);
BlockSpritesManager.LoadBlockSprites();
_currentScene = new GameScene(this, _connect, _ip);
}

Expand Down
10 changes: 5 additions & 5 deletions Blocktest/Code/Block System/BlockSprites.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public sealed class BlockSprites {

/* METHODS */

public BlockSprites(BlockShared newBlockShared, ContentManager content) {
public BlockSprites(BlockShared newBlockShared) {
BlockShared = newBlockShared;
LoadSprite(content);
LoadSprite();
}

/// <summary>
Expand All @@ -28,16 +28,16 @@ public BlockSprites(BlockShared newBlockShared, ContentManager content) {
/// <remarks>
/// DO NOT FORGET TO CALL THE BASE METHOD IF YOU OVERRIDE THIS.
/// </remarks>
public void LoadSprite(ContentManager content) {
public void LoadSprite() {
string path = @"Graphics\Blocks\" + BlockShared.BlockName.ToLower().Replace(" ", "");
try {
BlockSprite =
new Drawable(path,
new Rectangle(1, 1, 10,
10)); //this might need to be expanded in the future in case we decide to make use of the full 12x12 tiles on our spritesheets
/*if (!blockShared.blockSmoothing) {
if (!BlockShared.BlockSmoothing) {
return;
}*/
}
SpriteSheet = new SpriteSheet(path, 4, 4, 1);
if (SpriteSheet.OrderedSprites.Length <= 1) {
Console.WriteLine("Block " +
Expand Down
7 changes: 3 additions & 4 deletions Blocktest/Code/Block System/BlockSpritesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ public static string[] BlockSpriteNames {
private set => _blockSpriteNames = value;
}

public static void LoadBlockSprites(ContentManager content) {
public static void LoadBlockSprites() {
AllBlocksSprites = new BlockSprites[BlockManagerShared.AllBlocks.Length];
BlockSpriteNames = new string[BlockManagerShared.AllBlocks.Length];

for (int i = 0; i < BlockManagerShared.AllBlocks.Length; i++) {
BlockShared block = BlockManagerShared.AllBlocks[i];
BlockSprites newBlockSprites = new(block, content);
foreach (BlockShared block in BlockManagerShared.AllBlocks) {
BlockSprites newBlockSprites = new(block);
BlockSpriteNames[block.BlockId] = block.BlockName;
AllBlocksSprites[block.BlockId] = newBlockSprites;
}
Expand Down
9 changes: 7 additions & 2 deletions Blocktest/Code/Block System/TilemapSprites.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Blocktest.Rendering;
using Shared.Code;
using Shared.Code.Block_System;
namespace Blocktest.Block_System;

Expand Down Expand Up @@ -28,9 +29,13 @@ public TilemapSprites(TilemapShared newTilemap) {
public void DrawAllTiles(SpriteBatch spriteBatch) {
for (int x = 0; x < Tilemap.TilemapSize.X; x++) {
for (int y = 0; y < Tilemap.TilemapSize.Y; y++) {
TileShared? tile = Tilemap.TileGrid[x, y];
if (!Tilemap.TryGetTile(new Vector2Int(x, y), out TileShared? tile)) {
continue;
}
BlockSprites blockSprites = BlockSpritesManager.AllBlocksSprites[tile.SourceBlock.BlockId];
Drawable sprite = blockSprites.SpriteSheet.OrderedSprites[tile.Bitmask];
Drawable sprite = tile.SourceBlock.BlockSmoothing
? blockSprites.SpriteSheet.OrderedSprites[tile.Bitmask]
: blockSprites.BlockSprite;
spriteBatch.Draw(sprite.Texture, new Vector2(tile.Rectangle.X, tile.Rectangle.Y), sprite.Bounds,
tile.Color);
}
Expand Down
18 changes: 9 additions & 9 deletions Blocktest/Code/Networking/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ namespace Blocktest.Networking;
public sealed class Client {
private readonly EventBasedNetListener _listener;
private readonly NetManager _manager;
private NetPeer? _server;
public NetPeer? Server { get; private set; }
public TickBuffer ClientTickBuffer = new(0);

public Client() {
_listener = new EventBasedNetListener();
_manager = new NetManager(_listener);
_listener.NetworkReceiveEvent += NetworkRecieveEvent;
_listener.NetworkReceiveEvent += NetworkReceiveEvent;
_manager.Start();
}

Expand All @@ -36,10 +36,10 @@ public void Update() {
/// <param name="packetReader">Contains the packet from the server.</param>
/// <param name="channelNumber"></param>
/// <param name="deliveryMethod">The delivery method used to deliver this packet.</param>
protected void NetworkRecieveEvent(NetPeer peer, NetPacketReader packetReader, byte channelNumber,
DeliveryMethod deliveryMethod) {
if (_server == null) {
_server = peer;
private void NetworkReceiveEvent(NetPeer peer, NetPacketReader packetReader, byte channelNumber,
DeliveryMethod deliveryMethod) {
if (Server == null) {
Server = peer;
WorldDownload worldPacket = new();
byte packetByte = packetReader.GetByte();
PacketType packetType = (PacketType)packetByte;
Expand All @@ -58,7 +58,7 @@ protected void NetworkRecieveEvent(NetPeer peer, NetPacketReader packetReader, b
/// Handles packets after the first.
/// </summary>
/// <param name="packetReader">Contains the packet sent by the server.</param>
public void HandlePackets(NetPacketReader packetReader) {
private void HandlePackets(NetPacketReader packetReader) {
byte packetByte = packetReader.GetByte();
PacketType packetType = (PacketType)packetByte;
switch (packetType) {
Expand Down Expand Up @@ -90,13 +90,13 @@ public void SendTileChange(TileChange tileChange) {
NetDataWriter writer = new();
writer.Put((byte)PacketType.TileChange);
writer.Put(tileChange);
_server?.Send(writer, DeliveryMethod.ReliableUnordered);
Server?.Send(writer, DeliveryMethod.ReliableUnordered);
}

public void SendBreakTile(BreakTile breakTile) {
NetDataWriter writer = new();
writer.Put((byte)PacketType.BreakTile);
writer.Put(breakTile);
_server?.Send(writer, DeliveryMethod.ReliableUnordered);
Server?.Send(writer, DeliveryMethod.ReliableUnordered);
}
}
5 changes: 5 additions & 0 deletions Blocktest/Code/Scenes/GameScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public void Draw(GameTime gameTime, GraphicsDevice graphicsDevice) {
string fps = $"FPS: {_frameCounter.CurrentFramesPerSecond:##0.00}";
_spriteBatch.DrawString(_spriteFont, fps, new Vector2(10, 10), Color.Black);

if (_connect) {
string ping = $"Ping: {_networkingClient.Server?.Ping}ms";
_spriteBatch.DrawString(_spriteFont, ping, new Vector2(10, 30), Color.Black);
}

if (_buildMode) {
_spriteBatch.Draw(BlockSpritesManager.AllBlocksSprites[_blockSelected].BlockSprite.Texture,
new Vector2Int(Mouse.GetState().X - Mouse.GetState().X % 8,
Expand Down
7 changes: 6 additions & 1 deletion Shared/Code/Block System/BlockManagerShared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static BlockShared[] AllBlocks {
get => _allBlocks;
private set => _allBlocks = value;
}

/// <summary> List used to store the names of blocks. The indexes are the corresponding block's ID. </summary>
public static string[] BlockNames {
get => _blockNames;
Expand All @@ -43,7 +44,11 @@ where assemblyType.IsSubclassOf(typeof(BlockShared))
// For loops to populate main allBlocks array.
for (int i = 0; i < allBlockTypes.Length; i++) {
Type newBlockType = allBlockTypes[i];
BlockShared newBlock = (BlockShared)Activator.CreateInstance(newBlockType);
BlockShared? newBlock = (BlockShared?)Activator.CreateInstance(newBlockType);
if (newBlock == null) {
Console.WriteLine($"Failed to create instance of {newBlockType}!");
continue;
}
newBlock.Initialize();
if (newBlock.BlockId == -1) {
newBlock.BlockId = i;
Expand Down
54 changes: 12 additions & 42 deletions Shared/Code/Block System/TilemapShared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public sealed class TilemapShared {
/// A list of <see cref="Vector2Int" />s that specify which blocks should be refreshed when a tile is placed/destroyed.
/// Defaults to the changed block and all cardinal directions.
/// </summary>
private readonly List<Vector2Int> _adjacencies = new()
private static readonly List<Vector2Int> Adjacencies = new()
{ Vector2Int.Zero, Vector2Int.Up, Vector2Int.Down, Vector2Int.Left, Vector2Int.Right };

/// <summary>
Expand Down Expand Up @@ -59,11 +59,9 @@ public TileShared SetBlock(Vector2Int location, BlockShared newBlock) =>
/// <param name="location">Location the new Block will be placed.</param>
/// <param name="newTile">Block type to be placed in the cell.</param>
public TileShared SetTile(Vector2Int location, TileShared newTile) {
TileShared oldTile = GetTile(location);

TileGrid[location.X, location.Y] = newTile;

foreach (Vector2Int dir in _adjacencies) {
foreach (Vector2Int dir in Adjacencies) {
if (location.X + dir.X < 0 ||
location.X + dir.X >= TilemapSize.X ||
location.Y + dir.Y < 0 ||
Expand All @@ -77,37 +75,10 @@ public TileShared SetTile(Vector2Int location, TileShared newTile) {
}

/// <summary>
/// Deletes a <see cref="TileShared" /> at a specific location(sets value to null).
/// Deletes a <see cref="TileShared" /> at a specific location (sets block to air).
/// </summary>
/// <param name="location"></param>
public void DeleteTile(Vector2Int location) => SetTile(location, null);

/// <summary>
/// Gets the <see cref="TileShared" /> at a specific location on a <see cref="Tilemap" />.
/// </summary>
/// <param name="location">Location of the Tile on the Tilemap to check.</param>
/// <returns><see cref="TileShared" /> placed at the cell.</returns>
public TileShared? GetTile(Vector2Int location) => GetTile(location.X, location.Y);

/// <summary>
/// Gets the <see cref="TileShared" /> at a specific location on a <see cref="Tilemap" />.
/// </summary>
/// <param name="x">X position of the Tile on the Tilemap to check.</param>
/// <param name="y">Y position of the Tile on the Tilemap to check.</param>
/// <returns><see cref="TileShared" /> placed at the cell.</returns>
public TileShared? GetTile(int x, int y) {
if (x < 0 || y < 0 || x >= TilemapSize.X || y >= TilemapSize.Y) {
return null;
}
return TileGrid[x, y];
}

/// <summary>
/// Returns whether there is a <see cref="TileShared" /> at the location specified.
/// </summary>
/// <param name="location">Location to check.</param>
/// <returns>Returns true if there is a Tile at the position. Returns false otherwise.</returns>
public bool HasTile(Vector2Int location) => TileGrid[location.X, location.Y] != null;
public void DeleteTile(Vector2Int location) => SetBlock(location, BlockManagerShared.AllBlocks[0]);

public bool TryGetTile<T>(Vector2Int location, [NotNullWhen(true)] out T? result) where T : TileShared {
result = null;
Expand Down Expand Up @@ -158,7 +129,7 @@ public class TileShared {
public TileShared(BlockShared newBlock, Vector2Int position) {
SourceBlock = newBlock;
Rectangle = new Rectangle(GlobalsShared.GridSize.X * position.X, GlobalsShared.GridSize.Y * position.Y, Size,
Size); // HACK: This can probably be done better
Size); // TODO: This can probably be done better
}

/// <summary>
Expand All @@ -172,7 +143,7 @@ public void UpdateAdjacencies(Vector2Int position, TilemapShared tilemap) {
return;
} // If the tile doesn't smooth, don't even try

Bitmask = 0; // Using bitmask smoothing, look it up
Bitmask = 0; // Uses bitmask smoothing, look it up

if (HasSmoothableTile(position + Vector2Int.Up, tilemap)) {
Bitmask += 2;
Expand All @@ -195,19 +166,18 @@ public void UpdateAdjacencies(Vector2Int position, TilemapShared tilemap) {
/// <param name="tilemap">The tilemap on which the tile you want to check for smoothing is.</param>
/// <returns>Whether or not the tile can smooth with this tile.</returns>
private bool HasSmoothableTile(Vector2Int position, TilemapShared tilemap) {
TileShared otherTile = tilemap.GetTile(position);
if (SourceBlock.SmoothSelf) {
return IsSameTileType(otherTile);
if (tilemap.TryGetTile(position, out TileShared? tile)) {
return SourceBlock.SmoothSelf
? IsSameTileType(tile)
: tile.SourceBlock.BlockId != 0; // Don't smooth with air, possibly find nicer way to do this later.
}
return
otherTile != null &&
otherTile.SourceBlock.BlockId != 0; // Don't smooth with air, possibly find nicer way to do this later.
return false;
}

/// <summary>
/// If the tile provided is the same type (references the same block) as the current tile.
/// </summary>
/// <param name="otherTile">The other tile to check.</param>
/// <returns>Whether or not the other block is the same type as the current tile</returns>
private bool IsSameTileType(TileShared otherTile) => otherTile?.SourceBlock == SourceBlock;
private bool IsSameTileType(TileShared otherTile) => otherTile.SourceBlock == SourceBlock;
}
9 changes: 4 additions & 5 deletions Shared/Code/Blocks/Glass.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
/* Glass crashes game right now
using Shared.Code.Block_System;
namespace Shared.Blocks
{
public class Glass : BlockShared
{
public override void Initialize()
{
blockName = "Glass";
blockID = -1; // Needs to be changed if this is fixed
blockSmoothing = false;
BlockName = "Glass";
BlockId = 23;
BlockSmoothing = false;
base.Initialize();
}
}
}
*/

2 changes: 1 addition & 1 deletion Shared/Code/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Vector2Int(int x, int y) {
}

/// <inheritdoc />
public override bool Equals(object obj) => obj is Vector2Int @int && Equals(@int);
public override bool Equals(object? obj) => obj is Vector2Int @int && Equals(@int);

/// <summary>
/// Indicates whether this Vector2Int and another's X and Y values are the same.
Expand Down

0 comments on commit af23326

Please sign in to comment.