added inventory item rendering
This commit is contained in:
parent
823fcc8aed
commit
c2fd66a8f0
|
|
@ -19,7 +19,7 @@ public class Publisher
|
|||
{
|
||||
Enum.GetValues<PacketType>()
|
||||
.ForEach(type => this.subscribers[type] = new HashSet<MethodInfo>());
|
||||
|
||||
|
||||
this.kind = kind;
|
||||
this.Scan();
|
||||
}
|
||||
|
|
@ -38,7 +38,7 @@ public class Publisher
|
|||
Console.WriteLine($"Registering interaction method {method.Name} declared in {method.DeclaringType}");
|
||||
Console.WriteLine($"InteractorKind: {attribute.Kind}");
|
||||
Console.WriteLine($"PacketType: {attribute.Type}");
|
||||
|
||||
|
||||
switch (attribute.Kind)
|
||||
{
|
||||
case InteractorKind.Hybrid:
|
||||
|
|
@ -51,14 +51,13 @@ public class Publisher
|
|||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(attribute.Kind), attribute.Kind, null);
|
||||
}
|
||||
|
||||
|
||||
if (attribute.Kind == this.kind || this.kind == InteractorKind.Hybrid)
|
||||
{
|
||||
this.subscribers[attribute.Type].Add(method);
|
||||
Console.WriteLine("Subscribed!");
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -80,7 +79,7 @@ public class Publisher
|
|||
|
||||
public bool IsServerPacket(Packet packet)
|
||||
=> this.serverSubscriptions.Contains(packet.Type);
|
||||
|
||||
|
||||
public void Dump()
|
||||
{
|
||||
foreach (var pair in this.subscribers)
|
||||
|
|
@ -92,4 +91,4 @@ public class Publisher
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,16 +9,14 @@ namespace Mine2d.game.backend.interactor;
|
|||
[Interactor]
|
||||
public class ItemPhysics
|
||||
{
|
||||
|
||||
[Interaction(InteractorKind.Hybrid, PacketType.Tick)]
|
||||
public static void TickHybrid(TickPacket packet)
|
||||
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)
|
||||
foreach (var entity in chunk.Value.Entities)
|
||||
{
|
||||
if (entity is ItemEntity itemEntity)
|
||||
{
|
||||
|
|
@ -37,7 +35,7 @@ public class ItemPhysics
|
|||
}
|
||||
|
||||
[Interaction(InteractorKind.Hybrid, PacketType.Tick)]
|
||||
public static void Pickup(TickPacket packet)
|
||||
public static void Pickup()
|
||||
{
|
||||
var gameState = Context.Get().GameState;
|
||||
var world = gameState.World;
|
||||
|
|
@ -45,7 +43,6 @@ public class ItemPhysics
|
|||
{
|
||||
foreach (var player in gameState.Players)
|
||||
{
|
||||
|
||||
var items = chunk.Value.Entities.Where(e =>
|
||||
{
|
||||
return e is ItemEntity itemEntity &&
|
||||
|
|
@ -55,9 +52,8 @@ public class ItemPhysics
|
|||
if (items.Any())
|
||||
{
|
||||
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()
|
||||
{
|
||||
var ctx = Context.Get();
|
||||
var player = ctx.GameState.Players.FirstOrDefault(
|
||||
return ctx.GameState.Players.Find(
|
||||
p => p.Id == ctx.FrontendGameState.PlayerGuid
|
||||
);
|
||||
return player;
|
||||
}
|
||||
|
||||
public static void Move(Player p)
|
||||
|
|
@ -92,7 +91,7 @@ public class PlayerEntity
|
|||
public static bool HasCollisionWithAnyPlayer(Vector2 pos)
|
||||
{
|
||||
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);
|
||||
return ctx.GameState.Players.Any(
|
||||
player =>
|
||||
|
|
@ -113,8 +112,6 @@ public class PlayerEntity
|
|||
w = 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;
|
||||
}
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
using Mine2d.game.core;
|
||||
|
||||
namespace Mine2d.game.frontend.inventory
|
||||
{
|
||||
public class PlayerInventoryRenderer : IInventoryRenderer
|
||||
|
|
@ -7,17 +9,84 @@ namespace Mine2d.game.frontend.inventory
|
|||
public void Render()
|
||||
{
|
||||
var ctx = Context.Get();
|
||||
if(this.texture == IntPtr.Zero) {
|
||||
if (this.texture == IntPtr.Zero)
|
||||
{
|
||||
this.texture = ctx.TextureFactory.LoadTexture("hud.player-inventory");
|
||||
}
|
||||
var (width, height) = ctx.Renderer.GetTextureSize(this.texture);
|
||||
width *= ctx.FrontendGameState.Settings.UiScale;
|
||||
height *= ctx.FrontendGameState.Settings.UiScale;
|
||||
var extraSlotsWidth = InventoryConstants.ExtraSlotsWidth * ctx.FrontendGameState.Settings.UiScale;
|
||||
var uiScale = ctx.FrontendGameState.Settings.UiScale;
|
||||
width *= uiScale;
|
||||
height *= uiScale;
|
||||
var extraSlotsWidth = InventoryConstants.ExtraSlotsWidth * uiScale;
|
||||
var (windowWidth, windowHeight) = (ctx.FrontendGameState.WindowWidth, ctx.FrontendGameState.WindowHeight);
|
||||
var x = (windowWidth - (width - extraSlotsWidth)) / 2;
|
||||
var y = (windowHeight - height) / 2;
|
||||
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();
|
||||
renderer.DrawTexture(texture, (4 + i * 20) * uiScale, 4 * uiScale, 16 * uiScale, 16 * uiScale);
|
||||
renderer.DrawText("" + stack.Count, (4 + i * 20) * uiScale, 18 * uiScale);
|
||||
if(cursorPosition.X >= (4 + i * 20) * uiScale && cursorPosition.X <= (4 + i * 20 + 16) * uiScale && cursorPosition.Y >= 4 * uiScale && cursorPosition.Y <= (4 + 16) * 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)
|
||||
{
|
||||
Context.Get().FrontendGameState.Tooltip = stack.GetName();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,8 +4,6 @@ namespace Mine2d.game.frontend.renderer;
|
|||
|
||||
public class TooltipRenderer : IRenderer
|
||||
{
|
||||
private const int TooltipTextureWidth = 9;
|
||||
private const int TooltipTextureHeight = 9;
|
||||
private readonly IntPtr tooltipTexture;
|
||||
|
||||
public TooltipRenderer()
|
||||
|
|
@ -22,7 +20,7 @@ public class TooltipRenderer : IRenderer
|
|||
{
|
||||
var ctx = Context.Get();
|
||||
var tooltip = ctx.FrontendGameState.Tooltip;
|
||||
if(tooltip == null || tooltip.Trim().Length == 0)
|
||||
if (tooltip == null || tooltip.Trim().Length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
|
@ -42,7 +40,7 @@ public class TooltipRenderer : IRenderer
|
|||
0,
|
||||
4,
|
||||
4
|
||||
);
|
||||
);
|
||||
|
||||
renderer.DrawTexture(
|
||||
this.tooltipTexture,
|
||||
|
|
@ -68,7 +66,6 @@ public class TooltipRenderer : IRenderer
|
|||
4
|
||||
);
|
||||
|
||||
Console.WriteLine(height);
|
||||
renderer.DrawTexture(
|
||||
this.tooltipTexture,
|
||||
tooltipX,
|
||||
|
|
@ -153,4 +150,4 @@ public class TooltipRenderer : IRenderer
|
|||
|
||||
SDL_FreeSurface(surfaceMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,22 +6,27 @@ namespace Mine2d.game.state;
|
|||
public class PlayerInventory
|
||||
{
|
||||
public ItemStack[] Hotbar { get; set; } = new ItemStack[9];
|
||||
public ItemStack[] Inventory { get; set; } = new ItemStack[5 * 9];
|
||||
|
||||
public bool PickupItemStack(ItemStack itemStack)
|
||||
{
|
||||
Console.WriteLine("Picking up" + itemStack.Id + " " + itemStack.Count);
|
||||
var slot = InventoryUtils.GetFirstMatchingSlot(this.Hotbar, itemStack.Id);
|
||||
return PickupItemStack(itemStack, this.Hotbar) || PickupItemStack(itemStack, this.Inventory);
|
||||
}
|
||||
|
||||
public static bool PickupItemStack(ItemStack itemStack, ItemStack[] inventory)
|
||||
{
|
||||
var slot = InventoryUtils.GetFirstMatchingSlot(inventory, itemStack.Id);
|
||||
if (slot == -1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (this.Hotbar[slot] == null)
|
||||
if (inventory[slot] == null)
|
||||
{
|
||||
this.Hotbar[slot] = itemStack;
|
||||
inventory[slot] = itemStack;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Hotbar[slot].Count += itemStack.Count;
|
||||
inventory[slot].Count += itemStack.Count;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue