Re-introduce prototype code

This commit is contained in:
2024-08-23 00:39:15 -07:00
parent 8b3d1bed8a
commit 2fb0c364ca
62 changed files with 1685 additions and 187 deletions

View File

@@ -0,0 +1,126 @@
using Chickensoft.AutoInject;
using Chickensoft.GodotNodeInterfaces;
using Chickensoft.Introspection;
using GameJamDungeon;
using Godot;
using System.Collections.Generic;
public interface IInventoryMenu : IControl
{
public void PopulateItems(List<InventoryItem> items);
public void ClearItems();
}
[Meta(typeof(IAutoNode))]
public partial class InventoryMenu : Control, IInventoryMenu
{
public override void _Notification(int what) => this.Notify(what);
[Node] public IVBoxContainer ItemList { get; set; } = default!;
[Node] public TextureRect Cursor { get; set; } = default!;
[Dependency] public IGameRepo GameRepo => this.DependOn<IGameRepo>();
private int _currentSelection = 0;
public void PopulateItems(List<InventoryItem> items)
{
foreach (var item in items)
{
var label = new WeaponLabel(item) { Text = item.Name };
ItemList.AddChild(label);
}
if (ItemList.GetChildCount() > 0)
CallDeferred(nameof(InitializeInventoryMenu));
}
public void InitializeInventoryMenu()
{
if (ItemList.GetChildCount() > 0)
{
var currentItem = ItemList.GetChild<Control>(_currentSelection);
SetCursorLocation(currentItem);
}
}
public void ClearItems()
{
foreach (var item in ItemList.GetChildren())
ItemList.RemoveChild(item);
}
public void SetCursorLocation(Control menuItem)
{
var position = menuItem.GlobalPosition;
var size = menuItem.Size;
Cursor.GlobalPosition = new Vector2(position.X, position.Y + size.Y / 2.0f) - Cursor.Size / 2.0f - new Vector2(15, -5);
}
public void SetCursorToPrevious()
{
if (ItemList.GetChildCount() == 0)
return;
if (_currentSelection > 0)
{
_currentSelection -= 1;
var selectedMenuItem = ItemList.GetChild<Control>(_currentSelection);
SetCursorLocation(selectedMenuItem);
}
}
public void SetCursorToNext()
{
if (ItemList.GetChildCount() == 0)
return;
if (_currentSelection < ItemList.GetChildCount() - 1)
{
_currentSelection += 1;
var selectedMenuItem = ItemList.GetChild<Control>(_currentSelection);
SetCursorLocation(selectedMenuItem);
}
}
private void UnequipItem(WeaponLabel item)
{
item.UnequipItem();
}
public override void _Process(double delta)
{
var input = Vector2.Zero;
if (Input.IsActionJustPressed(GameInputs.MoveUp))
SetCursorToPrevious();
if (Input.IsActionJustPressed(GameInputs.MoveDown))
SetCursorToNext();
}
}
public partial class WeaponLabel : Label
{
public WeaponLabel(InventoryItem inventoryItem)
{
InventoryItem = inventoryItem;
LabelSettings = UnequippedItemFont;
}
public InventoryItem InventoryItem { get; set; } = default!;
private static LabelSettings UnequippedItemFont => GD.Load<LabelSettings>("res://src/vfx/Fonts/InventoryLabelSettings.tres");
private static LabelSettings EquippedItemFont => GD.Load<LabelSettings>("res://src/vfx/Fonts/EquippedInventoryLabelSettings.tres");
public void EquipItem()
{
LabelSettings = EquippedItemFont;
}
public void UnequipItem()
{
LabelSettings = UnequippedItemFont;
}
}