added crafting

This commit is contained in:
MasterGordon 2023-01-02 20:02:11 +01:00
parent 1bc8e5a56f
commit a12b4450c5
19 changed files with 221 additions and 31 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 200 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 191 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 229 B

View File

@ -15,6 +15,12 @@ public class PlayerInteract
{
return;
}
if (ctx.GameState.World.HasTileAt(packet.Target))
{
var tile = ctx.TileRegistry.GetTile(ctx.GameState.World.GetTileAt(packet.Target).Id);
tile.OnInteract(packet.Target);
return;
}
var stack = player.Inventory.Hotbar[packet.Slot];
if (stack == null || stack.Count <= 0)
{

View File

@ -16,6 +16,11 @@ public enum ItemId
RawTungsten = 115,
RawUranium = 116,
Diamond = 117,
Coal = 120,
GoldIngot = 121,
IronIngot = 122,
PickaxeBasic = 200,
PickaxeStone = 201,
PickaxeIron = 202,
PickaxeGold = 203,
}

View File

@ -72,6 +72,11 @@ public class World
return new Vector2((float)chunkX, (float)chunkY);
}
public STile GetTileAt(Vector2 pos)
{
return this.GetTileAt((int)pos.X, (int)pos.Y);
}
public STile GetTileAt(int x, int y)
{
return this.GetChunkAt(x, y).GetTileAt(x, y);
@ -82,6 +87,11 @@ public class World
this.GetChunkAt(x, y).SetTileAt(x, y, tile);
}
public bool HasTileAt(Vector2 pos)
{
return this.HasTileAt((int)pos.X, (int)pos.Y);
}
public bool HasTileAt(int x, int y)
{
return this.HasChunkAt(x, y) && this.GetChunkAt(x, y).HasTileAt(new Vector2(x, y));

View File

@ -30,8 +30,13 @@ public class ItemRegistry
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" ));
this.Register(ItemId.Coal, new Item(ItemId.Coal, "Coal", "items.coal" ));
this.Register(ItemId.IronIngot, new Item(ItemId.IronIngot, "Iron Ingot", "items.ingot-iron" ));
this.Register(ItemId.GoldIngot, new Item(ItemId.GoldIngot, "Gold Ingot", "items.ingot-gold" ));
this.Register(ItemId.PickaxeBasic, new PickaxeItem(ItemId.PickaxeBasic, "Basic Pickaxe", "items.pickaxe-basic", 15, 4));
this.Register(ItemId.PickaxeStone, new PickaxeItem(ItemId.PickaxeStone, "Stone Pickaxe", "items.pickaxe-stone", 25, 6));
this.Register(ItemId.PickaxeIron, new PickaxeItem(ItemId.PickaxeIron, "Iron Pickaxe", "items.pickaxe-iron", 40, 7));
this.Register(ItemId.PickaxeGold, new PickaxeItem(ItemId.PickaxeGold, "Gold Pickaxe", "items.pickaxe-gold", 60, 8));
}
public void Register(ItemId id, Item item)

View File

@ -97,4 +97,8 @@ public class Tile
{
return true;
}
public virtual void OnInteract(Vector2 position)
{
}
}

View File

@ -23,6 +23,7 @@ public enum Tiles
DiamondOre = 17,
DripstoneUp = 18,
DripstoneDown = 19,
CoalOre = 20,
}
public class TileRegistry
@ -50,6 +51,7 @@ public class TileRegistry
this.Tiles.Add(17, new OreTile("diamond-ore", new[] { "stone", "diamond-ore" }, 10, ItemId.Diamond));
this.Tiles.Add(18, new DecoTile("dripstone-up", "dripstone-up", 5, ItemId.Air));
this.Tiles.Add(19, new DecoTile("dripstone-down", "dripstone-down", 5, ItemId.Air));
this.Tiles.Add(20, new OreTile("coal-ore", new[] { "stone", "coal-ore" }, 4, ItemId.Coal));
}
public Tile GetTile(int id)

View File

@ -1,15 +1,78 @@
using Mine2d.game.core.data;
using Mine2d.game.frontend.inventory;
namespace Mine2d.game.core.tiles;
public class Recipe {
public ItemStack Result { get; set; }
public List<ItemStack> Ingredients { get; set; } = new();
}
public class Workbench : Tile
{
public static List<Recipe> Recipes { get; set; } = new();
public Workbench(string name, string texturePath, int hardness) : base(name, texturePath, hardness, ItemId.Workbench)
{
Recipes.Add(new Recipe
{
Result = new ItemStack(ItemId.PickaxeBasic, 1),
Ingredients = new List<ItemStack>
{
new ItemStack(ItemId.Stone, 2),
}
});
Recipes.Add(new Recipe
{
Result = new ItemStack(ItemId.PickaxeStone, 1),
Ingredients = new List<ItemStack>
{
new ItemStack(ItemId.Stone, 10),
}
});
Recipes.Add(new Recipe
{
Result = new ItemStack(ItemId.PickaxeIron, 1),
Ingredients = new List<ItemStack>
{
new ItemStack(ItemId.IronIngot, 10),
}
});
Recipes.Add(new Recipe
{
Result = new ItemStack(ItemId.PickaxeGold, 1),
Ingredients = new List<ItemStack>
{
new ItemStack(ItemId.GoldIngot, 10),
}
});
Recipes.Add(new Recipe
{
Result = new ItemStack(ItemId.IronIngot, 1),
Ingredients = new List<ItemStack>
{
new ItemStack(ItemId.Coal, 1),
new ItemStack(ItemId.RawIron, 1),
}
});
Recipes.Add(new Recipe
{
Result = new ItemStack(ItemId.GoldIngot, 1),
Ingredients = new List<ItemStack>
{
new ItemStack(ItemId.Coal, 1),
new ItemStack(ItemId.RawGold, 1),
}
});
}
public override bool IsSolid()
{
return false;
}
public override void OnInteract(Vector2 position)
{
Context.Get().FrontendGameState.OpenInventory = InventoryKind.Workbench;
}
}

View File

@ -65,6 +65,10 @@ public class ChunkGenerator
else
{
fill.Id = (int)WG.GetRandomOreAt(j + (y * 32));
if (new Random().NextInt64(0, 130) < 1)
{
fill.Id = (int)Tiles.CoalOre;
}
chunk.SetTile(i, j, fill);
}
}

View File

@ -10,7 +10,7 @@ public static class ItemRenderer
{
private static readonly Dictionary<int, (IntPtr, int, int)> Textures = new();
public static void RenderItemStack(ItemStack stack, Vector2 position, bool renderTooltip = true)
public static void RenderItemStack(ItemStack stack, Vector2 position, bool renderTooltip = true, string description = null)
{
var texture = stack.GetTexture();
var renderer = Context.Get().Renderer;
@ -36,7 +36,7 @@ public static class ItemRenderer
&& cursorPosition.Y <= position.Y + (16 * uiScale)
)
{
Context.Get().FrontendGameState.Tooltip = stack.GetName();
Context.Get().FrontendGameState.Tooltip = stack.GetName() + (description != null ? "\n" + description : "");
}
}
}

View File

@ -23,7 +23,12 @@ public class PlayerInventoryRenderer : Inventory
this.x = (windowWidth - (width - extraSlotsWidth)) / 2;
this.y = (windowHeight - height) / 2;
ctx.Renderer.DrawTexture(this.texture, this.x, this.y, width, height);
Render(this.x, this.y);
}
public static void Render(int x, int y) {
var ctx = Context.Get();
var uiScale = ctx.FrontendGameState.Settings.UiScale;
var player = PlayerEntity.GetSelf();
var inventory = player.Inventory.Inventory;
var hotbar = player.Inventory.Hotbar;
@ -34,7 +39,7 @@ public class PlayerInventoryRenderer : Inventory
{
continue;
}
ItemRenderer.RenderItemStack(stack, new Vector2(((4 + (i * 21)) * uiScale) + this.x, (4 * uiScale) + this.y), player.Inventory.Cursor == null);
ItemRenderer.RenderItemStack(stack, new Vector2(((4 + (i * 21)) * uiScale) + x, (4 * uiScale) + y), player.Inventory.Cursor == null);
}
for (var i = 0; i < inventory.Length; i++)
{
@ -44,28 +49,33 @@ public class PlayerInventoryRenderer : Inventory
continue;
}
ItemRenderer.RenderItemStack(stack, new Vector2(((4 + ((i % 9) * 21)) * uiScale) + this.x, ((4 + 21 + ((i / 9) * 21)) * uiScale) + this.y), player.Inventory.Cursor == null);
ItemRenderer.RenderItemStack(stack, new Vector2(((4 + ((i % 9) * 21)) * uiScale) + x, ((4 + 21 + ((i / 9) * 21)) * uiScale) + y), player.Inventory.Cursor == null);
}
var pickaxe = player.Inventory.Pickaxe;
if (pickaxe != null)
{
ItemRenderer.RenderItemStack(pickaxe, new Vector2(((4 + (9 * 21)) * uiScale) + this.x, (((5 * 21) + 4) * uiScale) + this.y), player.Inventory.Cursor == null);
ItemRenderer.RenderItemStack(pickaxe, new Vector2(((4 + (9 * 21)) * uiScale) + x, (((5 * 21) + 4) * uiScale) + y), player.Inventory.Cursor == null, "Test\nTT");
}
}
public override void OnClick(SDL_Event e)
{
OnClick(this.x, this.y, e);
}
public static void OnClick(int x, int y, SDL_Event e)
{
var cursorPosition = Context.Get().FrontendGameState.CursorPosition;
var player = PlayerEntity.GetSelf();
// is hotbar
if (cursorPosition.X >= this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale)
&& cursorPosition.X <= this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 9 * Context.Get().FrontendGameState.Settings.UiScale)
&& cursorPosition.Y >= this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale)
&& cursorPosition.Y <= this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * Context.Get().FrontendGameState.Settings.UiScale)
if (cursorPosition.X >= x + (4 * Context.Get().FrontendGameState.Settings.UiScale)
&& cursorPosition.X <= x + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 9 * Context.Get().FrontendGameState.Settings.UiScale)
&& cursorPosition.Y >= y + (4 * Context.Get().FrontendGameState.Settings.UiScale)
&& cursorPosition.Y <= y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * Context.Get().FrontendGameState.Settings.UiScale)
)
{
var hotbar = player.Inventory.Hotbar;
var index = (int)((cursorPosition.X - (this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale))) / (21 * Context.Get().FrontendGameState.Settings.UiScale));
var index = (int)((cursorPosition.X - (x + (4 * Context.Get().FrontendGameState.Settings.UiScale))) / (21 * Context.Get().FrontendGameState.Settings.UiScale));
if (e.button.button == SDL_BUTTON_LEFT)
{
player.Inventory.SwapWithCursor(index, hotbar);
@ -82,15 +92,15 @@ public class PlayerInventoryRenderer : Inventory
}
}
}
if (cursorPosition.X >= this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale)
&& cursorPosition.X <= this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 9 * Context.Get().FrontendGameState.Settings.UiScale)
&& cursorPosition.Y >= this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * Context.Get().FrontendGameState.Settings.UiScale)
&& cursorPosition.Y <= this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 6 * Context.Get().FrontendGameState.Settings.UiScale)
if (cursorPosition.X >= x + (4 * Context.Get().FrontendGameState.Settings.UiScale)
&& cursorPosition.X <= x + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 9 * Context.Get().FrontendGameState.Settings.UiScale)
&& cursorPosition.Y >= y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * Context.Get().FrontendGameState.Settings.UiScale)
&& cursorPosition.Y <= y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 6 * Context.Get().FrontendGameState.Settings.UiScale)
)
{
var inventory = player.Inventory.Inventory;
var index = (int)((cursorPosition.X - (this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale))) / (21 * Context.Get().FrontendGameState.Settings.UiScale))
+ ((int)((cursorPosition.Y - (this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * Context.Get().FrontendGameState.Settings.UiScale))) / (21 * Context.Get().FrontendGameState.Settings.UiScale)) * 9);
var index = (int)((cursorPosition.X - (x + (4 * Context.Get().FrontendGameState.Settings.UiScale))) / (21 * Context.Get().FrontendGameState.Settings.UiScale))
+ ((int)((cursorPosition.Y - (y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * Context.Get().FrontendGameState.Settings.UiScale))) / (21 * Context.Get().FrontendGameState.Settings.UiScale)) * 9);
if (e.button.button == SDL_BUTTON_LEFT)
{
player.Inventory.SwapWithCursor(index, inventory);
@ -107,7 +117,7 @@ public class PlayerInventoryRenderer : Inventory
}
}
}
if (IsCursorOnSlot(((4 + (9 * 21)) * Context.Get().FrontendGameState.Settings.UiScale) + this.x, (((5 * 21) + 4) * Context.Get().FrontendGameState.Settings.UiScale) + this.y))
if (IsCursorOnSlot(((4 + (9 * 21)) * Context.Get().FrontendGameState.Settings.UiScale) + x, (((5 * 21) + 4) * Context.Get().FrontendGameState.Settings.UiScale) + y))
{
if (e.button.button == SDL_BUTTON_LEFT)
{

View File

@ -2,12 +2,15 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Mine2d.game.core;
using Mine2d.game.core.tiles;
namespace Mine2d.game.frontend.inventory;
public class WorkbenchInventory : Inventory
{
private IntPtr texture = IntPtr.Zero;
private int x, y;
public override void Render()
{
var ctx = Context.Get();
@ -16,10 +19,57 @@ public class WorkbenchInventory : Inventory
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);
width *= uiScale;
height *= uiScale;
var extraSlotsWidth = InventoryConstants.ExtraSlotsWidth * uiScale;
var (windowWidth, windowHeight) = (ctx.FrontendGameState.WindowWidth, ctx.FrontendGameState.WindowHeight);
this.x = (windowWidth - (width - extraSlotsWidth)) / 2;
this.y = (windowHeight - height) / 2;
ctx.Renderer.DrawTexture(this.texture, this.x, this.y, width, height);
PlayerInventoryRenderer.Render(this.x, this.y + (50 * uiScale));
var rX = this.x + (4 * uiScale);
var rY = this.y + (4 * uiScale);
foreach ( var r in Workbench.Recipes) {
var desc = "Requires:\n";
foreach (var i in r.Ingredients) {
desc += $"{i.Count}x {i.GetName()}\n";
}
ItemRenderer.RenderItemStack(r.Result, new Vector2(rX, rY), true, desc);
rX += 21 * uiScale;
}
}
public override void OnClick(SDL_Event e)
{
var ctx = Context.Get();
var uiScale = ctx.FrontendGameState.Settings.UiScale;
PlayerInventoryRenderer.OnClick(this.x, this.y + (50 * uiScale), e);
var cursorPosition = Context.Get().FrontendGameState.CursorPosition;
var player = PlayerEntity.GetSelf();
var rX = this.x + (4 * uiScale);
var rY = this.y + (4 * uiScale);
foreach ( var r in Workbench.Recipes) {
if(cursorPosition.X >= rX && cursorPosition.X <= rX + (21 * uiScale) && cursorPosition.Y >= rY && cursorPosition.Y <= rY + (21 * uiScale)) {
Console.WriteLine("Clicked on recipe" + r.Result.GetName());
var canCraft = true;
foreach (var i in r.Ingredients) {
if (!player.Inventory.HasItemStack(i)) {
canCraft = false;
break;
}
}
if (canCraft) {
foreach (var i in r.Ingredients) {
player.Inventory.RemoveItemStack(i);
}
player.Inventory.PickupItemStack(r.Result);
}
}
rX += 21 * uiScale;
}
}
}

View File

@ -13,7 +13,6 @@ public class PlayerRenderer : IRenderer
this.playerTexture = Context.Get().TextureFactory.LoadTexture("character.character");
}
var ctx = Context.Get();
var camera = ctx.FrontendGameState.Camera;
var scale = ctx.FrontendGameState.Settings.GameScale;
var (width, height) = ctx.Window.GetSize();
foreach (var player in ctx.GameState.Players)
@ -26,13 +25,6 @@ public class PlayerRenderer : IRenderer
{
ctx.Renderer.SetColor(255, 0, 0);
}
// ctx.Renderer.DrawRect(
// (player.Position.X - (int)camera.Position.X) * scale,
// (player.Position.Y - (int)camera.Position.Y) * scale - 28 * scale,
// 14 * scale,
// 28 * scale
// );
var y = player.PlayerMovementState.MovingRight ? 32 * 3 : 32 * 1;
var moving = player.PlayerMovementState.CurrentVelocity.X != 0;
var dt = (DateTime.Now - DateTime.MinValue).TotalMilliseconds;

View File

@ -16,7 +16,7 @@ public class FrontendGameState
public Settings Settings { get; set; } = new Settings();
public string PlayerName { get; set; } = "Player";
public int HotbarIndex { get; set; }
public string Tooltip { get; set; } = "Test";
public string Tooltip { get; set; }
public InventoryKind OpenInventory { get; set; } = InventoryKind.None;
public InputState InputState { get; set; } = new();
public DebugState DebugState { get; set; } = new();

View File

@ -37,6 +37,45 @@ public class PlayerInventory
return true;
}
public bool HasItemStack(ItemStack itemStack)
{
return HasItemStack(itemStack, this.Hotbar) || HasItemStack(itemStack, this.Inventory);
}
public static bool HasItemStack(ItemStack itemStack, ItemStack[] inventory)
{
var slot = InventoryUtils.GetFirstMatchingSlot(inventory, itemStack.Id);
if (slot == -1)
{
return false;
}
if(inventory[slot] == null) return false;
return inventory[slot].Count >= itemStack.Count;
}
public bool RemoveItemStack(ItemStack itemStack)
{
return RemoveItemStack(itemStack, this.Hotbar) || RemoveItemStack(itemStack, this.Inventory);
}
public static bool RemoveItemStack(ItemStack itemStack, ItemStack[] inventory)
{
var slot = InventoryUtils.GetFirstMatchingSlot(inventory, itemStack.Id);
if (slot == -1)
{
return false;
}
if (inventory[slot].Count == itemStack.Count)
{
inventory[slot] = null;
}
else
{
inventory[slot].Count -= itemStack.Count;
}
return true;
}
public void SwapWithCursor(int slot, ItemStack[] inventory)
{
if (this.Cursor != null && inventory[slot] != null && this.Cursor.Id == inventory[slot].Id && this.Cursor.IsStackable())