From a12b4450c5652d4c7068f5c3b3dde66eb29d63f4 Mon Sep 17 00:00:00 2001 From: MasterGordon Date: Mon, 2 Jan 2023 20:02:11 +0100 Subject: [PATCH] added crafting --- Mine2d/assets/hud/workbench-inventory.png | Bin 1252 -> 1043 bytes Mine2d/assets/items/ingot-gold.png | Bin 0 -> 200 bytes Mine2d/assets/items/ingot-iron.png | Bin 0 -> 191 bytes Mine2d/assets/items/pickaxe-gold.png | Bin 0 -> 242 bytes Mine2d/assets/items/pickaxe-iron.png | Bin 0 -> 229 bytes .../game/backend/interactor/PlayerInteract.cs | 8 ++- Mine2d/game/core/data/ItemId.cs | 5 ++ Mine2d/game/core/data/World.cs | 10 +++ Mine2d/game/core/items/ItemRegistry.cs | 5 ++ Mine2d/game/core/tiles/Tile.cs | 4 ++ Mine2d/game/core/tiles/TileRegistry.cs | 2 + Mine2d/game/core/tiles/Workbench.cs | 63 ++++++++++++++++++ Mine2d/game/core/world/ChunkGenerator.cs | 4 ++ Mine2d/game/frontend/ItemRenderer.cs | 4 +- .../inventory/PlayerInventoryRenderer.cs | 40 ++++++----- .../frontend/inventory/WorkbenchInventory.cs | 58 ++++++++++++++-- .../game/frontend/renderer/PlayerRenderer.cs | 8 --- Mine2d/game/state/FrontendGameState.cs | 2 +- Mine2d/game/state/PlayerInventory.cs | 39 +++++++++++ 19 files changed, 221 insertions(+), 31 deletions(-) create mode 100644 Mine2d/assets/items/ingot-gold.png create mode 100644 Mine2d/assets/items/ingot-iron.png create mode 100644 Mine2d/assets/items/pickaxe-gold.png create mode 100644 Mine2d/assets/items/pickaxe-iron.png diff --git a/Mine2d/assets/hud/workbench-inventory.png b/Mine2d/assets/hud/workbench-inventory.png index dbd4c2803cdf4c6b30a9112945e47cd05e283757..6e31c24b41069c54bde2cc3a65071ed77af91394 100644 GIT binary patch literal 1043 zcmeAS@N?(olHy`uVBq!ia0vp^SAlpl2OE%-dmDKKNHG=%xjQkeJ16s!fr0s~r;B4q z#hkZyZC42=O0+#JcM=e^bnPlQVYi8moBR4i(Vcuis^Hs1C08y%LBYOOH6JIVpO)tn z9>s`rbjro&=IuNArzUgN)5E{Z&$IsD=|6wJN$t7(Rjb54SL!Yob~KczZ8Ke^v2j?L+>_nJF)>f6e-R%f~psGEH9lnue`Fn_q~4nihw!XiF#8As5!fMNI~Qjx$y$ z_+S@av+*Xk^RtujJD3+WZ7BSGc5lwL#AP#A&aB-Kghv+^!we%fU$xq2?;0AqdB*u{ z%egoHv>u$f_S)Iw%a{MS^D%A528l^_Wr4SD$o>1eRpijxm&O{v;A@z@saWjDbWsDA z+WCL8SFMU+NXw|&@29HKw(R`kO>^Hb-!Qw3d4l^Leo7EX!KJ&px8yY}gpMQ8e$^F&ty10-FMScka_tn~|d~+t;3Ot$XbzxG)zEeP#nAJ5JK#a`!leBE+!xr&{rZ!-6lY>94{>-}7PM+pkmDR+m;plolrW|D&&W)xSO#&{y5NI? z?ejj^J}?LbGzO87`w)I(dJO1~sLqei+3SPe3x~Pfoddy`Z zQJKQGu1SFh8TEI*b_&_BTzkdY-|H^8uPSGw{xX|j^2*~1n&+c>ky`M2=-QDx~2aisfq3y1;7??~MJYD@< J);T3K0RTM4&iw!Y literal 1252 zcmVPx(o=HSORCt{2-Q8~7AP|OOQg!eDf_;!4S#=y68<>Ilc^|1#m8QGke0q!ihPV<99K z%7FQX8nMQh{HY;I1g~cZ*BTIdmKfXxq~*P?g3z)AX0+EJB>ssv z$2*zo`lLA^Ru7)^4a=)%Gw#OWEw8T;wzpUEjz6>5V_Bqjqp+3M(5Ut5)aUd0iZn!M z@#Yw1J2oN#v3k%Xmb$!oJD%RVpFqR)KI+kJ8C0q-~;V9ds?6;;5 zu~tuLr5a}DULaFly8>eQ0M*KN_iw%R*(a~o=K+#oj=H4ePInLDEhLu9+pF~kWlxxo zVaODW-Oy5#x690*{*Q*9NqdIXsXa0VV<)~?ab&o%`hy@8I5Tx0B=f}g$&6#e@&1hi zf?ryeomjq{FTqp~0vnzt^HCcA8)+&EJ(0f)h8_f{ARs^m0Rbuq2v9*lFl6=3U2Oye z-J*x16aoSTQ7-Y8LO_5j_d6>>-pKNiNWDiX^dLB?%_}m@InD?Onq&(JLo77Q6p~Qm zMK7#Bxi}I4i}e{1sxlsUTlv}&7R&YMrCuTHXUmy9?bvwKvhlZeS!PqMkhintP9xM2 zHix#dA;(Ny#X9NvinP|(to@$l32?*!JP8+&mHPurv0V0SAqlnhsK(iU70YGMvN2yN z0ecV)qhGyz_479k%6dp#K2Rmy4jTHGK|J`@rv3<9cl*Z@-w-YyC1X zv)tX{eoUGTH^WNU2+R$!?pPgiAvQMwDaGdlQS=bLEqNYfq@h~~g@rO;j^#Z_Z)1!8 z{oJrYJs`DS#Opp$$YXgAQqvj|PoZL%=EIbLNH_G=jmuLIF=kp1^0rY$YA_n*>8zP2n}ZDAR`T&a6rrSfY6xX$&wyKa$hI{2TjYKC~RriSF2(P24-;c$Sr}u z3T7thfJFoZs30Jduwk`NZss5n5DI_jL4XPZ0#wlDzrc6B0{{R3V*UYYsm6b-#T|73 O0000lc?gF+A?kNpvu~;k(lO@O1TaS?83{1OP&rNA&;z literal 0 HcmV?d00001 diff --git a/Mine2d/assets/items/ingot-iron.png b/Mine2d/assets/items/ingot-iron.png new file mode 100644 index 0000000000000000000000000000000000000000..b924e1d266fa3999e26656001d3a9d9db8487509 GIT binary patch literal 191 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|Dm`5sLo9l) zPCPAmK!L->^O(>!@7D`YzdbnTgu;TyhBqg(-<#Zj;(hU4k(IADl)tY{+Q!7sBrwS{ zh)d~(u>fdwm8bSM@kynForMH1i1O+5zR{{H?hDFFl#8)jC4#ET}?Z|GuwzQSXM^i2KHk^0upSRiIg!SLVBQKe9W=)K3 z;`t!eutxHk?jl}?>pkAGVay#@d#*Cn%u2lHAeOEaX1qx;BG#d6^F&5tW@hHYM@!>9 muKjs9NPsuD-4H+J2=a(xqv@Djh5CWcCLI`zsYK4MGxJbypqgO>^n9Evp} zGa`S_>6q}=Ta%$-x_fF`uVIyH?RjI}*HOZH3GYK>C351omOf&t+Rd<~eaRFKhGl{W z4jnsbx%+FdgSMF9Ck1B*eYv^CjQ6`3p9`Ho^35vu--n6oO1AZfxbINPOaA3vQ}Cht c$Fv&^j;aLN`>2RfC()78&qol`;+0JnEp5dZ)H literal 0 HcmV?d00001 diff --git a/Mine2d/game/backend/interactor/PlayerInteract.cs b/Mine2d/game/backend/interactor/PlayerInteract.cs index 9b0bebc..27db3d6 100644 --- a/Mine2d/game/backend/interactor/PlayerInteract.cs +++ b/Mine2d/game/backend/interactor/PlayerInteract.cs @@ -15,8 +15,14 @@ public class PlayerInteract { return; } + if (ctx.GameState.World.HasTileAt(packet.Target)) + { + var tile = ctx.TileRegistry.GetTile(ctx.GameState.World.GetTileAt(packet.Target).Id); + tile.OnInteract(packet.Target); + return; + } var stack = player.Inventory.Hotbar[packet.Slot]; - if(stack == null || stack.Count <= 0) + if (stack == null || stack.Count <= 0) { return; } diff --git a/Mine2d/game/core/data/ItemId.cs b/Mine2d/game/core/data/ItemId.cs index d45d16c..b4b6df3 100644 --- a/Mine2d/game/core/data/ItemId.cs +++ b/Mine2d/game/core/data/ItemId.cs @@ -16,6 +16,11 @@ public enum ItemId RawTungsten = 115, RawUranium = 116, Diamond = 117, + Coal = 120, + GoldIngot = 121, + IronIngot = 122, PickaxeBasic = 200, PickaxeStone = 201, + PickaxeIron = 202, + PickaxeGold = 203, } diff --git a/Mine2d/game/core/data/World.cs b/Mine2d/game/core/data/World.cs index e8a4597..d2d9bb1 100644 --- a/Mine2d/game/core/data/World.cs +++ b/Mine2d/game/core/data/World.cs @@ -72,6 +72,11 @@ public class World return new Vector2((float)chunkX, (float)chunkY); } + public STile GetTileAt(Vector2 pos) + { + return this.GetTileAt((int)pos.X, (int)pos.Y); + } + public STile GetTileAt(int x, int y) { return this.GetChunkAt(x, y).GetTileAt(x, y); @@ -82,6 +87,11 @@ public class World this.GetChunkAt(x, y).SetTileAt(x, y, tile); } + public bool HasTileAt(Vector2 pos) + { + return this.HasTileAt((int)pos.X, (int)pos.Y); + } + public bool HasTileAt(int x, int y) { return this.HasChunkAt(x, y) && this.GetChunkAt(x, y).HasTileAt(new Vector2(x, y)); diff --git a/Mine2d/game/core/items/ItemRegistry.cs b/Mine2d/game/core/items/ItemRegistry.cs index b824988..17ef6ea 100644 --- a/Mine2d/game/core/items/ItemRegistry.cs +++ b/Mine2d/game/core/items/ItemRegistry.cs @@ -30,8 +30,13 @@ 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.Coal, new Item(ItemId.Coal, "Coal", "items.coal" )); + this.Register(ItemId.IronIngot, new Item(ItemId.IronIngot, "Iron Ingot", "items.ingot-iron" )); + this.Register(ItemId.GoldIngot, new Item(ItemId.GoldIngot, "Gold Ingot", "items.ingot-gold" )); this.Register(ItemId.PickaxeBasic, new PickaxeItem(ItemId.PickaxeBasic, "Basic Pickaxe", "items.pickaxe-basic", 15, 4)); this.Register(ItemId.PickaxeStone, new PickaxeItem(ItemId.PickaxeStone, "Stone Pickaxe", "items.pickaxe-stone", 25, 6)); + this.Register(ItemId.PickaxeIron, new PickaxeItem(ItemId.PickaxeIron, "Iron Pickaxe", "items.pickaxe-iron", 40, 7)); + this.Register(ItemId.PickaxeGold, new PickaxeItem(ItemId.PickaxeGold, "Gold Pickaxe", "items.pickaxe-gold", 60, 8)); } public void Register(ItemId id, Item item) diff --git a/Mine2d/game/core/tiles/Tile.cs b/Mine2d/game/core/tiles/Tile.cs index 257109c..b16b644 100644 --- a/Mine2d/game/core/tiles/Tile.cs +++ b/Mine2d/game/core/tiles/Tile.cs @@ -97,4 +97,8 @@ public class Tile { return true; } + + public virtual void OnInteract(Vector2 position) + { + } } diff --git a/Mine2d/game/core/tiles/TileRegistry.cs b/Mine2d/game/core/tiles/TileRegistry.cs index e824eeb..8adcb6f 100644 --- a/Mine2d/game/core/tiles/TileRegistry.cs +++ b/Mine2d/game/core/tiles/TileRegistry.cs @@ -23,6 +23,7 @@ public enum Tiles DiamondOre = 17, DripstoneUp = 18, DripstoneDown = 19, + CoalOre = 20, } public class TileRegistry @@ -50,6 +51,7 @@ public class TileRegistry this.Tiles.Add(17, new OreTile("diamond-ore", new[] { "stone", "diamond-ore" }, 10, ItemId.Diamond)); this.Tiles.Add(18, new DecoTile("dripstone-up", "dripstone-up", 5, ItemId.Air)); this.Tiles.Add(19, new DecoTile("dripstone-down", "dripstone-down", 5, ItemId.Air)); + this.Tiles.Add(20, new OreTile("coal-ore", new[] { "stone", "coal-ore" }, 4, ItemId.Coal)); } public Tile GetTile(int id) diff --git a/Mine2d/game/core/tiles/Workbench.cs b/Mine2d/game/core/tiles/Workbench.cs index 957cd4c..c4d01d5 100644 --- a/Mine2d/game/core/tiles/Workbench.cs +++ b/Mine2d/game/core/tiles/Workbench.cs @@ -1,15 +1,78 @@ using Mine2d.game.core.data; +using Mine2d.game.frontend.inventory; namespace Mine2d.game.core.tiles; +public class Recipe { + public ItemStack Result { get; set; } + public List Ingredients { get; set; } = new(); +} + public class Workbench : Tile { + public static List Recipes { get; set; } = new(); + public Workbench(string name, string texturePath, int hardness) : base(name, texturePath, hardness, ItemId.Workbench) { + Recipes.Add(new Recipe + { + Result = new ItemStack(ItemId.PickaxeBasic, 1), + Ingredients = new List + { + new ItemStack(ItemId.Stone, 2), + } + }); + Recipes.Add(new Recipe + { + Result = new ItemStack(ItemId.PickaxeStone, 1), + Ingredients = new List + { + new ItemStack(ItemId.Stone, 10), + } + }); + Recipes.Add(new Recipe + { + Result = new ItemStack(ItemId.PickaxeIron, 1), + Ingredients = new List + { + new ItemStack(ItemId.IronIngot, 10), + } + }); + Recipes.Add(new Recipe + { + Result = new ItemStack(ItemId.PickaxeGold, 1), + Ingredients = new List + { + new ItemStack(ItemId.GoldIngot, 10), + } + }); + Recipes.Add(new Recipe + { + Result = new ItemStack(ItemId.IronIngot, 1), + Ingredients = new List + { + new ItemStack(ItemId.Coal, 1), + new ItemStack(ItemId.RawIron, 1), + } + }); + Recipes.Add(new Recipe + { + Result = new ItemStack(ItemId.GoldIngot, 1), + Ingredients = new List + { + new ItemStack(ItemId.Coal, 1), + new ItemStack(ItemId.RawGold, 1), + } + }); } public override bool IsSolid() { return false; } + + public override void OnInteract(Vector2 position) + { + Context.Get().FrontendGameState.OpenInventory = InventoryKind.Workbench; + } } \ No newline at end of file diff --git a/Mine2d/game/core/world/ChunkGenerator.cs b/Mine2d/game/core/world/ChunkGenerator.cs index 328a5b1..99ea880 100644 --- a/Mine2d/game/core/world/ChunkGenerator.cs +++ b/Mine2d/game/core/world/ChunkGenerator.cs @@ -65,6 +65,10 @@ public class ChunkGenerator else { fill.Id = (int)WG.GetRandomOreAt(j + (y * 32)); + if (new Random().NextInt64(0, 130) < 1) + { + fill.Id = (int)Tiles.CoalOre; + } chunk.SetTile(i, j, fill); } } diff --git a/Mine2d/game/frontend/ItemRenderer.cs b/Mine2d/game/frontend/ItemRenderer.cs index 9a9e2e0..8bcdf8f 100644 --- a/Mine2d/game/frontend/ItemRenderer.cs +++ b/Mine2d/game/frontend/ItemRenderer.cs @@ -10,7 +10,7 @@ public static class ItemRenderer { private static readonly Dictionary Textures = new(); - public static void RenderItemStack(ItemStack stack, Vector2 position, bool renderTooltip = true) + public static void RenderItemStack(ItemStack stack, Vector2 position, bool renderTooltip = true, string description = null) { var texture = stack.GetTexture(); var renderer = Context.Get().Renderer; @@ -36,7 +36,7 @@ public static class ItemRenderer && cursorPosition.Y <= position.Y + (16 * uiScale) ) { - Context.Get().FrontendGameState.Tooltip = stack.GetName(); + Context.Get().FrontendGameState.Tooltip = stack.GetName() + (description != null ? "\n" + description : ""); } } } diff --git a/Mine2d/game/frontend/inventory/PlayerInventoryRenderer.cs b/Mine2d/game/frontend/inventory/PlayerInventoryRenderer.cs index 209e8c4..c6494e3 100644 --- a/Mine2d/game/frontend/inventory/PlayerInventoryRenderer.cs +++ b/Mine2d/game/frontend/inventory/PlayerInventoryRenderer.cs @@ -23,7 +23,12 @@ public class PlayerInventoryRenderer : Inventory this.x = (windowWidth - (width - extraSlotsWidth)) / 2; this.y = (windowHeight - height) / 2; ctx.Renderer.DrawTexture(this.texture, this.x, this.y, width, height); + Render(this.x, this.y); + } + public static void Render(int x, int y) { + var ctx = Context.Get(); + var uiScale = ctx.FrontendGameState.Settings.UiScale; var player = PlayerEntity.GetSelf(); var inventory = player.Inventory.Inventory; var hotbar = player.Inventory.Hotbar; @@ -34,7 +39,7 @@ public class PlayerInventoryRenderer : Inventory { continue; } - ItemRenderer.RenderItemStack(stack, new Vector2(((4 + (i * 21)) * uiScale) + this.x, (4 * uiScale) + this.y), player.Inventory.Cursor == null); + ItemRenderer.RenderItemStack(stack, new Vector2(((4 + (i * 21)) * uiScale) + x, (4 * uiScale) + y), player.Inventory.Cursor == null); } for (var i = 0; i < inventory.Length; i++) { @@ -44,28 +49,33 @@ public class PlayerInventoryRenderer : Inventory 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); + ItemRenderer.RenderItemStack(stack, new Vector2(((4 + ((i % 9) * 21)) * uiScale) + x, ((4 + 21 + ((i / 9) * 21)) * uiScale) + 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); + ItemRenderer.RenderItemStack(pickaxe, new Vector2(((4 + (9 * 21)) * uiScale) + x, (((5 * 21) + 4) * uiScale) + y), player.Inventory.Cursor == null, "Test\nTT"); } } public override void OnClick(SDL_Event e) + { + OnClick(this.x, this.y, e); + } + + public static void OnClick(int x, int y, 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) + if (cursorPosition.X >= x + (4 * Context.Get().FrontendGameState.Settings.UiScale) + && cursorPosition.X <= x + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 9 * Context.Get().FrontendGameState.Settings.UiScale) + && cursorPosition.Y >= y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + && cursorPosition.Y <= y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * Context.Get().FrontendGameState.Settings.UiScale) ) { var hotbar = player.Inventory.Hotbar; - var index = (int)((cursorPosition.X - (this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale))) / (21 * Context.Get().FrontendGameState.Settings.UiScale)); + var index = (int)((cursorPosition.X - (x + (4 * Context.Get().FrontendGameState.Settings.UiScale))) / (21 * Context.Get().FrontendGameState.Settings.UiScale)); if (e.button.button == SDL_BUTTON_LEFT) { player.Inventory.SwapWithCursor(index, hotbar); @@ -82,15 +92,15 @@ public class PlayerInventoryRenderer : 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 (cursorPosition.X >= x + (4 * Context.Get().FrontendGameState.Settings.UiScale) + && cursorPosition.X <= x + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 9 * Context.Get().FrontendGameState.Settings.UiScale) + && cursorPosition.Y >= y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * Context.Get().FrontendGameState.Settings.UiScale) + && cursorPosition.Y <= y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 6 * 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); + var index = (int)((cursorPosition.X - (x + (4 * Context.Get().FrontendGameState.Settings.UiScale))) / (21 * Context.Get().FrontendGameState.Settings.UiScale)) + + ((int)((cursorPosition.Y - (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); @@ -107,7 +117,7 @@ public class PlayerInventoryRenderer : Inventory } } } - if (IsCursorOnSlot(((4 + (9 * 21)) * Context.Get().FrontendGameState.Settings.UiScale) + this.x, (((5 * 21) + 4) * Context.Get().FrontendGameState.Settings.UiScale) + this.y)) + if (IsCursorOnSlot(((4 + (9 * 21)) * Context.Get().FrontendGameState.Settings.UiScale) + x, (((5 * 21) + 4) * Context.Get().FrontendGameState.Settings.UiScale) + y)) { if (e.button.button == SDL_BUTTON_LEFT) { diff --git a/Mine2d/game/frontend/inventory/WorkbenchInventory.cs b/Mine2d/game/frontend/inventory/WorkbenchInventory.cs index a2a34bc..30a186a 100644 --- a/Mine2d/game/frontend/inventory/WorkbenchInventory.cs +++ b/Mine2d/game/frontend/inventory/WorkbenchInventory.cs @@ -2,12 +2,15 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Mine2d.game.core; +using Mine2d.game.core.tiles; namespace Mine2d.game.frontend.inventory; public class WorkbenchInventory : Inventory { private IntPtr texture = IntPtr.Zero; + private int x, y; public override void Render() { var ctx = Context.Get(); @@ -16,10 +19,57 @@ public class WorkbenchInventory : Inventory this.texture = ctx.TextureFactory.LoadTexture("hud.workbench-inventory"); } var (width, height) = ctx.Renderer.GetTextureSize(this.texture); - var (windowWidth, windowHeight) = (ctx.FrontendGameState.WindowWidth, ctx.FrontendGameState.WindowHeight); - var x = (windowWidth - width) / 2; - var y = (windowHeight - height) / 2; var uiScale = ctx.FrontendGameState.Settings.UiScale; - ctx.Renderer.DrawTexture(this.texture, x, y, width * uiScale, height * 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); + + PlayerInventoryRenderer.Render(this.x, this.y + (50 * uiScale)); + var rX = this.x + (4 * uiScale); + var rY = this.y + (4 * uiScale); + foreach ( var r in Workbench.Recipes) { + var desc = "Requires:\n"; + foreach (var i in r.Ingredients) { + desc += $"{i.Count}x {i.GetName()}\n"; + } + ItemRenderer.RenderItemStack(r.Result, new Vector2(rX, rY), true, desc); + rX += 21 * uiScale; + } + } + + public override void OnClick(SDL_Event e) + { + var ctx = Context.Get(); + var uiScale = ctx.FrontendGameState.Settings.UiScale; + PlayerInventoryRenderer.OnClick(this.x, this.y + (50 * uiScale), e); + var cursorPosition = Context.Get().FrontendGameState.CursorPosition; + var player = PlayerEntity.GetSelf(); + var rX = this.x + (4 * uiScale); + var rY = this.y + (4 * uiScale); + foreach ( var r in Workbench.Recipes) { + if(cursorPosition.X >= rX && cursorPosition.X <= rX + (21 * uiScale) && cursorPosition.Y >= rY && cursorPosition.Y <= rY + (21 * uiScale)) { + Console.WriteLine("Clicked on recipe" + r.Result.GetName()); + var canCraft = true; + foreach (var i in r.Ingredients) { + if (!player.Inventory.HasItemStack(i)) { + canCraft = false; + break; + } + } + if (canCraft) { + foreach (var i in r.Ingredients) { + player.Inventory.RemoveItemStack(i); + } + player.Inventory.PickupItemStack(r.Result); + } + } + + + rX += 21 * uiScale; + } } } \ No newline at end of file diff --git a/Mine2d/game/frontend/renderer/PlayerRenderer.cs b/Mine2d/game/frontend/renderer/PlayerRenderer.cs index e283d8d..099bb68 100644 --- a/Mine2d/game/frontend/renderer/PlayerRenderer.cs +++ b/Mine2d/game/frontend/renderer/PlayerRenderer.cs @@ -13,7 +13,6 @@ public class PlayerRenderer : IRenderer this.playerTexture = Context.Get().TextureFactory.LoadTexture("character.character"); } var ctx = Context.Get(); - var camera = ctx.FrontendGameState.Camera; var scale = ctx.FrontendGameState.Settings.GameScale; var (width, height) = ctx.Window.GetSize(); foreach (var player in ctx.GameState.Players) @@ -26,13 +25,6 @@ public class PlayerRenderer : IRenderer { ctx.Renderer.SetColor(255, 0, 0); } - - // ctx.Renderer.DrawRect( - // (player.Position.X - (int)camera.Position.X) * scale, - // (player.Position.Y - (int)camera.Position.Y) * scale - 28 * scale, - // 14 * scale, - // 28 * scale - // ); var y = player.PlayerMovementState.MovingRight ? 32 * 3 : 32 * 1; var moving = player.PlayerMovementState.CurrentVelocity.X != 0; var dt = (DateTime.Now - DateTime.MinValue).TotalMilliseconds; diff --git a/Mine2d/game/state/FrontendGameState.cs b/Mine2d/game/state/FrontendGameState.cs index e7578d8..9d6a21c 100644 --- a/Mine2d/game/state/FrontendGameState.cs +++ b/Mine2d/game/state/FrontendGameState.cs @@ -16,7 +16,7 @@ public class FrontendGameState public Settings Settings { get; set; } = new Settings(); public string PlayerName { get; set; } = "Player"; public int HotbarIndex { get; set; } - public string Tooltip { get; set; } = "Test"; + public string Tooltip { get; set; } public InventoryKind OpenInventory { get; set; } = InventoryKind.None; public InputState InputState { get; set; } = new(); public DebugState DebugState { get; set; } = new(); diff --git a/Mine2d/game/state/PlayerInventory.cs b/Mine2d/game/state/PlayerInventory.cs index 23a264d..d1dba22 100644 --- a/Mine2d/game/state/PlayerInventory.cs +++ b/Mine2d/game/state/PlayerInventory.cs @@ -37,6 +37,45 @@ public class PlayerInventory return true; } + public bool HasItemStack(ItemStack itemStack) + { + return HasItemStack(itemStack, this.Hotbar) || HasItemStack(itemStack, this.Inventory); + } + + public static bool HasItemStack(ItemStack itemStack, ItemStack[] inventory) + { + var slot = InventoryUtils.GetFirstMatchingSlot(inventory, itemStack.Id); + if (slot == -1) + { + return false; + } + if(inventory[slot] == null) return false; + return inventory[slot].Count >= itemStack.Count; + } + + public bool RemoveItemStack(ItemStack itemStack) + { + return RemoveItemStack(itemStack, this.Hotbar) || RemoveItemStack(itemStack, this.Inventory); + } + + public static bool RemoveItemStack(ItemStack itemStack, ItemStack[] inventory) + { + var slot = InventoryUtils.GetFirstMatchingSlot(inventory, itemStack.Id); + if (slot == -1) + { + return false; + } + if (inventory[slot].Count == itemStack.Count) + { + inventory[slot] = null; + } + else + { + inventory[slot].Count -= itemStack.Count; + } + return true; + } + public void SwapWithCursor(int slot, ItemStack[] inventory) { if (this.Cursor != null && inventory[slot] != null && this.Cursor.Id == inventory[slot].Id && this.Cursor.IsStackable())