diff options
| author | Matheus <matheus.guedes.mg.m@gmail.com> | 2025-09-08 22:10:45 -0300 |
|---|---|---|
| committer | Matheus <matheus.guedes.mg.m@gmail.com> | 2025-09-08 22:10:45 -0300 |
| commit | f1b51bed52ffbd90b5b7cc8dcfc6f0484bbbeb3c (patch) | |
| tree | d607142daee4948765a97008bdef21fa6efa2d2b /scripts | |
| parent | 4b2afd3e2144e42bfa7f11a870584b9255052cf7 (diff) | |
inventário acessivel
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/GUI/InventoryMenu.cs | 52 | ||||
| -rw-r--r-- | scripts/GUI/InventoryMenu.cs.uid | 1 | ||||
| -rw-r--r-- | scripts/GUI/ItemMenuEntry.cs | 41 | ||||
| -rw-r--r-- | scripts/GUI/ItemMenuEntry.cs.uid | 1 | ||||
| -rw-r--r-- | scripts/entities/actions/DropAction.cs | 13 | ||||
| -rw-r--r-- | scripts/entities/actions/DropAction.cs.uid | 1 | ||||
| -rw-r--r-- | scripts/entities/actions/ItemAction.cs | 6 | ||||
| -rw-r--r-- | scripts/entities/actors/Actor.cs | 2 | ||||
| -rw-r--r-- | scripts/entities/actors/Inventory.cs | 4 | ||||
| -rw-r--r-- | scripts/entities/actors/Player.cs | 2 | ||||
| -rw-r--r-- | scripts/entities/items/ConsumableItem.cs | 8 | ||||
| -rw-r--r-- | scripts/entities/items/HealingConsumable.cs | 3 | ||||
| -rw-r--r-- | scripts/input/InputHandler.cs | 4 | ||||
| -rw-r--r-- | scripts/input/InventoryInputHandler.cs | 65 | ||||
| -rw-r--r-- | scripts/input/InventoryInputHandler.cs.uid | 1 | ||||
| -rw-r--r-- | scripts/input/MainGameInputHandler.cs | 6 |
16 files changed, 203 insertions, 7 deletions
diff --git a/scripts/GUI/InventoryMenu.cs b/scripts/GUI/InventoryMenu.cs new file mode 100644 index 0000000..bfb1795 --- /dev/null +++ b/scripts/GUI/InventoryMenu.cs @@ -0,0 +1,52 @@ +using Godot; +using System; + +public partial class InventoryMenu : CanvasLayer +{ + private static readonly PackedScene itemMenuEntryScene = GD.Load<PackedScene>("res://scenes/GUI/item_menu_entry.tscn"); + [Signal] + public delegate void ItemSelectedEventHandler(ConsumableItem item); + [Signal] + public delegate void ItemDropEventHandler(ConsumableItem item); + + private VBoxContainer itemsNode; + + public override void _Ready() { + base._Ready(); + + itemsNode = GetNode<VBoxContainer>("CenterContainer/PanelContainer/VBoxContainer/Items"); + Hide(); + } + + public void OnActivate(ConsumableItem item) { + EmitSignal(SignalName.ItemSelected, item); + } + + public void OnDrop(ConsumableItem item) { + EmitSignal(SignalName.ItemDrop, item); + } + + private void RegisterItem(int index, ConsumableItem item) { + char? shortcut = null; + + // Só terá atalho para as letras do alfabeto. + if (index < 26) { + shortcut = (char)('a' + index); + } + + ItemMenuEntry itemEntry = itemMenuEntryScene.Instantiate<ItemMenuEntry>(); + + itemsNode.AddChild(itemEntry); + itemEntry.Initialize(item, shortcut); + itemEntry.Activate += OnActivate; + itemEntry.Drop += OnDrop; + } + + public void Initialize(Inventory inventory) { + for (int i = 0; i < inventory.Items.Count; i++) { + RegisterItem(i, inventory.Items[i]); + } + + Show(); + } +} diff --git a/scripts/GUI/InventoryMenu.cs.uid b/scripts/GUI/InventoryMenu.cs.uid new file mode 100644 index 0000000..594b644 --- /dev/null +++ b/scripts/GUI/InventoryMenu.cs.uid @@ -0,0 +1 @@ +uid://dukpdkyy365y4 diff --git a/scripts/GUI/ItemMenuEntry.cs b/scripts/GUI/ItemMenuEntry.cs new file mode 100644 index 0000000..449c97b --- /dev/null +++ b/scripts/GUI/ItemMenuEntry.cs @@ -0,0 +1,41 @@ +using Godot; + +public partial class ItemMenuEntry : HBoxContainer +{ + private TextureRect icon; + private Label shortcutLabel; + private Label nameLabel; + private Button activateBtn; + private Button dropBtn; + + [Signal] + public delegate void ActivateEventHandler(ConsumableItem Item); + + [Signal] + public delegate void DropEventHandler(ConsumableItem item); + + private ConsumableItem item; + + public void Initialize(ConsumableItem item, char? shortcut) { + this.item = item; + nameLabel.Text = item.DisplayName; + if (shortcut != null) { + shortcutLabel.Text = $"{shortcut}"; + } else { + shortcutLabel.Text = ""; + } + icon.Texture = item.Texture; + } + + public override void _Ready() { + base._Ready(); + icon = GetNode<TextureRect>("Icon"); + shortcutLabel = GetNode<Label>("Shortcut"); + nameLabel = GetNode<Label>("ItemName"); + activateBtn = GetNode<Button>("ActivateBtn"); + dropBtn = GetNode<Button>("DropButton"); + + activateBtn.Pressed += () => EmitSignal(SignalName.Activate, item); + dropBtn.Pressed += () => EmitSignal(SignalName.Drop, item); + } +} diff --git a/scripts/GUI/ItemMenuEntry.cs.uid b/scripts/GUI/ItemMenuEntry.cs.uid new file mode 100644 index 0000000..9ebeb06 --- /dev/null +++ b/scripts/GUI/ItemMenuEntry.cs.uid @@ -0,0 +1 @@ +uid://bdejwf8qmlii0 diff --git a/scripts/entities/actions/DropAction.cs b/scripts/entities/actions/DropAction.cs new file mode 100644 index 0000000..e5bd929 --- /dev/null +++ b/scripts/entities/actions/DropAction.cs @@ -0,0 +1,13 @@ +using Godot; + +public partial class DropAction : ItemAction +{ + public DropAction(Player player, ConsumableItem item) : base(player, item) + { + } + + public override bool Perform() { + player.inventory.Drop(item); + return true; + } +}
\ No newline at end of file diff --git a/scripts/entities/actions/DropAction.cs.uid b/scripts/entities/actions/DropAction.cs.uid new file mode 100644 index 0000000..98ed82f --- /dev/null +++ b/scripts/entities/actions/DropAction.cs.uid @@ -0,0 +1 @@ +uid://by48a3a3gbvfa diff --git a/scripts/entities/actions/ItemAction.cs b/scripts/entities/actions/ItemAction.cs index 54ff207..d5247df 100644 --- a/scripts/entities/actions/ItemAction.cs +++ b/scripts/entities/actions/ItemAction.cs @@ -2,10 +2,12 @@ using Godot; public partial class ItemAction : Action { - private ConsumableItem item; - public ItemAction(Actor actor, ConsumableItem item) : base(actor) + protected ConsumableItem item; + protected Player player; + public ItemAction(Player player, ConsumableItem item) : base(player) { this.item = item; + this.player = player; } public override bool Perform() diff --git a/scripts/entities/actors/Actor.cs b/scripts/entities/actors/Actor.cs index 1717cfa..1cb1f37 100644 --- a/scripts/entities/actors/Actor.cs +++ b/scripts/entities/actors/Actor.cs @@ -139,7 +139,7 @@ public partial class Actor : Entity public int Heal(int amount) { int neoHp = Hp + amount; - if (amount > MaxHp) neoHp = MaxHp; + if (neoHp > MaxHp) neoHp = MaxHp; int recovered = neoHp - Hp; Hp = neoHp; diff --git a/scripts/entities/actors/Inventory.cs b/scripts/entities/actors/Inventory.cs index ea9569f..f1cff2a 100644 --- a/scripts/entities/actors/Inventory.cs +++ b/scripts/entities/actors/Inventory.cs @@ -32,4 +32,8 @@ public partial class Inventory : Node { Items.Add(item); } + + public void RemoveItem(ConsumableItem item) { + Items.Remove(item); + } }
\ No newline at end of file diff --git a/scripts/entities/actors/Player.cs b/scripts/entities/actors/Player.cs index d1c4c7f..71812e8 100644 --- a/scripts/entities/actors/Player.cs +++ b/scripts/entities/actors/Player.cs @@ -7,10 +7,12 @@ using System; [GlobalClass] public partial class Player : Actor { + private PlayerDefinition definition; public Inventory inventory; public Player(Vector2I initialPosition, MapData map, PlayerDefinition definition) : base(initialPosition, map, definition) { + this.definition = definition; SetDefinition(definition); } diff --git a/scripts/entities/items/ConsumableItem.cs b/scripts/entities/items/ConsumableItem.cs index 8c862da..82fab49 100644 --- a/scripts/entities/items/ConsumableItem.cs +++ b/scripts/entities/items/ConsumableItem.cs @@ -15,7 +15,7 @@ public abstract partial class ConsumableItem : Entity /// </summary> /// <param name="consumer"></param> /// <returns></returns> - public Action GetAction(Actor consumer) + public Action GetAction(Player consumer) { return new ItemAction(consumer, this); } @@ -28,4 +28,10 @@ public abstract partial class ConsumableItem : Entity /// <param name="action">Ação gerada pelo item.</param> /// <returns>Se a ação foi realizada ou não.</returns> public abstract bool Activate(ItemAction action); + + public void ConsumedBy(Player consumer) { + Inventory inventory = consumer.inventory; + inventory.RemoveItem(this); + QueueFree(); + } }
\ No newline at end of file diff --git a/scripts/entities/items/HealingConsumable.cs b/scripts/entities/items/HealingConsumable.cs index 3d8df51..2104693 100644 --- a/scripts/entities/items/HealingConsumable.cs +++ b/scripts/entities/items/HealingConsumable.cs @@ -12,7 +12,7 @@ public partial class HealingConsumable : ConsumableItem public override bool Activate(ItemAction action) { - Actor consumer = action.ThisActor; + Player consumer = (Player) action.ThisActor; int intendedAmount = (int)(HealingPercentage / 100 * consumer.MaxHp); int recovered = consumer.Heal(intendedAmount); @@ -22,6 +22,7 @@ public partial class HealingConsumable : ConsumableItem return false; } MessageLogData.Instance.AddMessage($"Você consome {DisplayName} e recupera {recovered} de HP"); + ConsumedBy(consumer); return true; } }
\ No newline at end of file diff --git a/scripts/input/InputHandler.cs b/scripts/input/InputHandler.cs index 7ee2be1..67f4abf 100644 --- a/scripts/input/InputHandler.cs +++ b/scripts/input/InputHandler.cs @@ -5,7 +5,8 @@ public enum InputHandlers MainGame, GameOver, Inspect, - Pickup + Pickup, + Inventory } /// <summary> @@ -29,6 +30,7 @@ public partial class InputHandler : Node inputHandlers.Add(InputHandlers.GameOver, GetNode<GameOverInputHandler>("GameOverInputHandler")); inputHandlers.Add(InputHandlers.Inspect, GetNode<InspectInputHandler>("InspectInputHandler")); inputHandlers.Add(InputHandlers.Pickup, GetNode<PickupInputHandler>("PickupInputHandler")); + inputHandlers.Add(InputHandlers.Inventory, GetNode<InventoryInputHandler>("InventoryInputHandler")); SetInputHandler(startingInputHandler); } diff --git a/scripts/input/InventoryInputHandler.cs b/scripts/input/InventoryInputHandler.cs new file mode 100644 index 0000000..98f8576 --- /dev/null +++ b/scripts/input/InventoryInputHandler.cs @@ -0,0 +1,65 @@ +using Godot; + +public partial class InventoryInputHandler : BaseInputHandler +{ + private static readonly PackedScene inventoryScene = GD.Load<PackedScene>("res://scenes/GUI/invetory_menu.tscn"); + + private InventoryMenu inventoryMenu; + + ConsumableItem activationItem = null; + ConsumableItem dropItem = null; + + [Export] + private Map map; + + public override void Enter() { + inventoryMenu = inventoryScene.Instantiate<InventoryMenu>(); + map.Map_Data.Player.AddChild(inventoryMenu); + inventoryMenu.Initialize(map.Map_Data.Player.inventory); + inventoryMenu.ItemSelected += OnItemActivate; + inventoryMenu.ItemDrop += OnItemDrop; + } + + public override void Exit() { + activationItem = null; + dropItem = null; + inventoryMenu.QueueFree(); + } + + public override Action GetAction(Player player) + { + Action action = null; + + if (activationItem != null) { + action = new ItemAction(player, activationItem); + Close(); + } + + if (dropItem != null) { + action = new DropAction(player, dropItem); + Close(); + } + + if (Input.IsActionJustPressed("quit")) { + Close(); + } + + return action; + } + + private void Close() { + GetParent<InputHandler>().SetInputHandler(InputHandlers.MainGame); + } + + private void ActivateItem() { + + } + + private void OnItemActivate(ConsumableItem item) { + activationItem = item; + } + + private void OnItemDrop(ConsumableItem item) { + dropItem = item; + } +}
\ No newline at end of file diff --git a/scripts/input/InventoryInputHandler.cs.uid b/scripts/input/InventoryInputHandler.cs.uid new file mode 100644 index 0000000..b5d0ed9 --- /dev/null +++ b/scripts/input/InventoryInputHandler.cs.uid @@ -0,0 +1 @@ +uid://bjcjktvyrdh10 diff --git a/scripts/input/MainGameInputHandler.cs b/scripts/input/MainGameInputHandler.cs index 6fb6aac..6bda004 100644 --- a/scripts/input/MainGameInputHandler.cs +++ b/scripts/input/MainGameInputHandler.cs @@ -4,7 +4,7 @@ using Godot; /// Esquema de controles principal do jogo. /// </summary> public partial class MainGameInputHandler : BaseInputHandler { - private readonly Godot.Collections.Dictionary<string, Vector2I> directions = new() + private static readonly Godot.Collections.Dictionary<string, Vector2I> directions = new() { {"walk-up", Vector2I.Up}, {"walk-down", Vector2I.Down}, @@ -25,6 +25,10 @@ public partial class MainGameInputHandler : BaseInputHandler { } } + if (Input.IsActionJustPressed("open-inventory")) { + GetParent<InputHandler>().SetInputHandler(InputHandlers.Inventory); + } + if (Input.IsActionJustPressed("pick-item")) { GetParent<InputHandler>().SetInputHandler(InputHandlers.Pickup); } |
