diff options
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/Game.cs | 2 | ||||
| -rw-r--r-- | scripts/entities/actions/PickUpAction.cs | 34 | ||||
| -rw-r--r-- | scripts/entities/actions/PickUpAction.cs.uid | 1 | ||||
| -rw-r--r-- | scripts/entities/actors/Inventory.cs | 35 | ||||
| -rw-r--r-- | scripts/entities/actors/Inventory.cs.uid | 1 | ||||
| -rw-r--r-- | scripts/entities/actors/Player.cs | 11 | ||||
| -rw-r--r-- | scripts/entities/actors/PlayerDefinition.cs | 8 | ||||
| -rw-r--r-- | scripts/entities/actors/PlayerDefinition.cs.uid | 1 | ||||
| -rw-r--r-- | scripts/input/InputHandler.cs | 4 | ||||
| -rw-r--r-- | scripts/input/MainGameInputHandler.cs | 4 | ||||
| -rw-r--r-- | scripts/input/PickupInputHandler.cs | 41 | ||||
| -rw-r--r-- | scripts/input/PickupInputHandler.cs.uid | 1 | ||||
| -rw-r--r-- | scripts/map/Map.cs | 6 | ||||
| -rw-r--r-- | scripts/map/MapData.cs | 45 |
14 files changed, 189 insertions, 5 deletions
diff --git a/scripts/Game.cs b/scripts/Game.cs index f25776e..b8c6a8b 100644 --- a/scripts/Game.cs +++ b/scripts/Game.cs @@ -9,7 +9,7 @@ public partial class Game : Node { /// <summary> /// Definição de um jogador. /// </summary> - private static readonly ActorDefinition playerDefinition = GD.Load<ActorDefinition>("res://assets/definitions/actor/Player.tres"); + private static readonly PlayerDefinition playerDefinition = GD.Load<PlayerDefinition>("res://assets/definitions/actor/Player.tres"); /// <summary> /// O jogo possui o mapa. /// </summary> diff --git a/scripts/entities/actions/PickUpAction.cs b/scripts/entities/actions/PickUpAction.cs new file mode 100644 index 0000000..b772bb7 --- /dev/null +++ b/scripts/entities/actions/PickUpAction.cs @@ -0,0 +1,34 @@ +using Godot; + +public partial class PickupAction : DirectionalAction +{ + protected Player player; + + public PickupAction(Player player, Vector2I offset) : base(player, offset) + { + this.player = player; + // Pegar itens requer um tempo menor. + cost = 2; + } + + public override bool Perform() + { + ConsumableItem item = Map_Data.GetFirstItemAtPosition(Destination); + + if (item == null) { + MessageLogData.Instance.AddMessage("Não tem item aqui."); + return false; + } + + if (player.inventory.Items.Count >= player.inventory.Capacity) { + MessageLogData.Instance.AddMessage("Seu inventário está cheio"); + return false; + } + + Map_Data.RemoveEntity(item); + player.inventory.Add(item); + + player.Energy -= cost; + return true; + } +}
\ No newline at end of file diff --git a/scripts/entities/actions/PickUpAction.cs.uid b/scripts/entities/actions/PickUpAction.cs.uid new file mode 100644 index 0000000..7ca9c72 --- /dev/null +++ b/scripts/entities/actions/PickUpAction.cs.uid @@ -0,0 +1 @@ +uid://dydpoqnwxwbq6 diff --git a/scripts/entities/actors/Inventory.cs b/scripts/entities/actors/Inventory.cs new file mode 100644 index 0000000..ea9569f --- /dev/null +++ b/scripts/entities/actors/Inventory.cs @@ -0,0 +1,35 @@ +using Godot; + +public partial class Inventory : Node { + private Player player; + + public Godot.Collections.Array<ConsumableItem> Items { get; private set; } = []; + + public int Capacity { get; private set; } + + public Inventory(int capacity) { + Capacity = capacity; + } + + public override void _Ready() { + base._Ready(); + player = GetParent<Player>(); + } + + public void Drop(ConsumableItem item) { + Items.Remove(item); + MapData data = player.Map_Data; + data.InsertEntity(item); + data.EmitSignal(MapData.SignalName.EntityPlaced, item); + item.Map_Data = data; + item.GridPosition = player.GridPosition; + + MessageLogData.Instance.AddMessage($"Você descarta {item.DisplayName}."); + } + + public void Add(ConsumableItem item) { + if (Items.Count >= Capacity) return; + + Items.Add(item); + } +}
\ No newline at end of file diff --git a/scripts/entities/actors/Inventory.cs.uid b/scripts/entities/actors/Inventory.cs.uid new file mode 100644 index 0000000..05c2beb --- /dev/null +++ b/scripts/entities/actors/Inventory.cs.uid @@ -0,0 +1 @@ +uid://isaqxdpou22h diff --git a/scripts/entities/actors/Player.cs b/scripts/entities/actors/Player.cs index 324e67a..d1c4c7f 100644 --- a/scripts/entities/actors/Player.cs +++ b/scripts/entities/actors/Player.cs @@ -7,7 +7,16 @@ using System; [GlobalClass] public partial class Player : Actor { - public Player(Vector2I initialPosition, MapData map, ActorDefinition definition) : base(initialPosition, map, definition) + public Inventory inventory; + + public Player(Vector2I initialPosition, MapData map, PlayerDefinition definition) : base(initialPosition, map, definition) { + SetDefinition(definition); + } + + public void SetDefinition(PlayerDefinition definition) { + inventory = new(definition.InventoryCapacity); + + AddChild(inventory); } } diff --git a/scripts/entities/actors/PlayerDefinition.cs b/scripts/entities/actors/PlayerDefinition.cs new file mode 100644 index 0000000..aca07e1 --- /dev/null +++ b/scripts/entities/actors/PlayerDefinition.cs @@ -0,0 +1,8 @@ +using Godot; + +[GlobalClass] +public partial class PlayerDefinition : ActorDefinition { + [ExportCategory("Player Mechanics")] + [Export] + public int InventoryCapacity = 0; +}
\ No newline at end of file diff --git a/scripts/entities/actors/PlayerDefinition.cs.uid b/scripts/entities/actors/PlayerDefinition.cs.uid new file mode 100644 index 0000000..9d01ab9 --- /dev/null +++ b/scripts/entities/actors/PlayerDefinition.cs.uid @@ -0,0 +1 @@ +uid://bd78nfh1tsjq6 diff --git a/scripts/input/InputHandler.cs b/scripts/input/InputHandler.cs index 4f5b4e1..7ee2be1 100644 --- a/scripts/input/InputHandler.cs +++ b/scripts/input/InputHandler.cs @@ -4,7 +4,8 @@ public enum InputHandlers { MainGame, GameOver, - Inspect + Inspect, + Pickup } /// <summary> @@ -27,6 +28,7 @@ public partial class InputHandler : Node // Controles para quando o jogador está morto. inputHandlers.Add(InputHandlers.GameOver, GetNode<GameOverInputHandler>("GameOverInputHandler")); inputHandlers.Add(InputHandlers.Inspect, GetNode<InspectInputHandler>("InspectInputHandler")); + inputHandlers.Add(InputHandlers.Pickup, GetNode<PickupInputHandler>("PickupInputHandler")); SetInputHandler(startingInputHandler); } diff --git a/scripts/input/MainGameInputHandler.cs b/scripts/input/MainGameInputHandler.cs index c7bf26a..6fb6aac 100644 --- a/scripts/input/MainGameInputHandler.cs +++ b/scripts/input/MainGameInputHandler.cs @@ -25,6 +25,10 @@ public partial class MainGameInputHandler : BaseInputHandler { } } + if (Input.IsActionJustPressed("pick-item")) { + GetParent<InputHandler>().SetInputHandler(InputHandlers.Pickup); + } + if (Input.IsActionJustPressed("inspect")) { GetParent<InputHandler>().SetInputHandler(InputHandlers.Inspect); } diff --git a/scripts/input/PickupInputHandler.cs b/scripts/input/PickupInputHandler.cs new file mode 100644 index 0000000..8f4f9b2 --- /dev/null +++ b/scripts/input/PickupInputHandler.cs @@ -0,0 +1,41 @@ +using Godot; + +/// <summary> +/// Esquema de controles para pegar um item. +/// </summary> +public partial class PickupInputHandler : BaseInputHandler { + private readonly Godot.Collections.Dictionary<string, Vector2I> directions = new() + { + {"walk-up", Vector2I.Up}, + {"walk-down", Vector2I.Down}, + {"walk-left", Vector2I.Left}, + {"walk-right", Vector2I.Right}, + {"walk-up-right", Vector2I.Up + Vector2I.Right}, + {"walk-up-left", Vector2I.Up + Vector2I.Left}, + {"walk-down-right", Vector2I.Down + Vector2I.Right}, + {"walk-down-left", Vector2I.Down + Vector2I.Left}, + {"skip-turn", Vector2I.Zero} + }; + public override Action GetAction(Player player) { + Action action = null; + + if (player.IsAlive) { + foreach (var direction in directions) { + if (Input.IsActionJustPressed(direction.Key)) { + action = new PickupAction(player, direction.Value); + Quit(); + } + } + + if (Input.IsActionJustPressed("quit")) { + Quit(); + } + } + + return action; + } + + private void Quit() { + GetParent<InputHandler>().SetInputHandler(InputHandlers.MainGame); + } +} diff --git a/scripts/input/PickupInputHandler.cs.uid b/scripts/input/PickupInputHandler.cs.uid new file mode 100644 index 0000000..139dd25 --- /dev/null +++ b/scripts/input/PickupInputHandler.cs.uid @@ -0,0 +1 @@ +uid://dspqgdxg5jji0 diff --git a/scripts/map/Map.cs b/scripts/map/Map.cs index f4f0783..148bda6 100644 --- a/scripts/map/Map.cs +++ b/scripts/map/Map.cs @@ -62,6 +62,8 @@ public partial class Map : Node2D { Map_Data = generator.GenerateDungeon(player); + Map_Data.EntityPlaced += OnEntityPlaced; + PlaceTiles(); PlaceEntities(); } @@ -77,4 +79,8 @@ public partial class Map : Node2D entity.Visible = Map_Data.GetTile(entity.GridPosition).IsInView; } } + + private void OnEntityPlaced(Entity entity) { + entitiesNode.AddChild(entity); + } } diff --git a/scripts/map/MapData.cs b/scripts/map/MapData.cs index 08a144d..f3e193e 100644 --- a/scripts/map/MapData.cs +++ b/scripts/map/MapData.cs @@ -10,9 +10,12 @@ public partial class MapData : RefCounted public static readonly TileDefinition wallDefinition = GD.Load<TileDefinition>("res://assets/definitions/tiles/wall.tres"); public static readonly TileDefinition floorDefinition = GD.Load<TileDefinition>("res://assets/definitions/tiles/floor.tres"); + [Signal] + public delegate void EntityPlacedEventHandler(Entity entity); + /// <summary> - /// Largura do mapa. - /// </summary> + /// Largura do mapa. + /// </summary> public int Width { get; private set; } /// <summary> /// Altura do mapa. @@ -33,6 +36,18 @@ public partial class MapData : RefCounted /// </summary> public Godot.Collections.Array<Entity> Entities { get; private set; } = []; + public Godot.Collections.Array<ConsumableItem> Items { + get { + Godot.Collections.Array<ConsumableItem> list = []; + foreach (Entity entity in Entities) { + if (entity is ConsumableItem item) { + list.Add(item); + } + } + return list; + } + } + private AStarGrid2D pathfinder; /// <summary> /// Objeto do Godot que utiliza do algoritmo A* para calcular @@ -196,6 +211,32 @@ public partial class MapData : RefCounted } return null; } + + /// <summary> + /// Obtém o primeiro item na posição especificada. + /// </summary> + /// <param name="pos">Posição</param> + /// <returns>O primeiro item na posição, nulo se não houver.</returns> + public ConsumableItem GetFirstItemAtPosition(Vector2I pos) { + foreach (ConsumableItem item in Items) { + if (item.GridPosition == pos) { + return item; + } + } + + return null; + } + + /// <summary> + /// Remove uma entidade do mapa sem dar free. + /// </summary> + /// <param name="entity">A entidade para remover</param> + public void RemoveEntity(Entity entity) { + // Eu removo a entidade do nó de entidades. + entity.GetParent().RemoveChild(entity); + // Eu removo a entidade da lista de entidades do mapa. + Entities.Remove(entity); + } /// <summary> /// Obtém todas as entidades na posição especificada. |
