diff --git a/Mine2d/+ b/Mine2d/+ deleted file mode 100644 index 1408d5e..0000000 --- a/Mine2d/+ +++ /dev/null @@ -1,61 +0,0 @@ -using Mine2d.engine; -using Mine2d.engine.system.annotations; -using Mine2d.game.core.data.entities; - -namespace Mine2d.game.backend.interactor; - -[Interactor] -public class ItemPhysics -{ - - [Interaction(InteractorKind.Hybrid, "tick")] - public static void TickHybrid() - { - var gameState = Context.Get().GameState; - var world = gameState.World; - foreach (var chunk in world.Chunks) - { - var entities = chunk.Value.Entities; - foreach (var entity in entities) - { - if (entity is ItemEntity itemEntity) - { - itemEntity.Velocity += new Vector2(0, 0.1f); - var nextPos = itemEntity.Position + itemEntity.Velocity; - if (world.HasChunkAt(nextPos) && world.GetChunkAt(nextPos).HasTileAt(nextPos)) - { - itemEntity.Velocity = new Vector2(0, 0); - continue; - } - itemEntity.Position = nextPos; - } - } - } - } - - [Interaction(InteractorKind.Hybrid, "tick")] - public static void Pickup() - { - var gameState = Context.Get().GameState; - var world = gameState.World; - foreach (var chunk in world.Chunks) - { - var entities = chunk.Value.Entities; - foreach (var entity in entities) - { - foreach (var player in gameState.Players) - { - - var items = chunk.Value.Entities.RemoveAll(e => - { - return e is ItemEntity itemEntity && (player.GetCenter() - itemEntity.Position).LengthSquared() < 8 * 8; - }); - if (items > 0) - { - Context.Get().GameAudio.Play(Sound.ItemPickup); - } - } - } - } - } -} diff --git a/Mine2d/assets/audio/block_hit_alt.wav b/Mine2d/assets/audio/block_hit_alt.wav new file mode 100644 index 0000000..0484f04 Binary files /dev/null and b/Mine2d/assets/audio/block_hit_alt.wav differ diff --git a/Mine2d/assets/hud/hotbar.png b/Mine2d/assets/hud/hotbar.png new file mode 100644 index 0000000..bbc5313 Binary files /dev/null and b/Mine2d/assets/hud/hotbar.png differ diff --git a/Mine2d/game/backend/interactor/ItemPhysics.cs b/Mine2d/game/backend/interactor/ItemPhysics.cs index 060aea5..fe71785 100644 --- a/Mine2d/game/backend/interactor/ItemPhysics.cs +++ b/Mine2d/game/backend/interactor/ItemPhysics.cs @@ -1,5 +1,6 @@ using Mine2d.engine; using Mine2d.engine.system.annotations; +using Mine2d.game.core.data; using Mine2d.game.core.data.entities; namespace Mine2d.game.backend.interactor; @@ -44,14 +45,17 @@ public class ItemPhysics foreach (var player in gameState.Players) { - var items = chunk.Value.Entities.RemoveAll(e => + var items = chunk.Value.Entities.Where(e => { - return e is ItemEntity itemEntity && (player.Position + new Vector2(7, 3) - itemEntity.Position).LengthSquared() < 8 * 8; + return e is ItemEntity itemEntity && + (player.Position + new Vector2(7, 3) - itemEntity.Position).LengthSquared() < 8 * 8 && + player.inventory.PickupItemStack(new ItemStack { Id = itemEntity.ItemId, Count = 1 }); }); - if (items > 0) + if (items.Any()) { Context.Get().GameAudio.Play(Sound.ItemPickup); } + _ = chunk.Value.Entities.RemoveAll(e => items.Contains(e)); } } } diff --git a/Mine2d/game/core/InventoryUtils.cs b/Mine2d/game/core/InventoryUtils.cs new file mode 100644 index 0000000..cfcdec1 --- /dev/null +++ b/Mine2d/game/core/InventoryUtils.cs @@ -0,0 +1,25 @@ +using Mine2d.game.core.data; + +namespace Mine2d.game.core; + +public static class InventoryUtils +{ + public static int GetFirstMatchingSlot(ItemStack[] inventory, ItemId id) + { + for (var i = 0; i < inventory.Length; i++) + { + if (inventory[i] != null && inventory[i].Id == id) + { + return i; + } + } + for (var i = 0; i < inventory.Length; i++) + { + if (inventory[i] == null) + { + return i; + } + } + return -1; + } +} diff --git a/Mine2d/game/core/PlayerEntity.cs b/Mine2d/game/core/PlayerEntity.cs index 7d1a3cb..f96daec 100644 --- a/Mine2d/game/core/PlayerEntity.cs +++ b/Mine2d/game/core/PlayerEntity.cs @@ -22,6 +22,13 @@ public class PlayerEntity { p.Movement += Constants.Gravity; p.Position += p.Movement; + if (p.Movement.Y > 8) + { + p.Movement = p.Movement with + { + Y = 8 + }; + } } public static void Collide(Player p) diff --git a/Mine2d/game/core/data/ItemStack.cs b/Mine2d/game/core/data/ItemStack.cs new file mode 100644 index 0000000..0b22406 --- /dev/null +++ b/Mine2d/game/core/data/ItemStack.cs @@ -0,0 +1,7 @@ +namespace Mine2d.game.core.data; + +public class ItemStack +{ + public ItemId Id { get; set; } + public int Count { get; set; } +} diff --git a/Mine2d/game/frontend/GameAudio.cs b/Mine2d/game/frontend/GameAudio.cs index fd921e2..e8bf752 100644 --- a/Mine2d/game/frontend/GameAudio.cs +++ b/Mine2d/game/frontend/GameAudio.cs @@ -10,7 +10,7 @@ public class GameAudio { this.audioPlayer = new(); this.audioPlayer.Register(Sound.BlockBreak, "assets.audio.block_break.wav"); - this.audioPlayer.Register(Sound.BlockHit, "assets.audio.block_hit.wav"); + this.audioPlayer.Register(Sound.BlockHit, "assets.audio.block_hit_alt.wav"); this.audioPlayer.Register(Sound.ItemPickup, "assets.audio.item_pickup.wav"); } diff --git a/Mine2d/game/state/GameState.cs b/Mine2d/game/state/GameState.cs index c33c2e8..92c6a5e 100644 --- a/Mine2d/game/state/GameState.cs +++ b/Mine2d/game/state/GameState.cs @@ -18,7 +18,7 @@ public class FrontendGameState public class Settings { - public int GameScale { get; set; } = 4; + public int GameScale { get; set; } = 6; public int UiScale { get; set; } = 4; public bool ShowCollision { get; set; } = true; public bool Fullscreen { get; set; } = false; diff --git a/Mine2d/game/state/Player.cs b/Mine2d/game/state/Player.cs index d1f4b18..18c3ac8 100644 --- a/Mine2d/game/state/Player.cs +++ b/Mine2d/game/state/Player.cs @@ -8,6 +8,7 @@ public class Player public Guid Id { get; set; } public Vector2 Mining { get; set; } public int MiningCooldown { get; set; } + public PlayerInventory inventory { get; set; } = new(); public Vector2 GetCenter() { diff --git a/Mine2d/game/state/PlayerInventory.cs b/Mine2d/game/state/PlayerInventory.cs new file mode 100644 index 0000000..1c164a1 --- /dev/null +++ b/Mine2d/game/state/PlayerInventory.cs @@ -0,0 +1,27 @@ +using Mine2d.game.core; +using Mine2d.game.core.data; + +namespace Mine2d.game.state; + +public class PlayerInventory +{ + public ItemStack[] Hotbar { get; set; } = new ItemStack[9]; + + public bool PickupItemStack(ItemStack itemStack) + { + var slot = InventoryUtils.GetFirstMatchingSlot(this.Hotbar, itemStack.Id); + if (slot == -1) + { + return false; + } + if (this.Hotbar[slot] == null) + { + this.Hotbar[slot] = itemStack; + } + else + { + this.Hotbar[slot].Count += itemStack.Count; + } + return true; + } +}