From af23326cb61946090c54c3e314021fac0453b1e3 Mon Sep 17 00:00:00 2001 From: Mark Suckerberg Date: Fri, 6 Oct 2023 12:50:05 -0500 Subject: [PATCH] more cleanup --- .idea/.idea.Blocktest/.idea/.name | 1 + Blocktest/BlocktestGame.cs | 2 +- Blocktest/Code/Block System/BlockSprites.cs | 10 ++-- .../Code/Block System/BlockSpritesManager.cs | 7 ++- Blocktest/Code/Block System/TilemapSprites.cs | 9 +++- Blocktest/Code/Networking/Client.cs | 18 +++---- Blocktest/Code/Scenes/GameScene.cs | 5 ++ .../Code/Block System/BlockManagerShared.cs | 7 ++- Shared/Code/Block System/TilemapShared.cs | 54 +++++-------------- Shared/Code/Blocks/Glass.cs | 9 ++-- Shared/Code/Math.cs | 2 +- 11 files changed, 54 insertions(+), 70 deletions(-) create mode 100644 .idea/.idea.Blocktest/.idea/.name diff --git a/.idea/.idea.Blocktest/.idea/.name b/.idea/.idea.Blocktest/.idea/.name new file mode 100644 index 0000000..99ea834 --- /dev/null +++ b/.idea/.idea.Blocktest/.idea/.name @@ -0,0 +1 @@ +Blocktest \ No newline at end of file diff --git a/Blocktest/BlocktestGame.cs b/Blocktest/BlocktestGame.cs index 5d4d380..8bbe51e 100644 --- a/Blocktest/BlocktestGame.cs +++ b/Blocktest/BlocktestGame.cs @@ -40,7 +40,7 @@ protected override void Initialize() { /// protected override void LoadContent() { ContentManager = Content; - BlockSpritesManager.LoadBlockSprites(Content); + BlockSpritesManager.LoadBlockSprites(); _currentScene = new GameScene(this, _connect, _ip); } diff --git a/Blocktest/Code/Block System/BlockSprites.cs b/Blocktest/Code/Block System/BlockSprites.cs index cfce130..32ef2c3 100644 --- a/Blocktest/Code/Block System/BlockSprites.cs +++ b/Blocktest/Code/Block System/BlockSprites.cs @@ -17,9 +17,9 @@ public sealed class BlockSprites { /* METHODS */ - public BlockSprites(BlockShared newBlockShared, ContentManager content) { + public BlockSprites(BlockShared newBlockShared) { BlockShared = newBlockShared; - LoadSprite(content); + LoadSprite(); } /// @@ -28,16 +28,16 @@ public BlockSprites(BlockShared newBlockShared, ContentManager content) { /// /// DO NOT FORGET TO CALL THE BASE METHOD IF YOU OVERRIDE THIS. /// - 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 " + diff --git a/Blocktest/Code/Block System/BlockSpritesManager.cs b/Blocktest/Code/Block System/BlockSpritesManager.cs index 1beae29..49faa06 100644 --- a/Blocktest/Code/Block System/BlockSpritesManager.cs +++ b/Blocktest/Code/Block System/BlockSpritesManager.cs @@ -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; } diff --git a/Blocktest/Code/Block System/TilemapSprites.cs b/Blocktest/Code/Block System/TilemapSprites.cs index 103b242..5fc7615 100644 --- a/Blocktest/Code/Block System/TilemapSprites.cs +++ b/Blocktest/Code/Block System/TilemapSprites.cs @@ -1,4 +1,5 @@ using Blocktest.Rendering; +using Shared.Code; using Shared.Code.Block_System; namespace Blocktest.Block_System; @@ -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); } diff --git a/Blocktest/Code/Networking/Client.cs b/Blocktest/Code/Networking/Client.cs index 32233ef..8c32727 100644 --- a/Blocktest/Code/Networking/Client.cs +++ b/Blocktest/Code/Networking/Client.cs @@ -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(); } @@ -36,10 +36,10 @@ public void Update() { /// Contains the packet from the server. /// /// The delivery method used to deliver this packet. - 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; @@ -58,7 +58,7 @@ protected void NetworkRecieveEvent(NetPeer peer, NetPacketReader packetReader, b /// Handles packets after the first. /// /// Contains the packet sent by the server. - public void HandlePackets(NetPacketReader packetReader) { + private void HandlePackets(NetPacketReader packetReader) { byte packetByte = packetReader.GetByte(); PacketType packetType = (PacketType)packetByte; switch (packetType) { @@ -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); } } \ No newline at end of file diff --git a/Blocktest/Code/Scenes/GameScene.cs b/Blocktest/Code/Scenes/GameScene.cs index 7a2ffa8..731b333 100644 --- a/Blocktest/Code/Scenes/GameScene.cs +++ b/Blocktest/Code/Scenes/GameScene.cs @@ -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, diff --git a/Shared/Code/Block System/BlockManagerShared.cs b/Shared/Code/Block System/BlockManagerShared.cs index f58d8ee..efdd758 100644 --- a/Shared/Code/Block System/BlockManagerShared.cs +++ b/Shared/Code/Block System/BlockManagerShared.cs @@ -18,6 +18,7 @@ public static BlockShared[] AllBlocks { get => _allBlocks; private set => _allBlocks = value; } + /// List used to store the names of blocks. The indexes are the corresponding block's ID. public static string[] BlockNames { get => _blockNames; @@ -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; diff --git a/Shared/Code/Block System/TilemapShared.cs b/Shared/Code/Block System/TilemapShared.cs index 5b451d0..4a019d4 100644 --- a/Shared/Code/Block System/TilemapShared.cs +++ b/Shared/Code/Block System/TilemapShared.cs @@ -11,7 +11,7 @@ public sealed class TilemapShared { /// A list of s that specify which blocks should be refreshed when a tile is placed/destroyed. /// Defaults to the changed block and all cardinal directions. /// - private readonly List _adjacencies = new() + private static readonly List Adjacencies = new() { Vector2Int.Zero, Vector2Int.Up, Vector2Int.Down, Vector2Int.Left, Vector2Int.Right }; /// @@ -59,11 +59,9 @@ public TileShared SetBlock(Vector2Int location, BlockShared newBlock) => /// Location the new Block will be placed. /// Block type to be placed in the cell. 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 || @@ -77,37 +75,10 @@ public TileShared SetTile(Vector2Int location, TileShared newTile) { } /// - /// Deletes a at a specific location(sets value to null). + /// Deletes a at a specific location (sets block to air). /// /// - public void DeleteTile(Vector2Int location) => SetTile(location, null); - - /// - /// Gets the at a specific location on a . - /// - /// Location of the Tile on the Tilemap to check. - /// placed at the cell. - public TileShared? GetTile(Vector2Int location) => GetTile(location.X, location.Y); - - /// - /// Gets the at a specific location on a . - /// - /// X position of the Tile on the Tilemap to check. - /// Y position of the Tile on the Tilemap to check. - /// placed at the cell. - public TileShared? GetTile(int x, int y) { - if (x < 0 || y < 0 || x >= TilemapSize.X || y >= TilemapSize.Y) { - return null; - } - return TileGrid[x, y]; - } - - /// - /// Returns whether there is a at the location specified. - /// - /// Location to check. - /// Returns true if there is a Tile at the position. Returns false otherwise. - public bool HasTile(Vector2Int location) => TileGrid[location.X, location.Y] != null; + public void DeleteTile(Vector2Int location) => SetBlock(location, BlockManagerShared.AllBlocks[0]); public bool TryGetTile(Vector2Int location, [NotNullWhen(true)] out T? result) where T : TileShared { result = null; @@ -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 } /// @@ -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; @@ -195,13 +166,12 @@ public void UpdateAdjacencies(Vector2Int position, TilemapShared tilemap) { /// The tilemap on which the tile you want to check for smoothing is. /// Whether or not the tile can smooth with this tile. 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; } /// @@ -209,5 +179,5 @@ private bool HasSmoothableTile(Vector2Int position, TilemapShared tilemap) { /// /// The other tile to check. /// Whether or not the other block is the same type as the current tile - private bool IsSameTileType(TileShared otherTile) => otherTile?.SourceBlock == SourceBlock; + private bool IsSameTileType(TileShared otherTile) => otherTile.SourceBlock == SourceBlock; } \ No newline at end of file diff --git a/Shared/Code/Blocks/Glass.cs b/Shared/Code/Blocks/Glass.cs index fc8de0e..8b89ae3 100644 --- a/Shared/Code/Blocks/Glass.cs +++ b/Shared/Code/Blocks/Glass.cs @@ -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(); } } } -*/ diff --git a/Shared/Code/Math.cs b/Shared/Code/Math.cs index fc4d772..d60f0fa 100644 --- a/Shared/Code/Math.cs +++ b/Shared/Code/Math.cs @@ -25,7 +25,7 @@ public Vector2Int(int x, int y) { } /// - public override bool Equals(object obj) => obj is Vector2Int @int && Equals(@int); + public override bool Equals(object? obj) => obj is Vector2Int @int && Equals(@int); /// /// Indicates whether this Vector2Int and another's X and Y values are the same.