From ca782d7310f2df854a8727535ac92337c85ed445 Mon Sep 17 00:00:00 2001 From: Matheus Date: Sun, 17 Aug 2025 19:32:36 -0300 Subject: Gigantesco refactor --- scripts/Actor.cs | 36 ------------------ scripts/Actor.cs.uid | 1 - scripts/Character.cs | 48 ------------------------ scripts/Character.cs.uid | 1 - scripts/DungeonLevel.cs | 21 ++++++----- scripts/DungeonLevel.cs.uid | 2 +- scripts/Enemy.cs | 7 ++-- scripts/Game.cs | 28 ++++++++++++++ scripts/Game.cs.uid | 1 + scripts/InputHandler.cs | 22 +++++++++++ scripts/InputHandler.cs.uid | 1 + scripts/TurnManager.cs | 55 ---------------------------- scripts/TurnManager.cs.uid | 1 - scripts/Utils/Grid.cs | 14 +++++++ scripts/Utils/Grid.cs.uid | 1 + scripts/actors/Actor.cs | 23 ++++++++++++ scripts/actors/Actor.cs.uid | 1 + scripts/actors/Player.cs | 8 ++++ scripts/actors/Player.cs.uid | 1 + scripts/actors/actions/Action.cs | 6 +++ scripts/actors/actions/Action.cs.uid | 1 + scripts/actors/actions/MovementAction.cs | 22 +++++++++++ scripts/actors/actions/MovementAction.cs.uid | 1 + 23 files changed, 145 insertions(+), 157 deletions(-) delete mode 100644 scripts/Actor.cs delete mode 100644 scripts/Actor.cs.uid delete mode 100644 scripts/Character.cs delete mode 100644 scripts/Character.cs.uid create mode 100644 scripts/Game.cs create mode 100644 scripts/Game.cs.uid create mode 100644 scripts/InputHandler.cs create mode 100644 scripts/InputHandler.cs.uid delete mode 100644 scripts/TurnManager.cs delete mode 100644 scripts/TurnManager.cs.uid create mode 100644 scripts/Utils/Grid.cs create mode 100644 scripts/Utils/Grid.cs.uid create mode 100644 scripts/actors/Actor.cs create mode 100644 scripts/actors/Actor.cs.uid create mode 100644 scripts/actors/Player.cs create mode 100644 scripts/actors/Player.cs.uid create mode 100644 scripts/actors/actions/Action.cs create mode 100644 scripts/actors/actions/Action.cs.uid create mode 100644 scripts/actors/actions/MovementAction.cs create mode 100644 scripts/actors/actions/MovementAction.cs.uid (limited to 'scripts') diff --git a/scripts/Actor.cs b/scripts/Actor.cs deleted file mode 100644 index d196940..0000000 --- a/scripts/Actor.cs +++ /dev/null @@ -1,36 +0,0 @@ -using Godot; - -public abstract partial class Actor : Node2D { - static int baseWalkCost = 10; - [Export] - public TileMapLayer Map { get; set; } - [Signal] - public delegate void actionPerformedEventHandler(); - - [Export] - public int Energy { get; set; } = 0; - [Export] - public int Speed { get; protected set; } = 10; - - protected void Walk(Vector2I offset) { - Vector2I toMovePos = Map.LocalToMap(Position); - toMovePos += offset; - - TileData tile = Map.GetCellTileData(toMovePos); - - if (tile != null) { - if ((bool) tile.GetCustomData("isWalkable")) { - Position = Map.MapToLocal(toMovePos); - } - } - - Energy -= baseWalkCost; - EndAction(); - } - - protected virtual void EndAction() { - EmitSignal(SignalName.actionPerformed); - } - - public abstract void PerformAction(); -} \ No newline at end of file diff --git a/scripts/Actor.cs.uid b/scripts/Actor.cs.uid deleted file mode 100644 index cf29b40..0000000 --- a/scripts/Actor.cs.uid +++ /dev/null @@ -1 +0,0 @@ -uid://c0cm4woy8lawl diff --git a/scripts/Character.cs b/scripts/Character.cs deleted file mode 100644 index 1db6ee1..0000000 --- a/scripts/Character.cs +++ /dev/null @@ -1,48 +0,0 @@ -using Godot; -using System; - -public partial class Character : Actor { - private bool canAct = false; - - public override void _Input(InputEvent @event) - { - base._Input(@event); - - if (!@event.IsPressed()) return; - - if (canAct) { - - - if (@event.IsActionPressed("walk-up")) { - Walk(Vector2I.Up); - } - if (@event.IsActionPressed("walk-down")) { - Walk(Vector2I.Down); - } - if (@event.IsActionPressed("walk-left")) { - Walk(Vector2I.Left); - } - if (@event.IsActionPressed("walk-right")) { - Walk(Vector2I.Right); - } - - if (@event.IsActionPressed("skip-turn")) { - SkipTurn(); - } - } - } - - private void SkipTurn() { - Energy = 0; - EndAction(); - } - - protected override void EndAction() { - canAct = false; - base.EndAction(); - } - - public override void PerformAction() { - canAct = true; - } -} diff --git a/scripts/Character.cs.uid b/scripts/Character.cs.uid deleted file mode 100644 index 8229b7f..0000000 --- a/scripts/Character.cs.uid +++ /dev/null @@ -1 +0,0 @@ -uid://c840l08453pu2 diff --git a/scripts/DungeonLevel.cs b/scripts/DungeonLevel.cs index c3536ac..a303254 100644 --- a/scripts/DungeonLevel.cs +++ b/scripts/DungeonLevel.cs @@ -1,18 +1,19 @@ using Godot; using System; -public partial class DungeonLevel : Node2D { - TurnManager turnManager; +public partial class DungeonLevel : Node2D +{ + public Player player; + public TileMapLayer buildingLayer; - public override void _Ready() { - base._Ready(); - - turnManager = GetNode("TurnManager"); + private Node2D actors; - turnManager.Tick(); - } + public override void _Ready() + { + base._Ready(); - public void OnTurnEnd() { - turnManager.Tick(); + buildingLayer = GetNode("Dungeon"); + actors = GetNode("Actors"); + player = actors.GetNode("Player"); } } diff --git a/scripts/DungeonLevel.cs.uid b/scripts/DungeonLevel.cs.uid index 5c3c2a2..a63fd2e 100644 --- a/scripts/DungeonLevel.cs.uid +++ b/scripts/DungeonLevel.cs.uid @@ -1 +1 @@ -uid://dwubb28wt4bhe +uid://cy7qumrpks2kx diff --git a/scripts/Enemy.cs b/scripts/Enemy.cs index 473ba0f..29a85b7 100644 --- a/scripts/Enemy.cs +++ b/scripts/Enemy.cs @@ -1,8 +1,7 @@ using Godot; using System; -public partial class Enemy : Actor { - public override void PerformAction() { - Walk(Vector2I.Right); - } +public partial class Enemy : Actor +{ + } diff --git a/scripts/Game.cs b/scripts/Game.cs new file mode 100644 index 0000000..8de7af6 --- /dev/null +++ b/scripts/Game.cs @@ -0,0 +1,28 @@ +using Godot; +using System; + +public partial class Game : Node { + private Player player; + public TileMapLayer Dungeon { get; private set; } + private DungeonLevel map; + private InputHandler inputHandler; + + public override void _Ready() { + base._Ready(); + + map = GetNode("Map"); + + inputHandler = GetNode("InputHandler"); + Dungeon = map.buildingLayer; + + player = map.player; + } + + public override void _PhysicsProcess(double delta) { + base._PhysicsProcess(delta); + + Action action = inputHandler.GetAction(); + + action?.Perform(this, player); + } +} diff --git a/scripts/Game.cs.uid b/scripts/Game.cs.uid new file mode 100644 index 0000000..5c3c2a2 --- /dev/null +++ b/scripts/Game.cs.uid @@ -0,0 +1 @@ +uid://dwubb28wt4bhe diff --git a/scripts/InputHandler.cs b/scripts/InputHandler.cs new file mode 100644 index 0000000..dae990a --- /dev/null +++ b/scripts/InputHandler.cs @@ -0,0 +1,22 @@ +using Godot; +using System; + +public partial class InputHandler : Node { + public Action GetAction() { + Action action = null; + + if (Input.IsActionJustPressed("walk-up")) { + action = new MovementAction(Vector2I.Up); + } else if (Input.IsActionJustPressed("walk-down")) { + action = new MovementAction(Vector2I.Down); + } else if (Input.IsActionJustPressed("walk-left")) { + action = new MovementAction(Vector2I.Left); + } else if (Input.IsActionJustPressed("walk-right")) { + action = new MovementAction(Vector2I.Right); + } else if (Input.IsActionJustPressed("skip-turn")) { + action = new MovementAction(Vector2I.Zero); + } + + return action; + } +} diff --git a/scripts/InputHandler.cs.uid b/scripts/InputHandler.cs.uid new file mode 100644 index 0000000..302a3b5 --- /dev/null +++ b/scripts/InputHandler.cs.uid @@ -0,0 +1 @@ +uid://ejqmdbc0524i diff --git a/scripts/TurnManager.cs b/scripts/TurnManager.cs deleted file mode 100644 index ea9050e..0000000 --- a/scripts/TurnManager.cs +++ /dev/null @@ -1,55 +0,0 @@ -using Godot; -using System; -using System.Collections.Generic; -using System.Reflection.Metadata.Ecma335; - -public partial class TurnManager : Node { - [Signal] - public delegate void turnBeginEventHandler(); - [Signal] - public delegate void turnEndEventHandler(); - - private Godot.Collections.Array actors = []; - private int index = 0; - - public int TurnCount { get; private set; } = 1; - - public void Tick() { - EmitSignal(SignalName.turnBegin); - - actors.Clear(); - actors = GetTree().GetNodesInGroup("TimeSlave"); - - index = -1; - NextActor(); - } - - private void NextActor() { - index++; - if (index >= actors.Count) { - EndTurn(); - return; - } - Actor currentActor = (Actor) actors[index]; - currentActor.Energy += currentActor.Speed; - ActorPerformAction(); - } - - private void ActorPerformAction() { - Actor currentActor = (Actor) actors[index]; - if (currentActor.Energy > 0) { - currentActor.PerformAction(); - } else { - NextActor(); - } - } - - public void OnActionEnd() { - ActorPerformAction(); - } - - private void EndTurn() { - TurnCount++; - EmitSignal(SignalName.turnEnd); - } -} diff --git a/scripts/TurnManager.cs.uid b/scripts/TurnManager.cs.uid deleted file mode 100644 index a306f3e..0000000 --- a/scripts/TurnManager.cs.uid +++ /dev/null @@ -1 +0,0 @@ -uid://ynjn2pmluvf6 diff --git a/scripts/Utils/Grid.cs b/scripts/Utils/Grid.cs new file mode 100644 index 0000000..bf5d98f --- /dev/null +++ b/scripts/Utils/Grid.cs @@ -0,0 +1,14 @@ +using Godot; +using System; + +public partial class Grid : GodotObject { + public static readonly Vector2I tileSize = new(16, 16); + + public static Vector2I WorldToGrid(Vector2 coord) { + return (Vector2I)(coord / tileSize); + } + + public static Vector2 GridToWorld(Vector2I coord) { + return coord * tileSize; + } +} diff --git a/scripts/Utils/Grid.cs.uid b/scripts/Utils/Grid.cs.uid new file mode 100644 index 0000000..c18e354 --- /dev/null +++ b/scripts/Utils/Grid.cs.uid @@ -0,0 +1 @@ +uid://75s33rka6l60 diff --git a/scripts/actors/Actor.cs b/scripts/actors/Actor.cs new file mode 100644 index 0000000..8e33b02 --- /dev/null +++ b/scripts/actors/Actor.cs @@ -0,0 +1,23 @@ +using Godot; + +[GlobalClass] +public abstract partial class Actor : Sprite2D { + private Vector2I gridPosition = Vector2I.Zero; + public Vector2I GridPosition { + set { + gridPosition = value; + Position = Grid.GridToWorld(value); + } + get => gridPosition; + } + + public override void _Ready() + { + base._Ready(); + GridPosition = Grid.WorldToGrid(Position); + } + + public void Walk(Vector2I offset) { + GridPosition += offset; + } +} \ No newline at end of file diff --git a/scripts/actors/Actor.cs.uid b/scripts/actors/Actor.cs.uid new file mode 100644 index 0000000..cf29b40 --- /dev/null +++ b/scripts/actors/Actor.cs.uid @@ -0,0 +1 @@ +uid://c0cm4woy8lawl diff --git a/scripts/actors/Player.cs b/scripts/actors/Player.cs new file mode 100644 index 0000000..c8abe12 --- /dev/null +++ b/scripts/actors/Player.cs @@ -0,0 +1,8 @@ +using Godot; +using System; + +[GlobalClass] +public partial class Player : Actor +{ + +} diff --git a/scripts/actors/Player.cs.uid b/scripts/actors/Player.cs.uid new file mode 100644 index 0000000..8229b7f --- /dev/null +++ b/scripts/actors/Player.cs.uid @@ -0,0 +1 @@ +uid://c840l08453pu2 diff --git a/scripts/actors/actions/Action.cs b/scripts/actors/actions/Action.cs new file mode 100644 index 0000000..21efa1a --- /dev/null +++ b/scripts/actors/actions/Action.cs @@ -0,0 +1,6 @@ +using Godot; +using System; + +public abstract partial class Action : RefCounted { + public abstract void Perform(Game game, Actor actor); +} diff --git a/scripts/actors/actions/Action.cs.uid b/scripts/actors/actions/Action.cs.uid new file mode 100644 index 0000000..9523b0a --- /dev/null +++ b/scripts/actors/actions/Action.cs.uid @@ -0,0 +1 @@ +uid://dlejckfyro2ch diff --git a/scripts/actors/actions/MovementAction.cs b/scripts/actors/actions/MovementAction.cs new file mode 100644 index 0000000..f29e2e8 --- /dev/null +++ b/scripts/actors/actions/MovementAction.cs @@ -0,0 +1,22 @@ +using Godot; +using System; + +public partial class MovementAction : Action +{ + public Vector2I Offset { get; private set; } + public MovementAction(Vector2I offset) + { + Offset = offset; + } + + public override void Perform(Game game, Actor actor) + { + Vector2I finalDestination = actor.GridPosition + Offset; + + TileData tile = game.Dungeon.GetCellTileData(finalDestination); + + if (tile == null || !(bool) tile.GetCustomData("isWalkable")) return; + + actor.Walk(Offset); + } +} diff --git a/scripts/actors/actions/MovementAction.cs.uid b/scripts/actors/actions/MovementAction.cs.uid new file mode 100644 index 0000000..07569ef --- /dev/null +++ b/scripts/actors/actions/MovementAction.cs.uid @@ -0,0 +1 @@ +uid://cdtpdaeg7hh6p -- cgit v1.2.3