diff --git a/Mine2d/assets/blocks/tine-ore.png b/Mine2d/assets/blocks/tin-ore.png similarity index 100% rename from Mine2d/assets/blocks/tine-ore.png rename to Mine2d/assets/blocks/tin-ore.png diff --git a/Mine2d/assets/hud/workbench-inventory.png b/Mine2d/assets/hud/workbench-inventory.png new file mode 100644 index 0000000..dbd4c28 Binary files /dev/null and b/Mine2d/assets/hud/workbench-inventory.png differ diff --git a/Mine2d/game/backend/interactor/Place.cs b/Mine2d/game/backend/interactor/Place.cs index 5d17651..b4e825a 100644 --- a/Mine2d/game/backend/interactor/Place.cs +++ b/Mine2d/game/backend/interactor/Place.cs @@ -2,6 +2,8 @@ using Mine2d.engine.system.annotations; using Mine2d.game.backend.network.packets; using Mine2d.game.core; using Mine2d.game.core.data; +using Mine2d.game.core.tiles; +using Mine2d.game.state; namespace Mine2d.game.backend.interactor; @@ -29,7 +31,11 @@ public class Place { var chunk = ctx.GameState.World.GetChunkAt(packet.Target); var tile = chunk.GetTileAt(packet.Target); + var tileId = tile.Id; + if(tileId == (int)Tiles.Workbench) { + ctx.FrontendGameState.OpenInventory = InventoryKind.Workbench; + } if (tileId != 0 || player.Inventory.Hotbar[packet.Slot] == null || player.Inventory.Hotbar[packet.Slot]?.Count <= 0) { return; diff --git a/Mine2d/game/core/data/ItemId.cs b/Mine2d/game/core/data/ItemId.cs index 824ec46..0bde656 100644 --- a/Mine2d/game/core/data/ItemId.cs +++ b/Mine2d/game/core/data/ItemId.cs @@ -5,4 +5,15 @@ public enum ItemId Air = 0, Stone = 1, Workbench = 6, + RawIron = 107, + RawCopper = 108, + RawTin = 109, + RawSilver = 110, + RawGold = 111, + RawLead = 112, + RawPlatinum = 113, + RawCobalt = 114, + RawTungsten = 115, + RawUranium = 116, + Diamond = 117, } diff --git a/Mine2d/game/core/data/World.cs b/Mine2d/game/core/data/World.cs index 205f104..e8a4597 100644 --- a/Mine2d/game/core/data/World.cs +++ b/Mine2d/game/core/data/World.cs @@ -98,7 +98,7 @@ public class World this.SetTileAt((int)head.Pos.X, (int)head.Pos.Y, stile with { Hits = stile.Hits - 1 }); if (stile.Hits >= 1) { - this.Cracks.Enqueue(new CrackQueueEntry { Pos = head.Pos, ResetTime = now.AddSeconds(1) }); + this.Cracks.Enqueue(new CrackQueueEntry { Pos = head.Pos, ResetTime = now.AddSeconds(0.5) }); needsReorder = true; } } diff --git a/Mine2d/game/core/items/Item.cs b/Mine2d/game/core/items/Item.cs index ae759a4..67a5fed 100644 --- a/Mine2d/game/core/items/Item.cs +++ b/Mine2d/game/core/items/Item.cs @@ -15,6 +15,13 @@ public class Item this.texture = Context.Get().TextureFactory.CreateTexture(textureName); } + public Item(ItemId id, string name, string textureName) + { + this.Id = id; + this.Name = name; + this.texture = Context.Get().TextureFactory.LoadTexture(textureName); + } + public void Render(Vector2 position) { var ctx = Context.Get(); diff --git a/Mine2d/game/core/items/ItemRegistry.cs b/Mine2d/game/core/items/ItemRegistry.cs index 9014042..737123d 100644 --- a/Mine2d/game/core/items/ItemRegistry.cs +++ b/Mine2d/game/core/items/ItemRegistry.cs @@ -10,6 +10,17 @@ public class ItemRegistry { this.Register(ItemId.Stone, new Item(ItemId.Stone, "Stone", new[] { "stone" })); this.Register(ItemId.Workbench, new Item(ItemId.Workbench, "Workbench", new[] { "workbench" })); + this.Register(ItemId.RawIron, new Item(ItemId.RawCobalt, "Raw Iron", "items.raw-iron" )); + this.Register(ItemId.RawCopper, new Item(ItemId.RawCopper, "Raw Copper", "items.raw-copper" )); + this.Register(ItemId.RawTin, new Item(ItemId.RawTin, "Raw Tin", "items.raw-tin" )); + this.Register(ItemId.RawSilver, new Item(ItemId.RawSilver, "Raw Silver", "items.raw-silver" )); + this.Register(ItemId.RawGold, new Item(ItemId.RawGold, "Raw Gold", "items.raw-gold" )); + this.Register(ItemId.RawLead, new Item(ItemId.RawLead, "Raw Lead", "items.raw-lead" )); + this.Register(ItemId.RawPlatinum, new Item(ItemId.RawPlatinum, "Raw Platinum", "items.raw-platinum" )); + this.Register(ItemId.RawCobalt, new Item(ItemId.RawCobalt, "Raw Cobalt", "items.raw-cobalt" )); + 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" )); } public void Register(ItemId id, Item item) diff --git a/Mine2d/game/core/tiles/OreTile.cs b/Mine2d/game/core/tiles/OreTile.cs index 84dff0b..59922d0 100644 --- a/Mine2d/game/core/tiles/OreTile.cs +++ b/Mine2d/game/core/tiles/OreTile.cs @@ -7,4 +7,8 @@ public class OreTile : Tile public OreTile(string name, string[] texturePath, int hardness) : base(name, Context.Get().TextureFactory.CreateTexture(texturePath), hardness, ItemId.Air) { } + + public OreTile(string name, string[] texturePath, int hardness, ItemId drop) : base(name, Context.Get().TextureFactory.CreateTexture(texturePath), hardness, drop) + { + } } diff --git a/Mine2d/game/core/tiles/TileRegistry.cs b/Mine2d/game/core/tiles/TileRegistry.cs index c6565c9..d99b9aa 100644 --- a/Mine2d/game/core/tiles/TileRegistry.cs +++ b/Mine2d/game/core/tiles/TileRegistry.cs @@ -10,6 +10,17 @@ public enum Tiles Ore3 = 4, Ore4 = 5, Workbench = 6, + IronOre = 7, + CopperOre = 8, + TinOre = 9, + SilverOre = 10, + GoldOre = 11, + LeadOre = 12, + PlatinumOre = 13, + CobaltOre = 14, + TungstenOre = 15, + UraniumOre = 16, + DiamondOre = 17, } public class TileRegistry @@ -24,6 +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)); } public Tile GetTile(int id) diff --git a/Mine2d/game/core/world/ChunkGenerator.cs b/Mine2d/game/core/world/ChunkGenerator.cs index b196c4c..eae416b 100644 --- a/Mine2d/game/core/world/ChunkGenerator.cs +++ b/Mine2d/game/core/world/ChunkGenerator.cs @@ -5,6 +5,7 @@ namespace Mine2d.game.core.world; public class ChunkGenerator { + private static WorldGenerator wg = new WorldGenerator(); public static Chunk CreateFilledChunk(int x, int y, STile fill) { var chunk = new Chunk(x, y); @@ -29,26 +30,7 @@ public class ChunkGenerator { for (var j = 0; j < Constants.ChunkSize; j++) { - if (new Random().Next(0, 100) < 10) - { - fill.Id = (int)Tiles.Ore1; - } - else if (new Random().Next(0, 100) < 10) - { - fill.Id = (int)Tiles.Ore2; - } - else if (new Random().Next(0, 100) < 10) - { - fill.Id = (int)Tiles.Ore3; - } - else if (new Random().Next(0, 100) < 10) - { - fill.Id = (int)Tiles.Ore4; - } - else - { - fill.Id = (int)Tiles.Stone; - } + fill.Id = (int)wg.GetRandomOreAt(j + y); chunk.SetTile(i, j, fill); } } diff --git a/Mine2d/game/core/world/WorldGenerator.cs b/Mine2d/game/core/world/WorldGenerator.cs index 2f2a4c0..1864c1f 100644 --- a/Mine2d/game/core/world/WorldGenerator.cs +++ b/Mine2d/game/core/world/WorldGenerator.cs @@ -6,25 +6,85 @@ using Mine2d.game.core.tiles; namespace Mine2d.game.core.world; -public struct GenerationSettings { +public class GenerationSettings { public int xOffset { get; set; } public int yOffset { get; set; } public Tiles tile { get; set; } public int GetWeight(int height) { - return (int)((-Math.Pow(height - this.xOffset, 2)*0.01) + this.yOffset); + return (int)((-Math.Pow(height - this.xOffset, 2)*0.01) + this.yOffset + (32*10)); } } public class WorldGenerator { - List settings = new (); + private readonly List settings = new (); public WorldGenerator() { + this.settings.Add(new GenerationSettings { + xOffset = 10, + yOffset = 15, + tile = Tiles.IronOre + }); + this.settings.Add(new GenerationSettings { + xOffset = 15, + yOffset = 20, + tile = Tiles.CopperOre + }); + this.settings.Add(new GenerationSettings { + xOffset = 20, + yOffset = 10, + tile = Tiles.TinOre + }); + this.settings.Add(new GenerationSettings { + xOffset = 40, + yOffset = 3, + tile = Tiles.SilverOre + }); + this.settings.Add(new GenerationSettings { + xOffset = 40, + yOffset = 3, + tile = Tiles.GoldOre + }); this.settings.Add(new GenerationSettings { xOffset = 50, - yOffset = 16, - tile = Tiles.Stone + yOffset = 15, + tile = Tiles.LeadOre + }); + this.settings.Add(new GenerationSettings { + xOffset = 60, + yOffset = 2, + tile = Tiles.PlatinumOre + }); + this.settings.Add(new GenerationSettings { + xOffset = 60, + yOffset = 3, + tile = Tiles.CobaltOre + }); + this.settings.Add(new GenerationSettings { + xOffset = 65, + yOffset = 10, + tile = Tiles.TungstenOre + }); + this.settings.Add(new GenerationSettings { + xOffset = 90, + yOffset = 1, + tile = Tiles.DiamondOre + }); + this.settings.Add(new GenerationSettings { + xOffset = 94, + yOffset = 2, + tile = Tiles.UraniumOre }); } + + 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; + } + return ore.tile; + } } \ No newline at end of file diff --git a/Mine2d/game/frontend/events/Exit.cs b/Mine2d/game/frontend/events/Exit.cs index 4a84a10..5a109ee 100644 --- a/Mine2d/game/frontend/events/Exit.cs +++ b/Mine2d/game/frontend/events/Exit.cs @@ -14,9 +14,9 @@ public class Exit [EventListener(EventType.KeyDown)] public static void OnKeyDown(SDL_Event e) { - if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_ESCAPE) - { - Environment.Exit(0); - } + // if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_ESCAPE) + // { + // Environment.Exit(0); + // } } } diff --git a/Mine2d/game/frontend/events/InventoryInput.cs b/Mine2d/game/frontend/events/InventoryInput.cs index f54f1e2..43156d0 100644 --- a/Mine2d/game/frontend/events/InventoryInput.cs +++ b/Mine2d/game/frontend/events/InventoryInput.cs @@ -47,12 +47,16 @@ public class InventoryInput var frontendGameState = Context.Get().FrontendGameState; if(e.key.keysym.sym == SDL_Keycode.SDLK_TAB) { - if(frontendGameState.OpenInventory != InventoryKind.Player) { + if(frontendGameState.OpenInventory == InventoryKind.None) { frontendGameState.OpenInventory = InventoryKind.Player; } else { frontendGameState.OpenInventory = InventoryKind.None; } } + if(e.key.keysym.sym == SDL_Keycode.SDLK_ESCAPE) + { + frontendGameState.OpenInventory = InventoryKind.None; + } if(frontendGameState.OpenInventory != InventoryKind.None) { throw new CancelEventException(); diff --git a/Mine2d/game/frontend/inventory/InventoryRegistry.cs b/Mine2d/game/frontend/inventory/InventoryRegistry.cs index 1ed482e..0a68343 100644 --- a/Mine2d/game/frontend/inventory/InventoryRegistry.cs +++ b/Mine2d/game/frontend/inventory/InventoryRegistry.cs @@ -9,6 +9,7 @@ public class InventoryRegistry public InventoryRegistry() { this.inventoryRenderers.Add(InventoryKind.Player, new PlayerInventoryRenderer()); + this.inventoryRenderers.Add(InventoryKind.Workbench, new WorkbenchInventory()); } public Inventory GetInventory(InventoryKind inventory) diff --git a/Mine2d/game/frontend/inventory/WorkbenchInventory.cs b/Mine2d/game/frontend/inventory/WorkbenchInventory.cs new file mode 100644 index 0000000..a2a34bc --- /dev/null +++ b/Mine2d/game/frontend/inventory/WorkbenchInventory.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Mine2d.game.frontend.inventory; + +public class WorkbenchInventory : Inventory +{ + private IntPtr texture = IntPtr.Zero; + public override void Render() + { + var ctx = Context.Get(); + if (this.texture == IntPtr.Zero) + { + 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); + } +} \ No newline at end of file diff --git a/Mine2d/game/state/FrontendGameState.cs b/Mine2d/game/state/FrontendGameState.cs index 06d30da..5bbb1ad 100644 --- a/Mine2d/game/state/FrontendGameState.cs +++ b/Mine2d/game/state/FrontendGameState.cs @@ -4,7 +4,8 @@ namespace Mine2d.game.state; public enum InventoryKind { None, - Player + Player, + Workbench } public class FrontendGameState