From 072c4de56c8faa0fca4ab6f9134bdc0e64e1444b Mon Sep 17 00:00:00 2001 From: MasterGordon Date: Mon, 26 Dec 2022 00:55:42 +0100 Subject: [PATCH] added debug console --- .editorconfig | 4 +- Mine2d/Mine2d.csproj | 1 + Mine2d/assets/blocks/coal-ore.png | Bin 0 -> 243 bytes Mine2d/assets/blocks/furnace.png | Bin 0 -> 390 bytes .../assets/blocks/simple-toolbench.aseprite | Bin 0 -> 703 bytes Mine2d/assets/items/coal.png | Bin 0 -> 201 bytes Mine2d/engine/EventPublisher.cs | 4 +- Mine2d/engine/EventService.cs | 46 +++---- Mine2d/engine/Renderer.cs | 2 +- Mine2d/engine/system/EventType.cs | 2 + .../backend/interactor/DebugCommandHandler.cs | 52 ++++++++ Mine2d/game/backend/interactor/Move.cs | 47 ++++---- Mine2d/game/backend/interactor/Place.cs | 1 + .../game/backend/interactor/PlayerMovement.cs | 37 ------ .../network/packets/DebugCommandPacket.cs | 41 +++++++ .../backend/network/packets/PacketType.cs | 3 +- Mine2d/game/core/Constants.cs | 4 +- Mine2d/game/core/Debugger.cs | 14 +++ Mine2d/game/core/PlayerEntity.cs | 12 +- Mine2d/game/core/tiles/TileRegistry.cs | 22 ++-- Mine2d/game/core/world/WorldGenerator.cs | 6 +- Mine2d/game/frontend/events/DebugInput.cs | 112 ++++++++++++++++-- Mine2d/game/frontend/events/InventoryInput.cs | 1 + .../inventory/DebugConsoleInventory.cs | 29 +++++ .../frontend/inventory/InventoryConstants.cs | 7 ++ .../frontend/inventory/InventoryRegistry.cs | 1 + .../frontend/renderer/InventoryRenderer.cs | 61 +++++----- Mine2d/game/state/FrontendGameState.cs | 15 ++- 28 files changed, 360 insertions(+), 164 deletions(-) create mode 100644 Mine2d/assets/blocks/coal-ore.png create mode 100644 Mine2d/assets/blocks/furnace.png create mode 100644 Mine2d/assets/blocks/simple-toolbench.aseprite create mode 100644 Mine2d/assets/items/coal.png create mode 100644 Mine2d/game/backend/interactor/DebugCommandHandler.cs delete mode 100644 Mine2d/game/backend/interactor/PlayerMovement.cs create mode 100644 Mine2d/game/backend/network/packets/DebugCommandPacket.cs create mode 100644 Mine2d/game/core/Debugger.cs create mode 100644 Mine2d/game/frontend/inventory/DebugConsoleInventory.cs diff --git a/.editorconfig b/.editorconfig index a7511ca..2bca450 100644 --- a/.editorconfig +++ b/.editorconfig @@ -82,5 +82,5 @@ resharper_web_config_module_not_resolved_highlighting = warning resharper_web_config_type_not_resolved_highlighting = warning resharper_web_config_wrong_module_highlighting = warning -# ignore RCS1102 -dotnet_diagnostic.RCS1102.severity = none \ No newline at end of file +dotnet_diagnostic.RCS1102.severity = none +csharp_style_namespace_declarations = file_scoped:warning diff --git a/Mine2d/Mine2d.csproj b/Mine2d/Mine2d.csproj index 62630ea..373da46 100644 --- a/Mine2d/Mine2d.csproj +++ b/Mine2d/Mine2d.csproj @@ -5,6 +5,7 @@ net7.0 enable disable + true diff --git a/Mine2d/assets/blocks/coal-ore.png b/Mine2d/assets/blocks/coal-ore.png new file mode 100644 index 0000000000000000000000000000000000000000..9c475eb5360bbe006ed242e6ae678fdda2ecf821 GIT binary patch literal 243 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|)_S@)hFJ8T zow$+ju!4ZIeB=Y>OH+IQ{Qqtuooy^U<3&I$m%CDF)@#<5^UqQauTOlksU?81Nb`t< zul1IefMQJsX7iVf4YzH=bKk6CJy0Fb&dl&!X2v<0n=5Qhmo=}MI_LAt0PTChuXa54 zj?DG%Jie`Tb;bMD9H!r6Jr?M;m9Ep8T%MjA{&mUGg4mfI4`WNy8P*>;ueE#q>WSaY rUU+s#zc`iDgTe~DWM4fC_ZEK literal 0 HcmV?d00001 diff --git a/Mine2d/assets/blocks/furnace.png b/Mine2d/assets/blocks/furnace.png new file mode 100644 index 0000000000000000000000000000000000000000..0d0809b43ffa702f8f56981ccc6de74543df78d6 GIT binary patch literal 390 zcmV;10eSw3P)Px$KuJVFR9J;$U>F4h3>b-3sHCX)ADMse{(YkL;sZ?7Fl5vLL)rl>)H0~t#s;Tc z=aC~A>R@72%?0Rc7#J8BLZziK)FC^FY9W9NAX|nF40swCa0d{K0&MY$EKX^OIN%|G z)%E2zHVmtuK85?7gOiit$M^3f8H}zcR9c!5TY|*u09?UNDZmz81k->h=WB{Be*Ik( zryO1fh;qJWuu!AC);)3SHBLvMn2sLwzWQ${wj2gFUlf6}k)r~h3uqPZ*Z{I!O!T%K z-4XCKfGw+CeM}q6k?n#<0jc?%5`b(M3j+fKgP@=wR)z2XV;3g}V721@eTW0zzkg3P zi%111b-}VuPljcko^)Zu07^l0@75){v9UVf{ku1GV`Fu|t$R0S~tFfcGYfBu{i06l@bF5wnW`Tzg`07*qoM6N<$g1Bv+K>z>% literal 0 HcmV?d00001 diff --git a/Mine2d/assets/blocks/simple-toolbench.aseprite b/Mine2d/assets/blocks/simple-toolbench.aseprite new file mode 100644 index 0000000000000000000000000000000000000000..916f032f3af2dc9c29de8334b0227a22658aec17 GIT binary patch literal 703 zcmb`F-%C?r7{}jbYd31vg_b|qTZ`yoxw92BQ5Xp!WZ~Gwq=Hh98Xm%a7?BhbQV4dj zm_f@$gycm~L|tSUl-NZ=bfFfyB(hF*Q53jnP_EuSXDa#!_`v(&InN94_w$}}o~;sM zX22$-$Ya@r5KXM5VrC85&089-U>6|&&#XO@YUkX1mP2MDm!-U=-zeq6#JOxI(<&i0 z%bjq~wo2Gi?uC(X1$=*UD@@#Kfj^%2!MUU#UjH0~##0TJU2TIktsWSv*#do)sdG!gtoK8bU2@$_H2rqVa!&bw92f`tErm6?t_4#0;PKT4{hG68{ zUf6S7QwA4UZM}C%!zH)@`nV&GHOlxRiz%8oB8VMocp-%mI+lx4SfD^Y;Up7FCV`|8 z<+PGy7F;Y8ghT9W=x9DAy^naX%lR$EN0~3{w>W!4uOH4Gcju_;!f*dxY}((ht^e*B zd|=a!srv(WemkAvP-sMp_odACEdP^+RB!*y;ZaeK)2t~0pHr3YiC z=`!5~71`cnU&r38``Fv)Fa0(3y76a`JNwE+;r3+7Xl|4LVae$EgI_vgMJE!qk>r~E ey2t6-wHG|;>i)mcXjJWv9ZtEX9b)*Xy7V8aRMe*c literal 0 HcmV?d00001 diff --git a/Mine2d/assets/items/coal.png b/Mine2d/assets/items/coal.png new file mode 100644 index 0000000000000000000000000000000000000000..40f176d22d58daa17e5fc918621550feffbe3b6a GIT binary patch literal 201 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|nmt_{Lo9ml zPTa_QK!JxPH-RZd4Z*sI3CAS4)cb@iPj$EMEM8#$5&#;pR&jEpB2_hqfE yX8XIGF=AHE`i*;Iydu|c^zSKTu> eventListeners = new(); - public EventPublisher() + public eventPublisher() { this.Scan(); } diff --git a/Mine2d/engine/EventService.cs b/Mine2d/engine/EventService.cs index 2975d45..1448122 100644 --- a/Mine2d/engine/EventService.cs +++ b/Mine2d/engine/EventService.cs @@ -4,44 +4,26 @@ namespace Mine2d.engine; public class EventService { - private static readonly EventPublisher EventPublisher = new(); + private static readonly eventPublisher EventPublisher = new(); public static void PollEvents() { while (SDL_PollEvent(out var e) != 0) { - if (e.type == SDL_EventType.SDL_QUIT) + var eventType = e.type switch { - EventPublisher.Publish(EventType.Quit, e); - } - if (e.type == SDL_EventType.SDL_WINDOWEVENT && e.window.windowEvent == SDL_WindowEventID.SDL_WINDOWEVENT_RESIZED) - { - EventPublisher.Publish(EventType.WindowResize, e); - } - if (e.type == SDL_EventType.SDL_MOUSEMOTION) - { - EventPublisher.Publish(EventType.MouseMotion, e); - } - if (e.type == SDL_EventType.SDL_MOUSEBUTTONDOWN) - { - EventPublisher.Publish(EventType.MouseButtonDown, e); - } - if (e.type == SDL_EventType.SDL_MOUSEBUTTONUP) - { - EventPublisher.Publish(EventType.MouseButtonUp, e); - } - if (e.type == SDL_EventType.SDL_KEYDOWN) - { - EventPublisher.Publish(EventType.KeyDown, e); - } - if (e.type == SDL_EventType.SDL_KEYUP) - { - EventPublisher.Publish(EventType.KeyUp, e); - } - if (e.type == SDL_EventType.SDL_MOUSEWHEEL) - { - EventPublisher.Publish(EventType.MouseWheel, e); - } + SDL_EventType.SDL_QUIT => EventType.Quit, + SDL_EventType.SDL_WINDOWEVENT when e.window.windowEvent == SDL_WindowEventID.SDL_WINDOWEVENT_RESIZED => EventType.WindowResize, + SDL_EventType.SDL_MOUSEMOTION => EventType.MouseMotion, + SDL_EventType.SDL_MOUSEBUTTONDOWN => EventType.MouseButtonDown, + SDL_EventType.SDL_MOUSEBUTTONUP => EventType.MouseButtonUp, + SDL_EventType.SDL_KEYDOWN => EventType.KeyDown, + SDL_EventType.SDL_KEYUP => EventType.KeyUp, + SDL_EventType.SDL_MOUSEWHEEL => EventType.MouseWheel, + SDL_EventType.SDL_TEXTINPUT => EventType.TextInput, + _ => EventType.Unhandled + }; + EventPublisher.Publish(eventType, e); } EventPublisher.Publish(EventType.Tick, new SDL_Event()); } diff --git a/Mine2d/engine/Renderer.cs b/Mine2d/engine/Renderer.cs index 711cca6..80fc10d 100644 --- a/Mine2d/engine/Renderer.cs +++ b/Mine2d/engine/Renderer.cs @@ -94,7 +94,7 @@ public class Renderer public (IntPtr texture, int width, int height, IntPtr surfaceMessage) CreateTextTexture(string text) { - var surfaceMessage = TTF_RenderText_Solid(this.font, text, this.color); + var surfaceMessage = TTF_RenderText_Solid_Wrapped(this.font, text, this.color, 1000); var texture = SDL_CreateTextureFromSurface(this.renderer, surfaceMessage); ProcessStatus(SDL_QueryTexture(texture, out _, out _, out var width, out var height)); return (texture, width, height, surfaceMessage); diff --git a/Mine2d/engine/system/EventType.cs b/Mine2d/engine/system/EventType.cs index 67a5433..edd7258 100644 --- a/Mine2d/engine/system/EventType.cs +++ b/Mine2d/engine/system/EventType.cs @@ -11,4 +11,6 @@ public enum EventType WindowResize, MouseWheel, Tick, + TextInput, + Unhandled } diff --git a/Mine2d/game/backend/interactor/DebugCommandHandler.cs b/Mine2d/game/backend/interactor/DebugCommandHandler.cs new file mode 100644 index 0000000..087373c --- /dev/null +++ b/Mine2d/game/backend/interactor/DebugCommandHandler.cs @@ -0,0 +1,52 @@ +using Mine2d.engine.extensions; +using Mine2d.engine.system.annotations; +using Mine2d.game.backend.network.packets; +using Mine2d.game.core; + +namespace Mine2d.game.backend.interactor; + +[Interactor] +public class DebugCommandHandler +{ + [Interaction(InteractorKind.Hybrid, PacketType.DebugCommand)] + public static void HandleUnknownDebugCommand(DebugCommandPacket packet) + { + if (packet.Command == DebugCommand.Unknown) + { + Debugger.Print("Unknown debug command '" + packet.RawCommand + "'"); + Debugger.Print("Available commands:"); + var cmds = string.Join(", ", Enum.GetValues().Where(command => command != DebugCommand.Unknown).Select(command => command.ToString().ToLower())); + Debugger.Print(cmds); + } + } + + [Interaction(InteractorKind.Hybrid, PacketType.DebugCommand)] + public static void HandleHelpDebugCommand(DebugCommandPacket packet) + { + if (packet.Command == DebugCommand.Help) + { + var enumType = typeof(DebugCommand); + Enum.GetValues() + .Where(command => command != DebugCommand.Unknown) + .ForEach(command => + { + var memberInfo = enumType.GetMember(command.ToString()); + var enumValueMemberInfo = memberInfo[0]; + var valueAttributes = enumValueMemberInfo.GetCustomAttributes(typeof(DebugCommandAttribute), false); + var attribute = (DebugCommandAttribute)valueAttributes[0]; + Debugger.Print(command.ToString().ToLower() + attribute.Usage + " - " + attribute.Desc); + }); + } + } + + [Interaction(InteractorKind.Hybrid, PacketType.DebugCommand)] + public static void HandleNoClipDebugCommand(DebugCommandPacket packet) + { + if (packet.Command == DebugCommand.NoClip) + { + var ds = Context.Get().FrontendGameState.DebugState; + ds.NoClip = !ds.NoClip; + Debugger.Print("noclip: " + ds.NoClip); + } + } +} \ No newline at end of file diff --git a/Mine2d/game/backend/interactor/Move.cs b/Mine2d/game/backend/interactor/Move.cs index 98958f3..99eda70 100644 --- a/Mine2d/game/backend/interactor/Move.cs +++ b/Mine2d/game/backend/interactor/Move.cs @@ -18,34 +18,33 @@ public class Move } } - // [Interaction(InteractorKind.Hybrid, PacketType.Tick)] - public static void TickHybrid() - { - var ctx = Context.Get(); - var fromPositions = new Dictionary(); - foreach (var player in ctx.GameState.Players) - { - fromPositions.Add(player.Id, player.Position); - } - ctx.GameState.Players.ForEach(PlayerEntity.Move); - ctx.GameState.Players.ForEach(PlayerEntity.Collide); - foreach (var player in ctx.GameState.Players) - { - if (player.Position != fromPositions[player.Id]) - { - ctx.Backend.ProcessPacket(new PlayerMovedPacket - { - PlayerGuid = player.Id, - Target = player.Position - }); - } - } - } - [Interaction(InteractorKind.Client, PacketType.Tick)] public static void SelfMovedClient() { var camera = Context.Get().FrontendGameState.Camera; camera.CenterOn(PlayerEntity.GetSelf().Position); } + + [Interaction(InteractorKind.Hybrid, PacketType.Tick)] + public static void OnHybridTick() + { + var context = Context.Get(); + var gameState = context.GameState; + + gameState.Players.ForEach(player => + { + var position = player.Position; + PlayerEntity.Move(player); + PlayerEntity.Collide(player); + + if (position != player.Position) + { + context.Backend.ProcessPacket(new PlayerMovedPacket + { + PlayerGuid = player.Id, + Target = player.Position + }); + } + }); + } } diff --git a/Mine2d/game/backend/interactor/Place.cs b/Mine2d/game/backend/interactor/Place.cs index b4e825a..d3cab9e 100644 --- a/Mine2d/game/backend/interactor/Place.cs +++ b/Mine2d/game/backend/interactor/Place.cs @@ -3,6 +3,7 @@ using Mine2d.game.backend.network.packets; using Mine2d.game.core; using Mine2d.game.core.data; using Mine2d.game.core.tiles; +using Mine2d.game.frontend.inventory; using Mine2d.game.state; namespace Mine2d.game.backend.interactor; diff --git a/Mine2d/game/backend/interactor/PlayerMovement.cs b/Mine2d/game/backend/interactor/PlayerMovement.cs deleted file mode 100644 index 0f2a7f5..0000000 --- a/Mine2d/game/backend/interactor/PlayerMovement.cs +++ /dev/null @@ -1,37 +0,0 @@ -using Mine2d.engine.system.annotations; -using Mine2d.game.backend.network.packets; -using Mine2d.game.core; - -namespace Mine2d.game.backend.interactor; - -[Interactor] -public class PlayerMovement -{ - [Interaction(InteractorKind.Client, PacketType.Tick)] - public static void OnTick() - { - } - - [Interaction(InteractorKind.Hybrid, PacketType.Tick)] - public static void OnHybridTick() - { - var context = Context.Get(); - var gameState = context.GameState; - - gameState.Players.ForEach(player => - { - var position = player.Position; - PlayerEntity.Move(player); - PlayerEntity.Collide(player); - - if (position != player.Position) - { - context.Backend.ProcessPacket(new PlayerMovedPacket - { - PlayerGuid = player.Id, - Target = player.Position - }); - } - }); - } -} \ No newline at end of file diff --git a/Mine2d/game/backend/network/packets/DebugCommandPacket.cs b/Mine2d/game/backend/network/packets/DebugCommandPacket.cs new file mode 100644 index 0000000..ab7e0f6 --- /dev/null +++ b/Mine2d/game/backend/network/packets/DebugCommandPacket.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Mine2d.game.backend.network.packets; + +[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)] +public class DebugCommandAttribute : Attribute +{ + public string Usage { get; set; } + public string Desc { get; set; } + + public DebugCommandAttribute(string usage, string desc) + { + this.Usage = usage; + this.Desc = desc; + } +} + +public enum DebugCommand { + [DebugCommand("", "Disables gravity and collision")] + NoClip, + + [DebugCommand(" ", "Gives you an item")] + Give, + + [DebugCommand("", "Lists all commands")] + Help, + + Unknown +} + +public class DebugCommandPacket : Packet +{ + public override PacketType Type => PacketType.DebugCommand; + + public DebugCommand Command { get; set; } + public string RawCommand { get; set; } + public string[] Args { get; set; } +} \ No newline at end of file diff --git a/Mine2d/game/backend/network/packets/PacketType.cs b/Mine2d/game/backend/network/packets/PacketType.cs index 839bad0..3fee7a7 100644 --- a/Mine2d/game/backend/network/packets/PacketType.cs +++ b/Mine2d/game/backend/network/packets/PacketType.cs @@ -11,5 +11,6 @@ public enum PacketType PlayerMoved, BlockBroken, Place, - Jump + Jump, + DebugCommand } diff --git a/Mine2d/game/core/Constants.cs b/Mine2d/game/core/Constants.cs index 708d93c..b87a075 100644 --- a/Mine2d/game/core/Constants.cs +++ b/Mine2d/game/core/Constants.cs @@ -5,7 +5,7 @@ public class Constants public const int ChunkSize = 32; public const int TileSize = 16; public const int BreakDistance = 64; - public static Vector2 Gravity = new(0, 0.1f); + public static Vector2 Gravity = new(0, 0.06f); - public const float JumpSpeed = 3f; + public const float JumpSpeed = 2.2f; } diff --git a/Mine2d/game/core/Debugger.cs b/Mine2d/game/core/Debugger.cs new file mode 100644 index 0000000..d4f6f35 --- /dev/null +++ b/Mine2d/game/core/Debugger.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Mine2d.game.core; + +public static class Debugger +{ + public static void Print(string message) + { + Context.Get().FrontendGameState.DebugState.Messages.Enqueue(message); + } +} \ No newline at end of file diff --git a/Mine2d/game/core/PlayerEntity.cs b/Mine2d/game/core/PlayerEntity.cs index 340871a..6d84b7a 100644 --- a/Mine2d/game/core/PlayerEntity.cs +++ b/Mine2d/game/core/PlayerEntity.cs @@ -21,7 +21,16 @@ public class PlayerEntity { var context = Context.Get(); var frontEndState = context.FrontendGameState; + var ds = frontEndState.DebugState; var inputState = frontEndState.InputState; + if (ds.NoClip) + { + player.Position += new Vector2( + inputState.GetAxis(InputAxis.Horizontal) * 2, + inputState.GetAxis(InputAxis.Vertical) * 2 + ); + return; + } var movement = player.PlayerMovementState; @@ -29,7 +38,7 @@ public class PlayerEntity { movement.CurrentVelocity += Constants.Gravity; } - else if(movement.CurrentVelocity.Y > 0) + else if (movement.CurrentVelocity.Y > 0) { movement.CurrentVelocity = movement.CurrentVelocity with { @@ -59,6 +68,7 @@ public class PlayerEntity public static void Collide(Player player) { + if (Context.Get().FrontendGameState.DebugState.NoClip) return; var movement = player.PlayerMovementState; var world = Context.Get().GameState.World; bool hasCollision; diff --git a/Mine2d/game/core/tiles/TileRegistry.cs b/Mine2d/game/core/tiles/TileRegistry.cs index d99b9aa..949b46b 100644 --- a/Mine2d/game/core/tiles/TileRegistry.cs +++ b/Mine2d/game/core/tiles/TileRegistry.cs @@ -35,17 +35,17 @@ public class TileRegistry this.Tiles.Add(4, new OreTile("ore3", new[] { "stone", "ore3" }, 8)); this.Tiles.Add(5, new OreTile("ore4", new[] { "stone", "ore4" }, 10)); this.Tiles.Add(6, new Workbench("workbench", "workbench", 10)); - this.Tiles.Add(7, new OreTile("iron-ore", new [] {"stone", "iron-ore"}, 6, ItemId.RawIron)); - this.Tiles.Add(8, new OreTile("copper-ore", new [] {"stone", "copper-ore"}, 6, ItemId.RawCopper)); - this.Tiles.Add(9, new OreTile("tin-ore", new [] {"stone", "tin-ore"}, 6, ItemId.RawTin)); - this.Tiles.Add(10, new OreTile("silver-ore", new [] {"stone", "silver-ore"}, 7, ItemId.RawSilver)); - this.Tiles.Add(11, new OreTile("gold-ore", new [] {"stone", "gold-ore"}, 8, ItemId.RawGold)); - this.Tiles.Add(12, new OreTile("lead-ore", new [] {"stone", "lead-ore"}, 9, ItemId.RawLead)); - this.Tiles.Add(13, new OreTile("platinum-ore", new [] {"stone", "platinum-ore"}, 10, ItemId.RawPlatinum)); - this.Tiles.Add(14, new OreTile("cobalt-ore", new [] {"stone", "cobalt-ore"}, 11, ItemId.RawCobalt)); - this.Tiles.Add(15, new OreTile("tungsten-ore", new [] {"stone", "tungsten-ore"}, 15, ItemId.RawTungsten)); - this.Tiles.Add(16, new OreTile("uranium-ore", new [] {"stone", "uranium-ore"}, 15, ItemId.RawUranium)); - this.Tiles.Add(17, new OreTile("diamond-ore", new [] {"stone", "diamond-ore"}, 10, ItemId.Diamond)); + this.Tiles.Add(7, new OreTile("iron-ore", new[] { "stone", "iron-ore" }, 6, ItemId.RawIron)); + this.Tiles.Add(8, new OreTile("copper-ore", new[] { "stone", "copper-ore" }, 6, ItemId.RawCopper)); + this.Tiles.Add(9, new OreTile("tin-ore", new[] { "stone", "tin-ore" }, 6, ItemId.RawTin)); + this.Tiles.Add(10, new OreTile("silver-ore", new[] { "stone", "silver-ore" }, 7, ItemId.RawSilver)); + this.Tiles.Add(11, new OreTile("gold-ore", new[] { "stone", "gold-ore" }, 8, ItemId.RawGold)); + this.Tiles.Add(12, new OreTile("lead-ore", new[] { "stone", "lead-ore" }, 9, ItemId.RawLead)); + this.Tiles.Add(13, new OreTile("platinum-ore", new[] { "stone", "platinum-ore" }, 10, ItemId.RawPlatinum)); + this.Tiles.Add(14, new OreTile("cobalt-ore", new[] { "stone", "cobalt-ore" }, 11, ItemId.RawCobalt)); + this.Tiles.Add(15, new OreTile("tungsten-ore", new[] { "stone", "tungsten-ore" }, 15, ItemId.RawTungsten)); + this.Tiles.Add(16, new OreTile("uranium-ore", new[] { "stone", "uranium-ore" }, 15, ItemId.RawUranium)); + this.Tiles.Add(17, new OreTile("diamond-ore", new[] { "stone", "diamond-ore" }, 10, ItemId.Diamond)); } public Tile GetTile(int id) diff --git a/Mine2d/game/core/world/WorldGenerator.cs b/Mine2d/game/core/world/WorldGenerator.cs index 90f6979..8fb801f 100644 --- a/Mine2d/game/core/world/WorldGenerator.cs +++ b/Mine2d/game/core/world/WorldGenerator.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Mine2d.game.core.tiles; namespace Mine2d.game.core.world; @@ -111,4 +107,4 @@ public class WorldGenerator } return Tiles.Stone; } -} \ No newline at end of file +} diff --git a/Mine2d/game/frontend/events/DebugInput.cs b/Mine2d/game/frontend/events/DebugInput.cs index bcba761..f1c5571 100644 --- a/Mine2d/game/frontend/events/DebugInput.cs +++ b/Mine2d/game/frontend/events/DebugInput.cs @@ -1,27 +1,119 @@ using Mine2d.engine.system; using Mine2d.engine.system.annotations; +using Mine2d.game.backend.network.packets; using Mine2d.game.core; using Mine2d.game.core.data; using Mine2d.game.core.data.entities; +using Mine2d.game.frontend.inventory; namespace Mine2d.game.frontend.events; public class DebugInput { - [EventListener(EventType.KeyDown)] + [EventListener(EventType.KeyDown, Priority = EventPriority.Important)] public static void OnKeyDown(SDL_Event e) { - if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_K) + if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_F10) { - var gameState = Context.Get().GameState; - var player = PlayerEntity.GetSelf(); - var chunk = gameState.World.GetChunkAt(player.Position); - var item = new ItemEntity + if (Context.Get().FrontendGameState.OpenInventory == InventoryKind.None) { - Position = player.Position, - ItemId = ItemId.Stone - }; - chunk.AddEntity(item); + Context.Get().FrontendGameState.OpenInventory = InventoryKind.DebugConsole; + } + else + { + Context.Get().FrontendGameState.OpenInventory = InventoryKind.None; + } + } + if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_BACKSPACE) + { + var frontendGameState = Context.Get().FrontendGameState; + if (frontendGameState.OpenInventory == InventoryKind.DebugConsole) + { + if (frontendGameState.DebugState.ConsoleInput.Length > 0) + { + frontendGameState.DebugState.ConsoleInput = frontendGameState.DebugState.ConsoleInput[0..^1]; + } + } + } + if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_RETURN) + { + var frontendGameState = Context.Get().FrontendGameState; + if (frontendGameState.OpenInventory == InventoryKind.DebugConsole) + { + frontendGameState.DebugState.Messages.Enqueue("> " + frontendGameState.DebugState.ConsoleInput); + var split = frontendGameState.DebugState.ConsoleInput.Split(' '); + var Command = split[0].ToLower() switch + { + "give" => DebugCommand.Give, + "help" => DebugCommand.Help, + "noclip" => DebugCommand.NoClip, + _ => DebugCommand.Unknown + }; + var Args = split.Length > 1 ? split[1..] : Array.Empty(); + + Context.Get().Backend.ProcessPacket(new DebugCommandPacket + { + Command = Command, + Args = Args, + RawCommand = split[0] + }); + frontendGameState.DebugState.ConsoleHistory.Add(frontendGameState.DebugState.ConsoleInput); + frontendGameState.DebugState.ConsoleHistoryIndex = frontendGameState.DebugState.ConsoleHistory.Count; + frontendGameState.DebugState.ConsoleInput = ""; + } + } + if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_UP) + { + var frontendGameState = Context.Get().FrontendGameState; + if (frontendGameState.OpenInventory == InventoryKind.DebugConsole) + { + if (frontendGameState.DebugState.ConsoleHistoryIndex > 0) + { + frontendGameState.DebugState.ConsoleHistoryIndex--; + if (frontendGameState.DebugState.ConsoleHistoryIndex < frontendGameState.DebugState.ConsoleHistory.Count) + frontendGameState.DebugState.ConsoleInput = frontendGameState.DebugState.ConsoleHistory[frontendGameState.DebugState.ConsoleHistoryIndex]; + } + } + } + if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_DOWN) + { + var frontendGameState = Context.Get().FrontendGameState; + if (frontendGameState.OpenInventory == InventoryKind.DebugConsole) + { + if (frontendGameState.DebugState.ConsoleHistoryIndex < frontendGameState.DebugState.ConsoleHistory.Count) + { + frontendGameState.DebugState.ConsoleHistoryIndex++; + if (frontendGameState.DebugState.ConsoleHistoryIndex < frontendGameState.DebugState.ConsoleHistory.Count) + frontendGameState.DebugState.ConsoleInput = frontendGameState.DebugState.ConsoleHistory[frontendGameState.DebugState.ConsoleHistoryIndex]; + else if (frontendGameState.DebugState.ConsoleHistoryIndex == frontendGameState.DebugState.ConsoleHistory.Count && frontendGameState.DebugState.ConsoleHistory[frontendGameState.DebugState.ConsoleHistoryIndex - 1] == frontendGameState.DebugState.ConsoleInput) + frontendGameState.DebugState.ConsoleInput = ""; + } + } + } + } + + [EventListener(EventType.TextInput)] + public static void OnTextInput(SDL_Event e) + { + var frontendGameState = Context.Get().FrontendGameState; + if (frontendGameState.OpenInventory == InventoryKind.DebugConsole) + { + unsafe + { + var bytes = e.text.text; + var text = System.Text.Encoding.UTF8.GetString(bytes, 32); + frontendGameState.DebugState.ConsoleInput += text[0]; + } + } + } + + [EventListener(EventType.Tick)] + public static void OnTick() + { + var frontendGameState = Context.Get().FrontendGameState; + while (frontendGameState.DebugState.Messages.Count > 30) + { + var _ = frontendGameState.DebugState.Messages.Dequeue(); } } } diff --git a/Mine2d/game/frontend/events/InventoryInput.cs b/Mine2d/game/frontend/events/InventoryInput.cs index 43156d0..b22a9ca 100644 --- a/Mine2d/game/frontend/events/InventoryInput.cs +++ b/Mine2d/game/frontend/events/InventoryInput.cs @@ -1,6 +1,7 @@ using Mine2d.engine; using Mine2d.engine.system; using Mine2d.engine.system.annotations; +using Mine2d.game.frontend.inventory; using Mine2d.game.state; namespace Mine2d.game.frontend.events; diff --git a/Mine2d/game/frontend/inventory/DebugConsoleInventory.cs b/Mine2d/game/frontend/inventory/DebugConsoleInventory.cs new file mode 100644 index 0000000..07c8a07 --- /dev/null +++ b/Mine2d/game/frontend/inventory/DebugConsoleInventory.cs @@ -0,0 +1,29 @@ +namespace Mine2d.game.frontend.inventory; + +public class DebugConsoleInventory : Inventory +{ + public override void Render() + { + var ctx = Context.Get(); + const int uiScale = 1; + var windowHeight = ctx.FrontendGameState.WindowHeight; + var history = ctx.FrontendGameState.DebugState.Messages; + var consoleInput = "> " + ctx.FrontendGameState.DebugState.ConsoleInput; + (nint texture, int width, int height, nint surfaceMessage) historyTex = default; + if (history.Count > 0) + { + historyTex = ctx.Renderer.CreateTextTexture(string.Join("\n", history)); + } + var inputTex = ctx.Renderer.CreateTextTexture(consoleInput); + if (history.Count > 0) + { + ctx.Renderer.DrawTexture(historyTex.texture, 0, windowHeight - (inputTex.height * uiScale) - (historyTex.height * uiScale), historyTex.width * uiScale, historyTex.height * uiScale); + } + ctx.Renderer.DrawTexture(inputTex.texture, 0, windowHeight - (inputTex.height * uiScale), inputTex.width * uiScale, inputTex.height * uiScale); + + SDL_DestroyTexture(inputTex.texture); + SDL_DestroyTexture(historyTex.texture); + SDL_FreeSurface(historyTex.surfaceMessage); + SDL_FreeSurface(inputTex.surfaceMessage); + } +} \ No newline at end of file diff --git a/Mine2d/game/frontend/inventory/InventoryConstants.cs b/Mine2d/game/frontend/inventory/InventoryConstants.cs index 42920a9..95c0295 100644 --- a/Mine2d/game/frontend/inventory/InventoryConstants.cs +++ b/Mine2d/game/frontend/inventory/InventoryConstants.cs @@ -1,5 +1,12 @@ namespace Mine2d.game.frontend.inventory; +public enum InventoryKind { + None, + Player, + Workbench, + DebugConsole +} + public static class InventoryConstants { public static readonly int ExtraSlotsWidth = 21; diff --git a/Mine2d/game/frontend/inventory/InventoryRegistry.cs b/Mine2d/game/frontend/inventory/InventoryRegistry.cs index 0a68343..6611532 100644 --- a/Mine2d/game/frontend/inventory/InventoryRegistry.cs +++ b/Mine2d/game/frontend/inventory/InventoryRegistry.cs @@ -10,6 +10,7 @@ public class InventoryRegistry { this.inventoryRenderers.Add(InventoryKind.Player, new PlayerInventoryRenderer()); this.inventoryRenderers.Add(InventoryKind.Workbench, new WorkbenchInventory()); + this.inventoryRenderers.Add(InventoryKind.DebugConsole, new DebugConsoleInventory()); } public Inventory GetInventory(InventoryKind inventory) diff --git a/Mine2d/game/frontend/renderer/InventoryRenderer.cs b/Mine2d/game/frontend/renderer/InventoryRenderer.cs index c49f788..59e338f 100644 --- a/Mine2d/game/frontend/renderer/InventoryRenderer.cs +++ b/Mine2d/game/frontend/renderer/InventoryRenderer.cs @@ -1,40 +1,39 @@ using Mine2d.engine; using Mine2d.game.core; -using Mine2d.game.state; +using Mine2d.game.frontend.inventory; -namespace Mine2d.game.frontend.renderer +namespace Mine2d.game.frontend.renderer; + +public class InventoryRenderer : IRenderer { - public class InventoryRenderer : IRenderer + public void Render() { - public void Render() + var ctx = Context.Get(); + var inventory = ctx.FrontendGameState.OpenInventory; + if (inventory == InventoryKind.None) return; + ctx.Renderer.SetColor(0, 0, 0, 200); + ctx.Renderer.SetDrawBlendMode(SDL_BlendMode.SDL_BLENDMODE_BLEND); + ctx.Renderer.DrawRect(0, 0, ctx.FrontendGameState.WindowWidth, ctx.FrontendGameState.WindowHeight); + var inventoryRenderer = ctx.InventoryRegistry.GetInventory(inventory); + inventoryRenderer.Render(); + var player = PlayerEntity.GetSelf(); + if (player.Inventory.Cursor != null) { - var ctx = Context.Get(); - var inventory = ctx.FrontendGameState.OpenInventory; - if (inventory == InventoryKind.None) return; - ctx.Renderer.SetColor(0, 0, 0, 200); - ctx.Renderer.SetDrawBlendMode(SDL_BlendMode.SDL_BLENDMODE_BLEND); - ctx.Renderer.DrawRect(0, 0, ctx.FrontendGameState.WindowWidth, ctx.FrontendGameState.WindowHeight); - var inventoryRenderer = ctx.InventoryRegistry.GetInventory(inventory); - inventoryRenderer.Render(); - var player = PlayerEntity.GetSelf(); - if (player.Inventory.Cursor != null) - { - var cursorPosition = ctx.FrontendGameState.CursorPosition; - var itemTexture = player.Inventory.Cursor.GetTexture(); - var uiScale = ctx.FrontendGameState.Settings.UiScale; - ctx.Renderer.DrawTexture( - itemTexture, - (int)(cursorPosition.X + 2), - (int)(cursorPosition.Y + 2), - 16 * uiScale, - 16 * uiScale - ); - ctx.Renderer.DrawText( - "" + player.Inventory.Cursor.Count, - (int)(cursorPosition.X + 2), - (int)(cursorPosition.Y + (12 * uiScale)) - ); - } + var cursorPosition = ctx.FrontendGameState.CursorPosition; + var itemTexture = player.Inventory.Cursor.GetTexture(); + var uiScale = ctx.FrontendGameState.Settings.UiScale; + ctx.Renderer.DrawTexture( + itemTexture, + (int)(cursorPosition.X + 2), + (int)(cursorPosition.Y + 2), + 16 * uiScale, + 16 * uiScale + ); + ctx.Renderer.DrawText( + "" + player.Inventory.Cursor.Count, + (int)(cursorPosition.X + 2), + (int)(cursorPosition.Y + (12 * uiScale)) + ); } } } \ No newline at end of file diff --git a/Mine2d/game/state/FrontendGameState.cs b/Mine2d/game/state/FrontendGameState.cs index 4bfec6b..b930724 100644 --- a/Mine2d/game/state/FrontendGameState.cs +++ b/Mine2d/game/state/FrontendGameState.cs @@ -1,12 +1,8 @@ using Mine2d.game.core; +using Mine2d.game.frontend.inventory; namespace Mine2d.game.state; -public enum InventoryKind { - None, - Player, - Workbench -} public class FrontendGameState { @@ -23,6 +19,15 @@ public class FrontendGameState public string Tooltip { get; set; } = "Test"; public InventoryKind OpenInventory { get; set; } = InventoryKind.None; public InputState InputState { get; set; } = new(); + public DebugState DebugState { get; set; } = new(); +} + +public class DebugState { + public Queue Messages { get; set; } = new(); + public string ConsoleInput { get; set; } = ""; + public List ConsoleHistory { get; set; } = new(); + public int ConsoleHistoryIndex { get; set; } = 0; + public bool NoClip { get; set; } = false; } public class Settings