added hotbar
This commit is contained in:
parent
8d686b0f84
commit
fa037677e4
61
Mine2d/+
61
Mine2d/+
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 277 B |
|
|
@ -1,5 +1,6 @@
|
||||||
using Mine2d.engine;
|
using Mine2d.engine;
|
||||||
using Mine2d.engine.system.annotations;
|
using Mine2d.engine.system.annotations;
|
||||||
|
using Mine2d.game.core.data;
|
||||||
using Mine2d.game.core.data.entities;
|
using Mine2d.game.core.data.entities;
|
||||||
|
|
||||||
namespace Mine2d.game.backend.interactor;
|
namespace Mine2d.game.backend.interactor;
|
||||||
|
|
@ -44,14 +45,17 @@ public class ItemPhysics
|
||||||
foreach (var player in gameState.Players)
|
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);
|
Context.Get().GameAudio.Play(Sound.ItemPickup);
|
||||||
}
|
}
|
||||||
|
_ = chunk.Value.Entities.RemoveAll(e => items.Contains(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -22,6 +22,13 @@ public class PlayerEntity
|
||||||
{
|
{
|
||||||
p.Movement += Constants.Gravity;
|
p.Movement += Constants.Gravity;
|
||||||
p.Position += p.Movement;
|
p.Position += p.Movement;
|
||||||
|
if (p.Movement.Y > 8)
|
||||||
|
{
|
||||||
|
p.Movement = p.Movement with
|
||||||
|
{
|
||||||
|
Y = 8
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Collide(Player p)
|
public static void Collide(Player p)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
namespace Mine2d.game.core.data;
|
||||||
|
|
||||||
|
public class ItemStack
|
||||||
|
{
|
||||||
|
public ItemId Id { get; set; }
|
||||||
|
public int Count { get; set; }
|
||||||
|
}
|
||||||
|
|
@ -10,7 +10,7 @@ public class GameAudio
|
||||||
{
|
{
|
||||||
this.audioPlayer = new();
|
this.audioPlayer = new();
|
||||||
this.audioPlayer.Register(Sound.BlockBreak, "assets.audio.block_break.wav");
|
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");
|
this.audioPlayer.Register(Sound.ItemPickup, "assets.audio.item_pickup.wav");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ public class FrontendGameState
|
||||||
|
|
||||||
public class Settings
|
public class Settings
|
||||||
{
|
{
|
||||||
public int GameScale { get; set; } = 4;
|
public int GameScale { get; set; } = 6;
|
||||||
public int UiScale { get; set; } = 4;
|
public int UiScale { get; set; } = 4;
|
||||||
public bool ShowCollision { get; set; } = true;
|
public bool ShowCollision { get; set; } = true;
|
||||||
public bool Fullscreen { get; set; } = false;
|
public bool Fullscreen { get; set; } = false;
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ public class Player
|
||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
public Vector2 Mining { get; set; }
|
public Vector2 Mining { get; set; }
|
||||||
public int MiningCooldown { get; set; }
|
public int MiningCooldown { get; set; }
|
||||||
|
public PlayerInventory inventory { get; set; } = new();
|
||||||
|
|
||||||
public Vector2 GetCenter()
|
public Vector2 GetCenter()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue