From b6755636e425ef9f1795e569e90664da368032fa Mon Sep 17 00:00:00 2001 From: Mark Suckerberg Date: Thu, 2 Nov 2023 19:47:39 -0500 Subject: [PATCH] Adds in-game UI --- .../Code/Block System/RenderableTilemap.cs | 2 +- Blocktest/Code/Rendering/Drawable.cs | 10 +++- Blocktest/Code/Scenes/GameScene.cs | 34 +++++++++---- Blocktest/Code/UI/GameUI.cs | 51 +++++++++++++++++++ DedicatedServer/Code/WorldHandler.cs | 28 +++++----- .../Code/Block System/BlockManagerShared.cs | 2 +- 6 files changed, 99 insertions(+), 28 deletions(-) create mode 100644 Blocktest/Code/UI/GameUI.cs diff --git a/Blocktest/Code/Block System/RenderableTilemap.cs b/Blocktest/Code/Block System/RenderableTilemap.cs index bbf8b87..f2fa1ee 100644 --- a/Blocktest/Code/Block System/RenderableTilemap.cs +++ b/Blocktest/Code/Block System/RenderableTilemap.cs @@ -30,7 +30,7 @@ private void OnTilemapChanged(TileShared tile, Vector2Int location) { foreach (Vector2Int dir in Adjacencies) { - if (!_tilemap.TryGetTile(location + dir, out TileShared? adjacentTile)) { + if (!_tilemap.TryGetTile(location + dir, out TileShared? _)) { continue; } _renderables[location.X + dir.X, location.Y + dir.Y].UpdateAdjacencies(location + dir, _tilemap); diff --git a/Blocktest/Code/Rendering/Drawable.cs b/Blocktest/Code/Rendering/Drawable.cs index 7e29967..f90c85d 100644 --- a/Blocktest/Code/Rendering/Drawable.cs +++ b/Blocktest/Code/Rendering/Drawable.cs @@ -1,6 +1,8 @@ +using Myra.Graphics2D; + namespace Blocktest.Rendering; -public sealed class Drawable { +public sealed class Drawable : IImage { public readonly Rectangle Bounds; public readonly Texture2D Texture; @@ -12,4 +14,10 @@ public Drawable(string fileName, Rectangle? bounds = null) { } public static Drawable ErrorDrawable { get; } = new(@"Graphics\Blocks\error"); + + public void Draw(RenderContext context, Rectangle dest, Color color) { + context.Draw(Texture, dest, Bounds, color); + } + + public Point Size => Bounds.Size; } \ No newline at end of file diff --git a/Blocktest/Code/Scenes/GameScene.cs b/Blocktest/Code/Scenes/GameScene.cs index e45924d..dca8d75 100644 --- a/Blocktest/Code/Scenes/GameScene.cs +++ b/Blocktest/Code/Scenes/GameScene.cs @@ -4,7 +4,9 @@ using Blocktest.Misc; using Blocktest.Networking; using Blocktest.Rendering; +using Blocktest.UI; using Microsoft.Xna.Framework.Input; +using Myra.Graphics2D.UI; using Shared.Code; using Shared.Code.Block_System; using Shared.Code.Packets; @@ -23,12 +25,14 @@ public sealed class GameScene : IScene { private readonly SpriteBatch _spriteBatch; private readonly WorldState _worldState = new(); - private int _blockSelected = 1; //ID of the block to place + public int BlockSelected = 1; //ID of the block to place - private bool _buildMode = true; //true for build, false for destroy + public bool BuildMode { get; private set; } = true; //true for build, false for destroy private KeyboardState _previousKeyboardState; - + private readonly Desktop _gameDesktop; + private readonly GameUI _gameUi; + public GameScene(BlocktestGame game, bool doConnect, IPEndPoint? ip) { _connect = doConnect; _spriteBatch = new SpriteBatch(game.GraphicsDevice); @@ -42,6 +46,9 @@ public GameScene(BlocktestGame game, bool doConnect, IPEndPoint? ip) { _blockStrings = BlockManagerShared.AllBlocks.Keys.ToArray(); + _gameUi = new GameUI(this); + _gameDesktop = new Desktop { Root = _gameUi }; + if (_connect && ip != null) { _networkingClient.Connect(ip); return; @@ -80,13 +87,15 @@ public void Draw(GameTime gameTime, GraphicsDevice graphicsDevice) { _frameCounter.Update(deltaTime); _spriteBatch.End(); + + _gameDesktop.Render(); } public void EndScene() { _networkingClient.Stop(); } - public void HandleInput() { + private void HandleInput() { if (!_game.IsActive) { return; } @@ -102,16 +111,15 @@ public void HandleInput() { //press E to toggle build/destroy if (currentKeyboardState.IsKeyDown(Keys.E) && _previousKeyboardState.IsKeyUp(Keys.E)) { - _buildMode = !_buildMode; + BuildMode = !BuildMode; } //Q changes which block you have selected if (currentKeyboardState.IsKeyDown(Keys.Q) && _previousKeyboardState.IsKeyUp(Keys.Q)) { - _blockSelected++; - if (_blockSelected >= BlockManagerShared.AllBlocks.Count) { - _blockSelected = 1; - } + + BlockSelected = (BlockSelected + 1) % BlockManagerShared.AllBlocks.Count; + _gameUi.BlockSelector.SelectedIndex = BlockSelected; } float moveValue = 2.5f; @@ -160,12 +168,12 @@ public void HandleInput() { Math.Clamp((int)mousePos.X / GlobalsShared.GridSize.X, 0, GlobalsShared.MaxX), Math.Clamp((int)mousePos.Y / GlobalsShared.GridSize.Y, 0, GlobalsShared.MaxY)); - if (_buildMode) { + if (BuildMode) { TileChange testChange = new() { TickNum = _networkingClient.LocalTickBuffer.CurrTick, Position = tilePos, Foreground = foreground, - BlockUid = _blockStrings[_blockSelected], + BlockUid = _blockStrings[BlockSelected], SourceId = _networkingClient.Server?.RemoteId ?? 0 }; @@ -219,4 +227,8 @@ private Rectangle GetFitRect() { return new Rectangle(x, y, width, height); } + + public void SetBuildMode(bool buildMode) { + BuildMode = buildMode; + } } \ No newline at end of file diff --git a/Blocktest/Code/UI/GameUI.cs b/Blocktest/Code/UI/GameUI.cs new file mode 100644 index 0000000..8a67d5b --- /dev/null +++ b/Blocktest/Code/UI/GameUI.cs @@ -0,0 +1,51 @@ +using Blocktest.Block_System; +using Blocktest.Scenes; +using Myra.Graphics2D; +using Myra.Graphics2D.UI; +using Shared.Code.Block_System; + +namespace Blocktest.UI; + +public sealed class GameUI : Grid { + public ComboBox BlockSelector; + + public GameUI(GameScene scene) { + BlockSelector = new ComboBox { + GridColumn = 0, + GridRow = 0, + Width = 200, + Height = 40, + Padding = new Thickness(5) + }; + + foreach (var block in BlockManagerShared.AllBlocks) { + var blockItem = new ListItem { + Text = block.Value.BlockName, + Image = BlockSpritesManager.AllBlocksSprites[block.Key].BlockSprite, + Id = block.Key + }; + BlockSelector.Items.Add(blockItem); + } + BlockSelector.SelectedIndexChanged += (_, _) => { + scene.BlockSelected = BlockSelector.SelectedIndex ?? 0; + }; + BlockSelector.SelectedIndex = scene.BlockSelected; + + Widgets.Add(BlockSelector); + + var buildMode = new CheckBox { + GridColumn = 1, + GridRow = 0, + Text = "Build Mode", + Width = 200, + Height = 40, + Padding = new Thickness(5), + IsChecked = scene.BuildMode + }; + buildMode.TouchDown += (_, _) => { + scene.SetBuildMode(!buildMode.IsChecked); + }; + + Widgets.Add(buildMode); + } +} \ No newline at end of file diff --git a/DedicatedServer/Code/WorldHandler.cs b/DedicatedServer/Code/WorldHandler.cs index f8f8e93..c37e401 100644 --- a/DedicatedServer/Code/WorldHandler.cs +++ b/DedicatedServer/Code/WorldHandler.cs @@ -17,11 +17,12 @@ internal sealed class WorldHandler { private readonly WorldState _worldState; private int _continueRun = 1; - private bool _continueWait = true; - private int _counter = 0; - private long _currentTicks = 0; - private TimeSpan _currentTime = TimeSpan.Zero; - private long _previousTicks = 0; + +#if DEBUG + private long _currentTicks; + private long _previousTicks; +#endif + public WorldHandler() { BlockManagerShared.Initialize(); @@ -43,23 +44,22 @@ public void Run() { } private void Loop() { - Timer timer = new(Tick, _frameCounter, TimeSpan.Zero, _targetTime); - //System.Threading.Timer timer = new(Tick, _frameCounter, 16, 16); + var timer = new Timer(Tick, _frameCounter, TimeSpan.Zero, _targetTime); + while (Interlocked.Exchange(ref _continueRun, 1) == 1) { - //Tick(); - //WaitHandler(); Thread.Sleep(1000); } } private void Tick(object? state) { lock (_locker) { - /*currentTicks = stopwatch.ElapsedTicks; - long test = currentTicks - previousTicks; + #if DEBUG + _currentTicks = _stopwatch.ElapsedTicks; + long test = _currentTicks - _previousTicks; Console.WriteLine("CurrentMilliseconds = " + test / 1000000); - Console.WriteLine("Current Tick = " + GlobalsServer.serverTickBuffer.currTick); - previousTicks = currentTicks; - counter++;*/ + Console.WriteLine("Current Tick = " + _server.LocalTickBuffer.CurrTick); + _previousTicks = _currentTicks; + #endif _server.Update(); } } diff --git a/Shared/Code/Block System/BlockManagerShared.cs b/Shared/Code/Block System/BlockManagerShared.cs index d7473f8..6c35801 100644 --- a/Shared/Code/Block System/BlockManagerShared.cs +++ b/Shared/Code/Block System/BlockManagerShared.cs @@ -21,7 +21,7 @@ public static void Initialize() { IDeserializer deserialize = new DeserializerBuilder().Build(); Assembly assembly = typeof(BlockManagerShared).Assembly; string[] assemblyNames = assembly.GetManifestResourceNames(); - var blockNames = assemblyNames.Where(x => x.StartsWith("Shared.Content.Blocks.")); + var blockNames = assemblyNames.Where(x => x.StartsWith("Shared.Content.Blocks.")).Order(); AllBlocks = new Dictionary(); foreach (string resourceName in blockNames) {