added inventory item rendering
This commit is contained in:
parent
823fcc8aed
commit
c2fd66a8f0
|
|
@ -58,7 +58,6 @@ public class Publisher
|
||||||
Console.WriteLine("Subscribed!");
|
Console.WriteLine("Subscribed!");
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,16 +9,14 @@ namespace Mine2d.game.backend.interactor;
|
||||||
[Interactor]
|
[Interactor]
|
||||||
public class ItemPhysics
|
public class ItemPhysics
|
||||||
{
|
{
|
||||||
|
|
||||||
[Interaction(InteractorKind.Hybrid, PacketType.Tick)]
|
[Interaction(InteractorKind.Hybrid, PacketType.Tick)]
|
||||||
public static void TickHybrid(TickPacket packet)
|
public static void TickHybrid()
|
||||||
{
|
{
|
||||||
var gameState = Context.Get().GameState;
|
var gameState = Context.Get().GameState;
|
||||||
var world = gameState.World;
|
var world = gameState.World;
|
||||||
foreach (var chunk in world.Chunks)
|
foreach (var chunk in world.Chunks)
|
||||||
{
|
{
|
||||||
var entities = chunk.Value.Entities;
|
foreach (var entity in chunk.Value.Entities)
|
||||||
foreach (var entity in entities)
|
|
||||||
{
|
{
|
||||||
if (entity is ItemEntity itemEntity)
|
if (entity is ItemEntity itemEntity)
|
||||||
{
|
{
|
||||||
|
|
@ -37,7 +35,7 @@ public class ItemPhysics
|
||||||
}
|
}
|
||||||
|
|
||||||
[Interaction(InteractorKind.Hybrid, PacketType.Tick)]
|
[Interaction(InteractorKind.Hybrid, PacketType.Tick)]
|
||||||
public static void Pickup(TickPacket packet)
|
public static void Pickup()
|
||||||
{
|
{
|
||||||
var gameState = Context.Get().GameState;
|
var gameState = Context.Get().GameState;
|
||||||
var world = gameState.World;
|
var world = gameState.World;
|
||||||
|
|
@ -45,7 +43,6 @@ public class ItemPhysics
|
||||||
{
|
{
|
||||||
foreach (var player in gameState.Players)
|
foreach (var player in gameState.Players)
|
||||||
{
|
{
|
||||||
|
|
||||||
var items = chunk.Value.Entities.Where(e =>
|
var items = chunk.Value.Entities.Where(e =>
|
||||||
{
|
{
|
||||||
return e is ItemEntity itemEntity &&
|
return e is ItemEntity itemEntity &&
|
||||||
|
|
@ -55,9 +52,8 @@ public class ItemPhysics
|
||||||
if (items.Any())
|
if (items.Any())
|
||||||
{
|
{
|
||||||
Context.Get().GameAudio.Play(Sound.ItemPickup);
|
Context.Get().GameAudio.Play(Sound.ItemPickup);
|
||||||
Console.WriteLine(packet.Tick + " " + items.Count());
|
|
||||||
}
|
}
|
||||||
_ = chunk.Value.Entities.RemoveAll(e => items.Contains(e));
|
_ = chunk.Value.Entities.RemoveAll(items.Contains);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,10 +12,9 @@ public class PlayerEntity
|
||||||
public static Player GetSelf()
|
public static Player GetSelf()
|
||||||
{
|
{
|
||||||
var ctx = Context.Get();
|
var ctx = Context.Get();
|
||||||
var player = ctx.GameState.Players.FirstOrDefault(
|
return ctx.GameState.Players.Find(
|
||||||
p => p.Id == ctx.FrontendGameState.PlayerGuid
|
p => p.Id == ctx.FrontendGameState.PlayerGuid
|
||||||
);
|
);
|
||||||
return player;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Move(Player p)
|
public static void Move(Player p)
|
||||||
|
|
@ -92,7 +91,7 @@ public class PlayerEntity
|
||||||
public static bool HasCollisionWithAnyPlayer(Vector2 pos)
|
public static bool HasCollisionWithAnyPlayer(Vector2 pos)
|
||||||
{
|
{
|
||||||
var ctx = Context.Get();
|
var ctx = Context.Get();
|
||||||
var ts = Constants.TileSize;
|
const int ts = Constants.TileSize;
|
||||||
var tilePos = new Vector2(pos.X - pos.X % ts, pos.Y - pos.Y % ts);
|
var tilePos = new Vector2(pos.X - pos.X % ts, pos.Y - pos.Y % ts);
|
||||||
return ctx.GameState.Players.Any(
|
return ctx.GameState.Players.Any(
|
||||||
player =>
|
player =>
|
||||||
|
|
@ -113,8 +112,6 @@ public class PlayerEntity
|
||||||
w = 16,
|
w = 16,
|
||||||
h = 16
|
h = 16
|
||||||
};
|
};
|
||||||
Console.WriteLine("playerRect: " + playerRect.x + ", " + playerRect.y + ", " + playerRect.w + ", " + playerRect.h);
|
|
||||||
Console.WriteLine("tileRect: " + tileRect.x + ", " + tileRect.y + ", " + tileRect.w + ", " + tileRect.h);
|
|
||||||
return SDL_HasIntersection(ref playerRect, ref tileRect) == SDL_bool.SDL_TRUE;
|
return SDL_HasIntersection(ref playerRect, ref tileRect) == SDL_bool.SDL_TRUE;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
using Mine2d.game.core;
|
||||||
|
|
||||||
namespace Mine2d.game.frontend.inventory
|
namespace Mine2d.game.frontend.inventory
|
||||||
{
|
{
|
||||||
public class PlayerInventoryRenderer : IInventoryRenderer
|
public class PlayerInventoryRenderer : IInventoryRenderer
|
||||||
|
|
@ -7,17 +9,84 @@ namespace Mine2d.game.frontend.inventory
|
||||||
public void Render()
|
public void Render()
|
||||||
{
|
{
|
||||||
var ctx = Context.Get();
|
var ctx = Context.Get();
|
||||||
if(this.texture == IntPtr.Zero) {
|
if (this.texture == IntPtr.Zero)
|
||||||
|
{
|
||||||
this.texture = ctx.TextureFactory.LoadTexture("hud.player-inventory");
|
this.texture = ctx.TextureFactory.LoadTexture("hud.player-inventory");
|
||||||
}
|
}
|
||||||
var (width, height) = ctx.Renderer.GetTextureSize(this.texture);
|
var (width, height) = ctx.Renderer.GetTextureSize(this.texture);
|
||||||
width *= ctx.FrontendGameState.Settings.UiScale;
|
var uiScale = ctx.FrontendGameState.Settings.UiScale;
|
||||||
height *= ctx.FrontendGameState.Settings.UiScale;
|
width *= uiScale;
|
||||||
var extraSlotsWidth = InventoryConstants.ExtraSlotsWidth * ctx.FrontendGameState.Settings.UiScale;
|
height *= uiScale;
|
||||||
|
var extraSlotsWidth = InventoryConstants.ExtraSlotsWidth * uiScale;
|
||||||
var (windowWidth, windowHeight) = (ctx.FrontendGameState.WindowWidth, ctx.FrontendGameState.WindowHeight);
|
var (windowWidth, windowHeight) = (ctx.FrontendGameState.WindowWidth, ctx.FrontendGameState.WindowHeight);
|
||||||
var x = (windowWidth - (width - extraSlotsWidth)) / 2;
|
var x = (windowWidth - (width - extraSlotsWidth)) / 2;
|
||||||
var y = (windowHeight - height) / 2;
|
var y = (windowHeight - height) / 2;
|
||||||
ctx.Renderer.DrawTexture(this.texture, x, y, width, height);
|
ctx.Renderer.DrawTexture(this.texture, x, y, width, height);
|
||||||
|
|
||||||
|
var player = PlayerEntity.GetSelf();
|
||||||
|
var inventory = player.Inventory.Inventory;
|
||||||
|
var hotbar = player.Inventory.Hotbar;
|
||||||
|
var cursorPosition = Context.Get().FrontendGameState.CursorPosition;
|
||||||
|
for (var i = 0; i < hotbar.Length; i++)
|
||||||
|
{
|
||||||
|
var stack = hotbar[i];
|
||||||
|
if (stack == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var itemTexture = stack.GetTexture();
|
||||||
|
ctx.Renderer.DrawTexture(
|
||||||
|
itemTexture,
|
||||||
|
((4 + (i * 22)) * uiScale) + x,
|
||||||
|
(4 * uiScale) + y,
|
||||||
|
16 * uiScale,
|
||||||
|
16 * uiScale
|
||||||
|
);
|
||||||
|
ctx.Renderer.DrawText(
|
||||||
|
"" + stack.Count,
|
||||||
|
((4 + (i * 22)) * uiScale) + x,
|
||||||
|
(14 * uiScale) + y
|
||||||
|
);
|
||||||
|
if (cursorPosition.X >= ((4 + (i * 22)) * uiScale) + x
|
||||||
|
&& cursorPosition.X <= ((4 + (i * 22)) * uiScale) + x + (16 * uiScale)
|
||||||
|
&& cursorPosition.Y >= (4 * uiScale) + y
|
||||||
|
&& cursorPosition.Y <= (4 * uiScale) + y + (16 * uiScale)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
Context.Get().FrontendGameState.Tooltip = stack.GetName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (var i = 0; i < inventory.Length; i++)
|
||||||
|
{
|
||||||
|
var stack = inventory[i];
|
||||||
|
if (stack == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var itemTexture = stack.GetTexture();
|
||||||
|
ctx.Renderer.DrawTexture(
|
||||||
|
itemTexture,
|
||||||
|
((4 + ((i % 9) * 22)) * uiScale) + x,
|
||||||
|
((4 + 21 + ((i / 9) * 22)) * uiScale) + y,
|
||||||
|
16 * uiScale,
|
||||||
|
16 * uiScale
|
||||||
|
);
|
||||||
|
ctx.Renderer.DrawText(
|
||||||
|
"" + stack.Count,
|
||||||
|
((4 + ((i % 9) * 22)) * uiScale) + x,
|
||||||
|
((14 + 21 + ((i / 9) * 22)) * uiScale) + y
|
||||||
|
);
|
||||||
|
if (cursorPosition.X >= ((4 + ((i % 9) * 22)) * uiScale) + x
|
||||||
|
&& cursorPosition.X <= ((4 + ((i % 9) * 22)) * uiScale) + x + (16 * uiScale)
|
||||||
|
&& cursorPosition.Y >= ((4 + 21 + ((i / 9) * 22)) * uiScale) + y
|
||||||
|
&& cursorPosition.Y <= ((4 + 21 + ((i / 9) * 22)) * uiScale) + y + (16 * uiScale)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
Context.Get().FrontendGameState.Tooltip = stack.GetName();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -54,8 +54,8 @@ public class HudRenderer : IRenderer
|
||||||
|
|
||||||
var texture = stack.GetTexture();
|
var texture = stack.GetTexture();
|
||||||
renderer.DrawTexture(texture, (4 + i * 20) * uiScale, 4 * uiScale, 16 * uiScale, 16 * uiScale);
|
renderer.DrawTexture(texture, (4 + i * 20) * uiScale, 4 * uiScale, 16 * uiScale, 16 * uiScale);
|
||||||
renderer.DrawText("" + stack.Count, (4 + i * 20) * uiScale, 18 * uiScale);
|
renderer.DrawText("" + stack.Count, (4 + i * 20) * uiScale, 14 * uiScale);
|
||||||
if(cursorPosition.X >= (4 + i * 20) * uiScale && cursorPosition.X <= (4 + i * 20 + 16) * uiScale && cursorPosition.Y >= 4 * uiScale && cursorPosition.Y <= (4 + 16) * uiScale)
|
if (cursorPosition.X >= (4 + i * 20) * uiScale && cursorPosition.X <= (4 + i * 20 + 16) * uiScale && cursorPosition.Y >= 4 * uiScale && cursorPosition.Y <= (4 + 16) * uiScale)
|
||||||
{
|
{
|
||||||
Context.Get().FrontendGameState.Tooltip = stack.GetName();
|
Context.Get().FrontendGameState.Tooltip = stack.GetName();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,6 @@ namespace Mine2d.game.frontend.renderer;
|
||||||
|
|
||||||
public class TooltipRenderer : IRenderer
|
public class TooltipRenderer : IRenderer
|
||||||
{
|
{
|
||||||
private const int TooltipTextureWidth = 9;
|
|
||||||
private const int TooltipTextureHeight = 9;
|
|
||||||
private readonly IntPtr tooltipTexture;
|
private readonly IntPtr tooltipTexture;
|
||||||
|
|
||||||
public TooltipRenderer()
|
public TooltipRenderer()
|
||||||
|
|
@ -22,7 +20,7 @@ public class TooltipRenderer : IRenderer
|
||||||
{
|
{
|
||||||
var ctx = Context.Get();
|
var ctx = Context.Get();
|
||||||
var tooltip = ctx.FrontendGameState.Tooltip;
|
var tooltip = ctx.FrontendGameState.Tooltip;
|
||||||
if(tooltip == null || tooltip.Trim().Length == 0)
|
if (tooltip == null || tooltip.Trim().Length == 0)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -68,7 +66,6 @@ public class TooltipRenderer : IRenderer
|
||||||
4
|
4
|
||||||
);
|
);
|
||||||
|
|
||||||
Console.WriteLine(height);
|
|
||||||
renderer.DrawTexture(
|
renderer.DrawTexture(
|
||||||
this.tooltipTexture,
|
this.tooltipTexture,
|
||||||
tooltipX,
|
tooltipX,
|
||||||
|
|
|
||||||
|
|
@ -6,22 +6,27 @@ namespace Mine2d.game.state;
|
||||||
public class PlayerInventory
|
public class PlayerInventory
|
||||||
{
|
{
|
||||||
public ItemStack[] Hotbar { get; set; } = new ItemStack[9];
|
public ItemStack[] Hotbar { get; set; } = new ItemStack[9];
|
||||||
|
public ItemStack[] Inventory { get; set; } = new ItemStack[5 * 9];
|
||||||
|
|
||||||
public bool PickupItemStack(ItemStack itemStack)
|
public bool PickupItemStack(ItemStack itemStack)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Picking up" + itemStack.Id + " " + itemStack.Count);
|
return PickupItemStack(itemStack, this.Hotbar) || PickupItemStack(itemStack, this.Inventory);
|
||||||
var slot = InventoryUtils.GetFirstMatchingSlot(this.Hotbar, itemStack.Id);
|
}
|
||||||
|
|
||||||
|
public static bool PickupItemStack(ItemStack itemStack, ItemStack[] inventory)
|
||||||
|
{
|
||||||
|
var slot = InventoryUtils.GetFirstMatchingSlot(inventory, itemStack.Id);
|
||||||
if (slot == -1)
|
if (slot == -1)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (this.Hotbar[slot] == null)
|
if (inventory[slot] == null)
|
||||||
{
|
{
|
||||||
this.Hotbar[slot] = itemStack;
|
inventory[slot] = itemStack;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.Hotbar[slot].Count += itemStack.Count;
|
inventory[slot].Count += itemStack.Count;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue