From b1dbad6dd46e727bb0bbf60e085cde45ee9a2ca9 Mon Sep 17 00:00:00 2001 From: MasterGordon Date: Wed, 21 Dec 2022 18:10:26 +0100 Subject: [PATCH] added basic worldgen --- Mine2d/assets/hud/player-inventory.png | Bin 634 -> 797 bytes Mine2d/engine/FontManager.cs | 2 +- Mine2d/game/backend/interactor/Move.cs | 2 +- Mine2d/game/core/world/ChunkGenerator.cs | 2 +- Mine2d/game/core/world/WorldGenerator.cs | 84 +++++++++++------- .../inventory/PlayerInventoryRenderer.cs | 24 ++++- Mine2d/game/state/FrontendGameState.cs | 4 +- Mine2d/game/state/PlayerInventory.cs | 45 +++++++++- 8 files changed, 126 insertions(+), 37 deletions(-) diff --git a/Mine2d/assets/hud/player-inventory.png b/Mine2d/assets/hud/player-inventory.png index a42212ea1c7ae424726ea31c4e1cc1cf906807f6..fa443bd2141e429c6110208ba714cc07f7e5403f 100644 GIT binary patch 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% literal 634 zcmeAS@N?(olHy`uVBq!ia0vp^SAn>ZgAGWA$Nx_PQjEnx?oJHr&dI!FU|=%wba4!+ znDh3IZ`SPq2Dgjq$x$sQxJ-1HN`LsbJu_sg0*WJudU9rHmuI8Kv=QD4$>B~Fs zo8;B}{`Iu*0Q>j9$IoY;{+roj$oJZ+@3fA_yJOQ5-yV}_dh{;mXPDXDzWRC7Q@&jP zSyH<#^RsS#USZPPSB^W%7d6dTp`fPK(aDuWkU9Bu+>Z3q;Edj{6>?2-6vv&X*b zL^%lV;+Q{7h7c{{`)_QK%(q%eY*2y25@>0MC0NVOA4iv+t+?z-I56OLyRdG2xnbSo z9cT3F8xI_QY4cW(fBoO{2dc|oW_ settings = new (); + private readonly List settings = new(); - public WorldGenerator() { - this.settings.Add(new GenerationSettings { + public WorldGenerator() + { + this.settings.Add(new GenerationSettings + { xOffset = 10, yOffset = 15, tile = Tiles.IronOre }); - this.settings.Add(new GenerationSettings { + this.settings.Add(new GenerationSettings + { xOffset = 15, yOffset = 20, tile = Tiles.CopperOre }); - this.settings.Add(new GenerationSettings { + this.settings.Add(new GenerationSettings + { xOffset = 20, yOffset = 10, tile = Tiles.TinOre }); - this.settings.Add(new GenerationSettings { + this.settings.Add(new GenerationSettings + { xOffset = 40, yOffset = 3, tile = Tiles.SilverOre }); - this.settings.Add(new GenerationSettings { - xOffset = 40, + this.settings.Add(new GenerationSettings + { + xOffset = 80, yOffset = 3, tile = Tiles.GoldOre }); - this.settings.Add(new GenerationSettings { - xOffset = 50, + this.settings.Add(new GenerationSettings + { + xOffset = 90, yOffset = 15, tile = Tiles.LeadOre }); - this.settings.Add(new GenerationSettings { - xOffset = 60, - yOffset = 2, + this.settings.Add(new GenerationSettings + { + xOffset = 160, + yOffset = 3, tile = Tiles.PlatinumOre }); - this.settings.Add(new GenerationSettings { - xOffset = 60, + this.settings.Add(new GenerationSettings + { + xOffset = 160, yOffset = 3, tile = Tiles.CobaltOre }); - this.settings.Add(new GenerationSettings { - xOffset = 65, + this.settings.Add(new GenerationSettings + { + xOffset = 165, yOffset = 10, tile = Tiles.TungstenOre }); - this.settings.Add(new GenerationSettings { - xOffset = 90, + this.settings.Add(new GenerationSettings + { + xOffset = 290, yOffset = 1, tile = Tiles.DiamondOre }); - this.settings.Add(new GenerationSettings { - xOffset = 94, + this.settings.Add(new GenerationSettings + { + xOffset = 294, yOffset = 2, tile = Tiles.UraniumOre }); } - public Tiles GetRandomOreAt(int height) { + public Tiles GetRandomOreAt(int height) + { var random = new Random(); - var weight = random.Next(0, 4000); - var ore = this.settings.FirstOrDefault(x => x.GetWeight(height) > weight, null); - if(ore == null) { - return Tiles.Stone; + var ores = new List(); + foreach (var setting in this.settings) + { + for (var i = 0; i < setting.GetWeight(height); i++) + { + ores.Add(setting.tile); + } } - return ore.tile; + + var rng = random.Next(0, 700); + if (rng < ores.Count) + { + return ores[rng]; + } + return Tiles.Stone; } } \ No newline at end of file diff --git a/Mine2d/game/frontend/inventory/PlayerInventoryRenderer.cs b/Mine2d/game/frontend/inventory/PlayerInventoryRenderer.cs index 8dedbb2..4a7400c 100644 --- a/Mine2d/game/frontend/inventory/PlayerInventoryRenderer.cs +++ b/Mine2d/game/frontend/inventory/PlayerInventoryRenderer.cs @@ -110,12 +110,23 @@ namespace Mine2d.game.frontend.inventory { player.Inventory.SwapWithCursor(index, hotbar); } + if (e.button.button == SDL_BUTTON_RIGHT) + { + if (player.Inventory.Cursor == null) + { + player.Inventory.TakeHalf(index, hotbar); + } + else + { + player.Inventory.DropOne(index, hotbar); + } + } } // 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 * 5 * Context.Get().FrontendGameState.Settings.UiScale) + && cursorPosition.Y <= this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 6 * Context.Get().FrontendGameState.Settings.UiScale) ) { var player = PlayerEntity.GetSelf(); @@ -126,6 +137,17 @@ namespace Mine2d.game.frontend.inventory { 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); + } + } } } } diff --git a/Mine2d/game/state/FrontendGameState.cs b/Mine2d/game/state/FrontendGameState.cs index 5bbb1ad..4bfec6b 100644 --- a/Mine2d/game/state/FrontendGameState.cs +++ b/Mine2d/game/state/FrontendGameState.cs @@ -27,8 +27,8 @@ public class FrontendGameState public class Settings { - public int GameScale { get; set; } = 4; - public int UiScale { get; set; } = 3; + public int GameScale { get; set; } = 6; + public int UiScale { get; set; } = 4; public bool ShowCollision { get; set; } = true; public bool Fullscreen { get; set; } = false; } \ No newline at end of file diff --git a/Mine2d/game/state/PlayerInventory.cs b/Mine2d/game/state/PlayerInventory.cs index 9702979..511e7e7 100644 --- a/Mine2d/game/state/PlayerInventory.cs +++ b/Mine2d/game/state/PlayerInventory.cs @@ -34,6 +34,49 @@ public class PlayerInventory public void SwapWithCursor(int slot, ItemStack[] inventory) { - (inventory[slot], this.Cursor) = (this.Cursor, inventory[slot]); + if (this.Cursor != null && inventory[slot] != null && this.Cursor.Id == inventory[slot].Id) + { + inventory[slot].Count += this.Cursor.Count; + this.Cursor = null; + } + else + { + (inventory[slot], this.Cursor) = (this.Cursor, inventory[slot]); + } + } + + public void TakeHalf(int slot, ItemStack[] inventory) + { + if (inventory[slot] == null) + return; + if(inventory[slot].Count == 1) + { + this.Cursor = inventory[slot]; + inventory[slot] = null; + return; + } + this.Cursor = new ItemStack(inventory[slot].Id, inventory[slot].Count / 2); + inventory[slot].Count -= this.Cursor.Count; + } + + public void DropOne(int slot, ItemStack[] inventory) + { + if (inventory[slot] == null) + { + inventory[slot] = new ItemStack(this.Cursor.Id, 1); + } + else if (inventory[slot].Id == this.Cursor.Id) + { + inventory[slot].Count++; + } + else + { + return; + } + this.Cursor.Count--; + if (this.Cursor.Count == 0) + { + this.Cursor = null; + } } }