Skip to content

Commit

Permalink
Adds in-game UI
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkSuckerberg committed Nov 3, 2023
1 parent 8a81ee0 commit b675563
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Blocktest/Code/Block System/RenderableTilemap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion Blocktest/Code/Rendering/Drawable.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}
34 changes: 23 additions & 11 deletions Blocktest/Code/Scenes/GameScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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
};

Expand Down Expand Up @@ -219,4 +227,8 @@ private Rectangle GetFitRect() {

return new Rectangle(x, y, width, height);
}

public void SetBuildMode(bool buildMode) {
BuildMode = buildMode;
}
}
51 changes: 51 additions & 0 deletions Blocktest/Code/UI/GameUI.cs
Original file line number Diff line number Diff line change
@@ -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,

Check warning on line 14 in Blocktest/Code/UI/GameUI.cs

View workflow job for this annotation

GitHub Actions / build

'Widget.GridColumn' is obsolete: 'Use Grid.GetColumn/Grid.SetColumn'
GridRow = 0,

Check warning on line 15 in Blocktest/Code/UI/GameUI.cs

View workflow job for this annotation

GitHub Actions / build

'Widget.GridRow' is obsolete: 'Use Grid.GetColumn/Grid.SetColumn'
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,

Check warning on line 37 in Blocktest/Code/UI/GameUI.cs

View workflow job for this annotation

GitHub Actions / build

'Widget.GridColumn' is obsolete: 'Use Grid.GetColumn/Grid.SetColumn'
GridRow = 0,

Check warning on line 38 in Blocktest/Code/UI/GameUI.cs

View workflow job for this annotation

GitHub Actions / build

'Widget.GridRow' is obsolete: 'Use Grid.GetColumn/Grid.SetColumn'
Text = "Build Mode",
Width = 200,
Height = 40,
Padding = new Thickness(5),
IsChecked = scene.BuildMode
};
buildMode.TouchDown += (_, _) => {
scene.SetBuildMode(!buildMode.IsChecked);
};

Widgets.Add(buildMode);
}
}
28 changes: 14 additions & 14 deletions DedicatedServer/Code/WorldHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
}
Expand Down
2 changes: 1 addition & 1 deletion Shared/Code/Block System/BlockManagerShared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, BlockShared>();
foreach (string resourceName in blockNames) {
Expand Down

0 comments on commit b675563

Please sign in to comment.