From cf9e6eb072a5a3d098f8d5590b30b8ce3cc09a58 Mon Sep 17 00:00:00 2001 From: MasterGordon Date: Mon, 2 Jan 2023 01:09:11 +0100 Subject: [PATCH] improved inventory rendering --- Mine2d/assets/hud/player-inventory.png | Bin 797 -> 812 bytes Mine2d/assets/items/basic-pickaxe.png | Bin 0 -> 263 bytes Mine2d/assets/items/pickaxe-basic.png | Bin 0 -> 252 bytes Mine2d/engine/Publisher.cs | 1 - Mine2d/game/backend/interactor/Breaking.cs | 3 +- .../{Place.cs => PlayerInteract.cs} | 10 +- .../backend/network/packets/PacketType.cs | 2 +- .../backend/network/packets/PlacePacket.cs | 4 +- Mine2d/game/core/InventoryUtils.cs | 2 +- Mine2d/game/core/data/ItemId.cs | 1 + Mine2d/game/core/data/ItemStack.cs | 12 + Mine2d/game/core/items/Item.cs | 5 + Mine2d/game/core/items/ItemRegistry.cs | 10 + Mine2d/game/core/items/PickaxeItem.cs | 19 ++ Mine2d/game/core/tiles/Workbench.cs | 21 +- Mine2d/game/core/world/ChunkGenerator.cs | 4 +- Mine2d/game/frontend/ItemRenderer.cs | 54 +++++ Mine2d/game/frontend/events/InventoryInput.cs | 2 +- .../frontend/events/PlayerMovementInput.cs | 19 +- .../game/frontend/events/PlayerPlaceInput.cs | 8 +- .../inventory/PlayerInventoryRenderer.cs | 229 ++++++++---------- Mine2d/game/frontend/renderer/GameRenderer.cs | 2 +- Mine2d/game/frontend/renderer/HudRenderer.cs | 11 +- .../frontend/renderer/InventoryRenderer.cs | 15 +- ...ItemRenderer.cs => ItemEnitityRenderer.cs} | 2 +- Mine2d/game/state/Player.cs | 8 + Mine2d/game/state/PlayerInventory.cs | 35 ++- 27 files changed, 285 insertions(+), 194 deletions(-) create mode 100644 Mine2d/assets/items/basic-pickaxe.png create mode 100644 Mine2d/assets/items/pickaxe-basic.png rename Mine2d/game/backend/interactor/{Place.cs => PlayerInteract.cs} (68%) create mode 100644 Mine2d/game/core/items/PickaxeItem.cs create mode 100644 Mine2d/game/frontend/ItemRenderer.cs rename Mine2d/game/frontend/renderer/{ItemRenderer.cs => ItemEnitityRenderer.cs} (95%) diff --git a/Mine2d/assets/hud/player-inventory.png b/Mine2d/assets/hud/player-inventory.png index fa443bd2141e429c6110208ba714cc07f7e5403f..c94ec2c001a6c208e4c7ac8a8d9d4d38537c818e 100644 GIT binary patch delta 670 zcmbQswuWtjVf`~t7srr_IdAXA=3NdDX}$Q`Bp@=+?Lm0U42Ng-ye(GU8eKnh`Bf)o ztCr<#bM?M9X+o3+dz_uD`NsU!mp;CH+`gRY|IYLAa;sIJhcBJ7DfjlK>@3GZ<7qtE zpdg~hE{Xe;YJI>{67~gAy%V(~fQH}7O%+IJY`9C9T z{frd~Ikx{)56sBDJ~xAx17Mtc7h60Toa{!~Sa!?Nw^199WCJm>D1ex2;g znz*RRAkcBf_mvO&SGMQG{i-{&tjXy8m0RBR?jZutKJi!W`!DnFrTP1JZRHc;s#hrZ z041;5zo@LZ!SnCujq0dHO-66?zHYQxU;2|BYGiY~0?;gw;mz@9OpI+VLpUoI*u?Mi ztz1&~;wr_y9S3mhh$iyS(qY*<85e-mhGc{zmlR#`pVkCJQo3))%|8 zPPyahwe&`a!1D*<9Gh2y9S;tJ^IIUCTCE)q?g;O@9;0_Fq~O?6_h;)ZPx$~@7u<^6 zSkIKin=g;Abx!Q$a`Um>8x~@aa1P{Q?q~Zvb+jxV=fB$}Ao_)ewe(MohDo5~aZ~A2 z&Rqpduk>zP)HG>tv3qRDg^3j+M|ige6rB&9niT99vlW~s+V?vgNUL8FcAWp;*Lt>l kyOP~k|I25%T>ASbuhIphZ4>w|$}j+dr>mdKI;Vst0BZ+8CjbBd literal 797 zcmeAS@N?(olHy`uVBq!ia0vp^SAn>ZgAGWA$Nx_PQjEnx?oJHr&dI!FU|_oL>Eakt zG3V`F?>?bKk+z4y9a2u;xw5i)q!a$%HaHe5;IwBOb8Cq30=>Ue%T9mp3HbQp$=QcN zU(17K_c`!#O3eQo)J^-vDj|CT?F`WPQJ&V>fXtM8|`8*aZ6U%hX|f@F_~b$^>DE|X!p zoEnTP(0q4C-R-~BD7R90YRobg+0Ngqf#w!};BL7I4&mYt-80kL(r<+bJX0=R@v{8) zyxS#km);9L*tyT2<@ddMh+=psA;r?OF0K`KJiS8AM33Nb^v`)*=Ol`DO_E>AT;J)- znm*^Ylk<7DEh`fm4BKoa>Ruc9G&uhiwmo`&)-QAAqB^R*X7vBS8Rs1)`~Y%UoUFvJF-^cUa;fScdJ)zTB)$^=9b*t>)P%69S;1>yRXB? r|L7BvT)z4*}Q$iB}xm09% diff --git a/Mine2d/assets/items/basic-pickaxe.png b/Mine2d/assets/items/basic-pickaxe.png new file mode 100644 index 0000000000000000000000000000000000000000..34560dce2758853ec34a49ce3433bac622b25177 GIT binary patch literal 263 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|4tlychFJ6_ zCrD%*=&BMQ?FLtpO%;H={Y@(3hu7Pm;mOgdfB2>O`F)9s`JIMka+XZt zr(4c8WUkILw5#~n`|#%L^E~|X>YKZ@J&Fu#rd72*GMW>2;Q1o<^ZO6(ZNAES!^9q_ ztg-j0ew$4k^90kwdtA3LMkZ$cU+PjBIOp_%0|!35<~L4cPB62t{wtY%8 literal 0 HcmV?d00001 diff --git a/Mine2d/assets/items/pickaxe-basic.png b/Mine2d/assets/items/pickaxe-basic.png new file mode 100644 index 0000000000000000000000000000000000000000..ab8cf484e2df994924532b0c7ae91121ebcadaec GIT binary patch literal 252 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|wt2cZhFJ6_ zCrD%*=&BMQ?FLtpO%;H={Y@(3bN;{PH)fVT9pA=&e!t-dJK^LvHbpFY z$6L-eWUkILw5#~n%h;Ca;5e!K;pQvaBGVs!VKNg}+|BY#c8%VFWj_yo&gaR|sW*J` zQL15$V6c%#^%u=WWtd4dGq!88{K5%CWtQkwqN`H1N&_y43piT y887no>ije*#rO{Q6RX)1V`dvN=-FSE0eU4dmFe#~k?BAmFnGH9xvX p.Id == packet.PlayerGuid); diff --git a/Mine2d/game/backend/network/packets/PacketType.cs b/Mine2d/game/backend/network/packets/PacketType.cs index 3fee7a7..dde97e6 100644 --- a/Mine2d/game/backend/network/packets/PacketType.cs +++ b/Mine2d/game/backend/network/packets/PacketType.cs @@ -10,7 +10,7 @@ public enum PacketType Break, PlayerMoved, BlockBroken, - Place, + PlayerInteract, Jump, DebugCommand } diff --git a/Mine2d/game/backend/network/packets/PlacePacket.cs b/Mine2d/game/backend/network/packets/PlacePacket.cs index ce25ef5..e793542 100644 --- a/Mine2d/game/backend/network/packets/PlacePacket.cs +++ b/Mine2d/game/backend/network/packets/PlacePacket.cs @@ -1,8 +1,8 @@ namespace Mine2d.game.backend.network.packets; -public class PlacePacket : Packet +public class PlayerInteractPacket : Packet { - public override PacketType Type => PacketType.Place; + public override PacketType Type => PacketType.PlayerInteract; public Guid PlayerGuid { get; init; } public Vector2 Target { get; init; } diff --git a/Mine2d/game/core/InventoryUtils.cs b/Mine2d/game/core/InventoryUtils.cs index cfcdec1..dc45a06 100644 --- a/Mine2d/game/core/InventoryUtils.cs +++ b/Mine2d/game/core/InventoryUtils.cs @@ -8,7 +8,7 @@ public static class InventoryUtils { for (var i = 0; i < inventory.Length; i++) { - if (inventory[i] != null && inventory[i].Id == id) + if (inventory[i] != null && inventory[i].Id == id && inventory[i].IsStackable()) { return i; } diff --git a/Mine2d/game/core/data/ItemId.cs b/Mine2d/game/core/data/ItemId.cs index 0bde656..fa28294 100644 --- a/Mine2d/game/core/data/ItemId.cs +++ b/Mine2d/game/core/data/ItemId.cs @@ -16,4 +16,5 @@ public enum ItemId RawTungsten = 115, RawUranium = 116, Diamond = 117, + PickaxeBasic = 200, } diff --git a/Mine2d/game/core/data/ItemStack.cs b/Mine2d/game/core/data/ItemStack.cs index bd2aeed..7ec90b1 100644 --- a/Mine2d/game/core/data/ItemStack.cs +++ b/Mine2d/game/core/data/ItemStack.cs @@ -1,3 +1,5 @@ +using Mine2d.game.core.items; + namespace Mine2d.game.core.data; public class ItemStack @@ -24,4 +26,14 @@ public class ItemStack { return Context.Get().ItemRegistry.GetItem(this.Id).Name; } + + public bool IsStackable() + { + return Context.Get().ItemRegistry.GetItem(this.Id).GetKind() == ItemKind.Default; + } + + public ItemKind GetKind() + { + return Context.Get().ItemRegistry.GetItem(this.Id).GetKind(); + } } diff --git a/Mine2d/game/core/items/Item.cs b/Mine2d/game/core/items/Item.cs index 2547290..10a176a 100644 --- a/Mine2d/game/core/items/Item.cs +++ b/Mine2d/game/core/items/Item.cs @@ -40,4 +40,9 @@ public class Item public virtual void Interact(ItemStack stack, Vector2 position, Player player) { } + + public virtual ItemKind GetKind() + { + return ItemKind.Default; + } } diff --git a/Mine2d/game/core/items/ItemRegistry.cs b/Mine2d/game/core/items/ItemRegistry.cs index a74deb5..57572e8 100644 --- a/Mine2d/game/core/items/ItemRegistry.cs +++ b/Mine2d/game/core/items/ItemRegistry.cs @@ -2,6 +2,15 @@ using Mine2d.game.core.data; namespace Mine2d.game.core.items; +public enum ItemKind +{ + Default, + Pickaxe, + Helmet, + Chestplate, + Leggings, +} + public class ItemRegistry { private readonly Dictionary items = new(); @@ -21,6 +30,7 @@ public class ItemRegistry this.Register(ItemId.RawTungsten, new Item(ItemId.RawTungsten, "Raw Tungsten", "items.raw-tungsten" )); this.Register(ItemId.RawUranium, new Item(ItemId.RawUranium, "Raw Uranium", "items.raw-uranium" )); this.Register(ItemId.Diamond, new Item(ItemId.Diamond, "Diamond", "items.diamond" )); + this.Register(ItemId.PickaxeBasic, new PickaxeItem(ItemId.PickaxeBasic, "Basic Pickaxe", "items.pickaxe-basic")); } public void Register(ItemId id, Item item) diff --git a/Mine2d/game/core/items/PickaxeItem.cs b/Mine2d/game/core/items/PickaxeItem.cs new file mode 100644 index 0000000..e7455ee --- /dev/null +++ b/Mine2d/game/core/items/PickaxeItem.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Mine2d.game.core.data; + +namespace Mine2d.game.core.items; + +public class PickaxeItem : Item +{ + public PickaxeItem(ItemId id, string name, string textureName) : base(id, name, textureName) + { + } + + public override ItemKind GetKind() + { + return ItemKind.Pickaxe; + } +} \ No newline at end of file diff --git a/Mine2d/game/core/tiles/Workbench.cs b/Mine2d/game/core/tiles/Workbench.cs index 3127ea8..957cd4c 100644 --- a/Mine2d/game/core/tiles/Workbench.cs +++ b/Mine2d/game/core/tiles/Workbench.cs @@ -1,16 +1,15 @@ using Mine2d.game.core.data; -namespace Mine2d.game.core.tiles -{ - public class Workbench : Tile - { - public Workbench(string name, string texturePath, int hardness) : base(name, texturePath, hardness, ItemId.Workbench) - { - } +namespace Mine2d.game.core.tiles; - public override bool IsSolid() - { - return false; - } +public class Workbench : Tile +{ + public Workbench(string name, string texturePath, int hardness) : base(name, texturePath, hardness, ItemId.Workbench) + { + } + + public override bool IsSolid() + { + return false; } } \ No newline at end of file diff --git a/Mine2d/game/core/world/ChunkGenerator.cs b/Mine2d/game/core/world/ChunkGenerator.cs index aad5b7e..328a5b1 100644 --- a/Mine2d/game/core/world/ChunkGenerator.cs +++ b/Mine2d/game/core/world/ChunkGenerator.cs @@ -21,6 +21,7 @@ public class ChunkGenerator } public static readonly OpenSimplexNoise Noise = new(); + public static readonly OpenSimplexNoise Noise2 = new(); public static Chunk CreateChunk(int x, int y) { var fill = new STile @@ -33,8 +34,9 @@ public class ChunkGenerator for (var j = 0; j < Constants.ChunkSize; j++) { var n = (Noise.coherentNoise(i + (x * 32), j + (y * 32), 0, 1, 25, 0.5f, 0.9f)); + var n2 = (Noise2.coherentNoise(i + (x * 32), j + (y * 32), 0, 1, 25, 0.5f, 0.9f)); // Console.WriteLine(i * (x * 32) + " "+ j * (y * 32)); - if (n > 0.08) continue; + if (n > 0.08 || n2 > 0.08) continue; chunk.SetTile(i, j, fill); } } diff --git a/Mine2d/game/frontend/ItemRenderer.cs b/Mine2d/game/frontend/ItemRenderer.cs new file mode 100644 index 0000000..9a9e2e0 --- /dev/null +++ b/Mine2d/game/frontend/ItemRenderer.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Mine2d.game.core.data; + +namespace Mine2d.game.frontend; + +public static class ItemRenderer +{ + private static readonly Dictionary Textures = new(); + + public static void RenderItemStack(ItemStack stack, Vector2 position, bool renderTooltip = true) + { + var texture = stack.GetTexture(); + var renderer = Context.Get().Renderer; + var uiScale = Context.Get().FrontendGameState.Settings.UiScale; + renderer.DrawTexture(texture, (int)position.X, (int)position.Y, 16 * uiScale, 16 * uiScale); + if (stack.Count > 1) + { + var (textTexture, width, height) = GetCountTexture(stack.Count); + renderer.DrawTexture( + textTexture, + (int)position.X + (16 * uiScale) - width - (1 * uiScale), + (int)position.Y + (16 * uiScale) - height + (1 * uiScale), + width, + height + ); + } + if (renderTooltip) + { + var cursorPosition = Context.Get().FrontendGameState.CursorPosition; + if (cursorPosition.X >= position.X + && cursorPosition.X <= position.X + (16 * uiScale) + && cursorPosition.Y >= position.Y + && cursorPosition.Y <= position.Y + (16 * uiScale) + ) + { + Context.Get().FrontendGameState.Tooltip = stack.GetName(); + } + } + } + + public static (IntPtr, int, int) GetCountTexture(int count) + { + if (Textures.TryGetValue(count, out var value)) + { + return value; + } + var (textTexture, width, height, _) = Context.Get().Renderer.CreateTextTexture("" + count); + Textures.Add(count, (textTexture, width, height)); + return (textTexture, width, height); + } +} \ No newline at end of file diff --git a/Mine2d/game/frontend/events/InventoryInput.cs b/Mine2d/game/frontend/events/InventoryInput.cs index b22a9ca..4ecc29d 100644 --- a/Mine2d/game/frontend/events/InventoryInput.cs +++ b/Mine2d/game/frontend/events/InventoryInput.cs @@ -65,7 +65,7 @@ public class InventoryInput } [EventListener(EventType.KeyUp, EventPriority.Highest)] - public static void OnKeyUpOpenInventory(SDL_Event e) + public static void OnKeyUpOpenInventory(SDL_Event _) { var frontendGameState = Context.Get().FrontendGameState; if(frontendGameState.OpenInventory != InventoryKind.None) diff --git a/Mine2d/game/frontend/events/PlayerMovementInput.cs b/Mine2d/game/frontend/events/PlayerMovementInput.cs index 5dccb33..847c940 100644 --- a/Mine2d/game/frontend/events/PlayerMovementInput.cs +++ b/Mine2d/game/frontend/events/PlayerMovementInput.cs @@ -6,18 +6,17 @@ using Mine2d.engine.system; using Mine2d.engine.system.annotations; using Mine2d.game.backend.network.packets; -namespace Mine2d.game.frontend.events +namespace Mine2d.game.frontend.events; + +public class PlayerMovementInput { - public class PlayerMovementInput + [EventListener(EventType.KeyDown)] + public static void onKeyDown(SDL_Event e) { - [EventListener(EventType.KeyDown)] - public static void onKeyDown(SDL_Event e) - { - if(e.key.keysym.sym == SDL_Keycode.SDLK_SPACE) { - Context.Get().Backend.ProcessPacket(new JumpPacket { - PlayerId = Context.Get().FrontendGameState.PlayerGuid - }); - } + if(e.key.keysym.sym == SDL_Keycode.SDLK_SPACE) { + Context.Get().Backend.ProcessPacket(new JumpPacket { + PlayerId = Context.Get().FrontendGameState.PlayerGuid + }); } } } \ No newline at end of file diff --git a/Mine2d/game/frontend/events/PlayerPlaceInput.cs b/Mine2d/game/frontend/events/PlayerPlaceInput.cs index f5d825f..f1af560 100644 --- a/Mine2d/game/frontend/events/PlayerPlaceInput.cs +++ b/Mine2d/game/frontend/events/PlayerPlaceInput.cs @@ -4,7 +4,7 @@ using Mine2d.game.backend.network.packets; namespace Mine2d.game.frontend.events; -public class PlayerPlaceInput +public class PlayerInteractInput { [EventListener(EventType.MouseButtonDown)] public static void OnMouseDown(SDL_Event e) @@ -15,11 +15,11 @@ public class PlayerPlaceInput } var ctx = Context.Get(); - var amp = ctx.FrontendGameState.CursorPosition - / ctx.FrontendGameState.Settings.GameScale + var amp = (ctx.FrontendGameState.CursorPosition + / ctx.FrontendGameState.Settings.GameScale) + ctx.FrontendGameState.Camera.Position; - ctx.Backend.ProcessPacket(new PlacePacket + ctx.Backend.ProcessPacket(new PlayerInteractPacket { PlayerGuid = ctx.FrontendGameState.PlayerGuid, Target = amp, diff --git a/Mine2d/game/frontend/inventory/PlayerInventoryRenderer.cs b/Mine2d/game/frontend/inventory/PlayerInventoryRenderer.cs index 4a7400c..209e8c4 100644 --- a/Mine2d/game/frontend/inventory/PlayerInventoryRenderer.cs +++ b/Mine2d/game/frontend/inventory/PlayerInventoryRenderer.cs @@ -1,154 +1,133 @@ using Mine2d.game.core; -namespace Mine2d.game.frontend.inventory +namespace Mine2d.game.frontend.inventory; + +public class PlayerInventoryRenderer : Inventory { + private IntPtr texture = IntPtr.Zero; + private int x, y; - public class PlayerInventoryRenderer : Inventory + public override void Render() { - private IntPtr texture = IntPtr.Zero; - private int x, y; - - public override void Render() + var ctx = Context.Get(); + if (this.texture == IntPtr.Zero) { - var ctx = Context.Get(); - if (this.texture == IntPtr.Zero) - { - this.texture = ctx.TextureFactory.LoadTexture("hud.player-inventory"); - } - var (width, height) = ctx.Renderer.GetTextureSize(this.texture); - var uiScale = ctx.FrontendGameState.Settings.UiScale; - width *= uiScale; - height *= uiScale; - var extraSlotsWidth = InventoryConstants.ExtraSlotsWidth * uiScale; - var (windowWidth, windowHeight) = (ctx.FrontendGameState.WindowWidth, ctx.FrontendGameState.WindowHeight); - this.x = (windowWidth - (width - extraSlotsWidth)) / 2; - this.y = (windowHeight - height) / 2; - ctx.Renderer.DrawTexture(this.texture, this.x, this.y, width, height); + this.texture = ctx.TextureFactory.LoadTexture("hud.player-inventory"); + } + var (width, height) = ctx.Renderer.GetTextureSize(this.texture); + var uiScale = ctx.FrontendGameState.Settings.UiScale; + width *= uiScale; + height *= uiScale; + var extraSlotsWidth = InventoryConstants.ExtraSlotsWidth * uiScale; + var (windowWidth, windowHeight) = (ctx.FrontendGameState.WindowWidth, ctx.FrontendGameState.WindowHeight); + this.x = (windowWidth - (width - extraSlotsWidth)) / 2; + this.y = (windowHeight - height) / 2; + ctx.Renderer.DrawTexture(this.texture, this.x, this.y, width, height); - var player = PlayerEntity.GetSelf(); - var inventory = player.Inventory.Inventory; + var player = PlayerEntity.GetSelf(); + var inventory = player.Inventory.Inventory; + var hotbar = player.Inventory.Hotbar; + for (var i = 0; i < hotbar.Length; i++) + { + var stack = hotbar[i]; + if (stack == null) + { + continue; + } + ItemRenderer.RenderItemStack(stack, new Vector2(((4 + (i * 21)) * uiScale) + this.x, (4 * uiScale) + this.y), player.Inventory.Cursor == null); + } + for (var i = 0; i < inventory.Length; i++) + { + var stack = inventory[i]; + if (stack == null) + { + continue; + } + + ItemRenderer.RenderItemStack(stack, new Vector2(((4 + ((i % 9) * 21)) * uiScale) + this.x, ((4 + 21 + ((i / 9) * 21)) * uiScale) + this.y), player.Inventory.Cursor == null); + } + var pickaxe = player.Inventory.Pickaxe; + if (pickaxe != null) + { + ItemRenderer.RenderItemStack(pickaxe, new Vector2(((4 + (9 * 21)) * uiScale) + this.x, (((5 * 21) + 4) * uiScale) + this.y), player.Inventory.Cursor == null); + } + } + + public override void OnClick(SDL_Event e) + { + var cursorPosition = Context.Get().FrontendGameState.CursorPosition; + var player = PlayerEntity.GetSelf(); + // is hotbar + if (cursorPosition.X >= this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale) + && cursorPosition.X <= this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 9 * Context.Get().FrontendGameState.Settings.UiScale) + && cursorPosition.Y >= this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + && cursorPosition.Y <= this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * Context.Get().FrontendGameState.Settings.UiScale) + ) + { var hotbar = player.Inventory.Hotbar; - var cursorPosition = Context.Get().FrontendGameState.CursorPosition; - for (var i = 0; i < hotbar.Length; i++) + var index = (int)((cursorPosition.X - (this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale))) / (21 * Context.Get().FrontendGameState.Settings.UiScale)); + if (e.button.button == SDL_BUTTON_LEFT) { - var stack = hotbar[i]; - if (stack == null) - { - continue; - } - - var itemTexture = stack.GetTexture(); - ctx.Renderer.DrawTexture( - itemTexture, - ((4 + (i * 21)) * uiScale) + this.x, - (4 * uiScale) + this.y, - 16 * uiScale, - 16 * uiScale - ); - ctx.Renderer.DrawText( - "" + stack.Count, - ((4 + (i * 21)) * uiScale) + this.x, - (14 * uiScale) + this.y - ); - if (player.Inventory.Cursor == null && - cursorPosition.X >= ((4 + (i * 21)) * uiScale) + this.x - && cursorPosition.X <= ((4 + (i * 21)) * uiScale) + this.x + (16 * uiScale) - && cursorPosition.Y >= (4 * uiScale) + this.y - && cursorPosition.Y <= (4 * uiScale) + this.y + (16 * uiScale) - ) - { - Context.Get().FrontendGameState.Tooltip = stack.GetName(); - } + player.Inventory.SwapWithCursor(index, hotbar); } - for (var i = 0; i < inventory.Length; i++) + if (e.button.button == SDL_BUTTON_RIGHT) { - var stack = inventory[i]; - if (stack == null) + if (player.Inventory.Cursor == null) { - continue; + player.Inventory.TakeHalf(index, hotbar); } - - var itemTexture = stack.GetTexture(); - ctx.Renderer.DrawTexture( - itemTexture, - ((4 + ((i % 9) * 21)) * uiScale) + this.x, - ((4 + 21 + ((i / 9) * 21)) * uiScale) + this.y, - 16 * uiScale, - 16 * uiScale - ); - ctx.Renderer.DrawText( - "" + stack.Count, - ((4 + ((i % 9) * 21)) * uiScale) + this.x, - ((14 + 21 + ((i / 9) * 21)) * uiScale) + this.y - ); - if (player.Inventory.Cursor == null && - cursorPosition.X >= ((4 + ((i % 9) * 21)) * uiScale) + this.x - && cursorPosition.X <= ((4 + ((i % 9) * 21)) * uiScale) + this.x + (16 * uiScale) - && cursorPosition.Y >= ((4 + 21 + ((i / 9) * 21)) * uiScale) + this.y - && cursorPosition.Y <= ((4 + 21 + ((i / 9) * 21)) * uiScale) + this.y + (16 * uiScale) - ) + else { - Context.Get().FrontendGameState.Tooltip = stack.GetName(); + player.Inventory.DropOne(index, hotbar); } } } - - public override void OnClick(SDL_Event e) + if (cursorPosition.X >= this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale) + && cursorPosition.X <= this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 9 * Context.Get().FrontendGameState.Settings.UiScale) + && cursorPosition.Y >= this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * Context.Get().FrontendGameState.Settings.UiScale) + && cursorPosition.Y <= this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 6 * Context.Get().FrontendGameState.Settings.UiScale) + ) { - var cursorPosition = Context.Get().FrontendGameState.CursorPosition; - // is hotbar - if (cursorPosition.X >= this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale) - && cursorPosition.X <= this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 9 * Context.Get().FrontendGameState.Settings.UiScale) - && cursorPosition.Y >= this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale) - && cursorPosition.Y <= this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * Context.Get().FrontendGameState.Settings.UiScale) - ) + var inventory = player.Inventory.Inventory; + var index = (int)((cursorPosition.X - (this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale))) / (21 * Context.Get().FrontendGameState.Settings.UiScale)) + + ((int)((cursorPosition.Y - (this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * Context.Get().FrontendGameState.Settings.UiScale))) / (21 * Context.Get().FrontendGameState.Settings.UiScale)) * 9); + if (e.button.button == SDL_BUTTON_LEFT) { - var player = PlayerEntity.GetSelf(); - var hotbar = player.Inventory.Hotbar; - var index = (int)((cursorPosition.X - (this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale))) / (21 * Context.Get().FrontendGameState.Settings.UiScale)); - if (e.button.button == SDL_BUTTON_LEFT) + player.Inventory.SwapWithCursor(index, inventory); + } + if (e.button.button == SDL_BUTTON_RIGHT) + { + if (player.Inventory.Cursor == null) { - player.Inventory.SwapWithCursor(index, hotbar); + player.Inventory.TakeHalf(index, inventory); } - if (e.button.button == SDL_BUTTON_RIGHT) + else { - if (player.Inventory.Cursor == null) - { - player.Inventory.TakeHalf(index, hotbar); - } - else - { - player.Inventory.DropOne(index, hotbar); - } + player.Inventory.DropOne(index, inventory); } } - // is inventory - if (cursorPosition.X >= this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale) - && cursorPosition.X <= this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 9 * Context.Get().FrontendGameState.Settings.UiScale) - && cursorPosition.Y >= this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * Context.Get().FrontendGameState.Settings.UiScale) - && cursorPosition.Y <= this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 6 * Context.Get().FrontendGameState.Settings.UiScale) - ) + } + if (IsCursorOnSlot(((4 + (9 * 21)) * Context.Get().FrontendGameState.Settings.UiScale) + this.x, (((5 * 21) + 4) * Context.Get().FrontendGameState.Settings.UiScale) + this.y)) + { + if (e.button.button == SDL_BUTTON_LEFT) { - var player = PlayerEntity.GetSelf(); - var inventory = player.Inventory.Inventory; - var index = (int)((cursorPosition.X - (this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale))) / (21 * Context.Get().FrontendGameState.Settings.UiScale)) - + ((int)((cursorPosition.Y - (this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * Context.Get().FrontendGameState.Settings.UiScale))) / (21 * Context.Get().FrontendGameState.Settings.UiScale)) * 9); - if (e.button.button == SDL_BUTTON_LEFT) - { - player.Inventory.SwapWithCursor(index, inventory); - } - if (e.button.button == SDL_BUTTON_RIGHT) - { - if (player.Inventory.Cursor == null) - { - player.Inventory.TakeHalf(index, inventory); - } - else - { - player.Inventory.DropOne(index, inventory); - } - } + player.Inventory.SwapPickaxeWithCursor(); + } + if (e.button.button == SDL_BUTTON_RIGHT) + { + player.Inventory.SwapPickaxeWithCursor(); } } } + + private static bool IsCursorOnSlot(int x, int y) + { + var cursorPosition = Context.Get().FrontendGameState.CursorPosition; + var width = 21 * Context.Get().FrontendGameState.Settings.UiScale; + var height = 21 * Context.Get().FrontendGameState.Settings.UiScale; + return cursorPosition.X >= x + && cursorPosition.X <= x + width + && cursorPosition.Y >= y + && cursorPosition.Y <= y + height; + } } diff --git a/Mine2d/game/frontend/renderer/GameRenderer.cs b/Mine2d/game/frontend/renderer/GameRenderer.cs index cb60643..ae57412 100644 --- a/Mine2d/game/frontend/renderer/GameRenderer.cs +++ b/Mine2d/game/frontend/renderer/GameRenderer.cs @@ -11,7 +11,7 @@ public class GameRenderer : IRenderer this.renderers.Add(new BackgroundRenderer()); this.renderers.Add(new WorldRenderer()); this.renderers.Add(new PlayerRenderer()); - this.renderers.Add(new ItemRenderer()); + this.renderers.Add(new ItemEnitityRenderer()); this.renderers.Add(new WorldCursorRenderer()); this.renderers.Add(new HudRenderer()); this.renderers.Add(new InventoryRenderer()); diff --git a/Mine2d/game/frontend/renderer/HudRenderer.cs b/Mine2d/game/frontend/renderer/HudRenderer.cs index 248f1a9..e3e42ab 100644 --- a/Mine2d/game/frontend/renderer/HudRenderer.cs +++ b/Mine2d/game/frontend/renderer/HudRenderer.cs @@ -37,13 +37,10 @@ public class HudRenderer : IRenderer var renderer = Context.Get().Renderer; var uiScale = Context.Get().FrontendGameState.Settings.UiScale; var activeSlot = Context.Get().FrontendGameState.HotbarIndex; - // var window = Context.Get().Window; - // var (width, height) = window.GetSize(); var (hotbarWidth, hotbarHeight) = renderer.GetTextureSize(this.hotbarTexture); var player = PlayerEntity.GetSelf(); renderer.DrawTexture(this.hotbarTexture, 0, 0, hotbarWidth * uiScale, hotbarHeight * uiScale); renderer.DrawTexture(this.hotbarActiveTexture, activeSlot * 24 * uiScale, 0, 24 * uiScale, 24 * uiScale); - var cursorPosition = Context.Get().FrontendGameState.CursorPosition; for (var i = 0; i < player?.Inventory.Hotbar.Length; i++) { var stack = player.Inventory.Hotbar[i]; @@ -52,13 +49,7 @@ public class HudRenderer : IRenderer continue; } - var texture = stack.GetTexture(); - renderer.DrawTexture(texture, (4 + (i * 24)) * uiScale, 4 * uiScale, 16 * uiScale, 16 * uiScale); - renderer.DrawText("" + stack.Count, (4 + (i * 24)) * uiScale, 14 * uiScale); - if (cursorPosition.X >= (4 + (i * 24)) * uiScale && cursorPosition.X <= (4 + (i * 24) + 16) * uiScale && cursorPosition.Y >= 4 * uiScale && cursorPosition.Y <= (4 + 16) * uiScale) - { - Context.Get().FrontendGameState.Tooltip = stack.GetName(); - } + ItemRenderer.RenderItemStack(stack, new Vector2((4 + (i * 24)) * uiScale, 4 * uiScale)); } } } diff --git a/Mine2d/game/frontend/renderer/InventoryRenderer.cs b/Mine2d/game/frontend/renderer/InventoryRenderer.cs index 59e338f..b14ee4e 100644 --- a/Mine2d/game/frontend/renderer/InventoryRenderer.cs +++ b/Mine2d/game/frontend/renderer/InventoryRenderer.cs @@ -20,20 +20,7 @@ public class InventoryRenderer : IRenderer 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)) - ); + ItemRenderer.RenderItemStack(player.Inventory.Cursor, cursorPosition + new Vector2(2, 2)); } } } \ No newline at end of file diff --git a/Mine2d/game/frontend/renderer/ItemRenderer.cs b/Mine2d/game/frontend/renderer/ItemEnitityRenderer.cs similarity index 95% rename from Mine2d/game/frontend/renderer/ItemRenderer.cs rename to Mine2d/game/frontend/renderer/ItemEnitityRenderer.cs index 86ac6c8..985f8c7 100644 --- a/Mine2d/game/frontend/renderer/ItemRenderer.cs +++ b/Mine2d/game/frontend/renderer/ItemEnitityRenderer.cs @@ -4,7 +4,7 @@ using Mine2d.game.core.data.entities; namespace Mine2d.game.frontend.renderer; -public class ItemRenderer : IRenderer +public class ItemEnitityRenderer : IRenderer { public void Render() { diff --git a/Mine2d/game/state/Player.cs b/Mine2d/game/state/Player.cs index 49c3cec..55ff8ca 100644 --- a/Mine2d/game/state/Player.cs +++ b/Mine2d/game/state/Player.cs @@ -14,4 +14,12 @@ public class Player { return this.Position + new Vector2(7, -14); } + + public int GetMiningSpeed() { + return 10; + } + + public int GetHarvestLevel() { + return 1; + } } diff --git a/Mine2d/game/state/PlayerInventory.cs b/Mine2d/game/state/PlayerInventory.cs index 511e7e7..23a264d 100644 --- a/Mine2d/game/state/PlayerInventory.cs +++ b/Mine2d/game/state/PlayerInventory.cs @@ -1,5 +1,6 @@ using Mine2d.game.core; using Mine2d.game.core.data; +using Mine2d.game.core.items; namespace Mine2d.game.state; @@ -7,6 +8,10 @@ public class PlayerInventory { public ItemStack[] Hotbar { get; set; } = new ItemStack[9]; public ItemStack[] Inventory { get; set; } = new ItemStack[5 * 9]; + public ItemStack Pickaxe { get; set; } + public ItemStack Helmet { get; set; } + public ItemStack Chestplate { get; set; } + public ItemStack Leggings { get; set; } public ItemStack Cursor { get; set; } public bool PickupItemStack(ItemStack itemStack) @@ -34,7 +39,7 @@ public class PlayerInventory public void SwapWithCursor(int slot, ItemStack[] inventory) { - if (this.Cursor != null && inventory[slot] != null && this.Cursor.Id == inventory[slot].Id) + if (this.Cursor != null && inventory[slot] != null && this.Cursor.Id == inventory[slot].Id && this.Cursor.IsStackable()) { inventory[slot].Count += this.Cursor.Count; this.Cursor = null; @@ -49,7 +54,7 @@ public class PlayerInventory { if (inventory[slot] == null) return; - if(inventory[slot].Count == 1) + if (inventory[slot].Count == 1) { this.Cursor = inventory[slot]; inventory[slot] = null; @@ -65,7 +70,7 @@ public class PlayerInventory { inventory[slot] = new ItemStack(this.Cursor.Id, 1); } - else if (inventory[slot].Id == this.Cursor.Id) + else if (inventory[slot].Id == this.Cursor.Id && this.Cursor.IsStackable()) { inventory[slot].Count++; } @@ -79,4 +84,28 @@ public class PlayerInventory this.Cursor = null; } } + + public void SwapPickaxeWithCursor() + { + if (this.Cursor != null && this.Cursor?.GetKind() != ItemKind.Pickaxe) return; + (this.Pickaxe, this.Cursor) = (this.Cursor, this.Pickaxe); + } + + public void SwapHelmetWithCursor() + { + if (this.Cursor != null && this.Cursor?.GetKind() != ItemKind.Helmet) return; + (this.Helmet, this.Cursor) = (this.Cursor, this.Helmet); + } + + public void SwapChestplateWithCursor() + { + if (this.Cursor != null && this.Cursor?.GetKind() != ItemKind.Chestplate) return; + (this.Chestplate, this.Cursor) = (this.Cursor, this.Chestplate); + } + + public void SwapLeggingsWithCursor() + { + if (this.Cursor != null && this.Cursor?.GetKind() != ItemKind.Leggings) return; + (this.Leggings, this.Cursor) = (this.Cursor, this.Leggings); + } }