summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorMatheus <matheus.guedes.mg.m@gmail.com>2025-08-17 19:32:36 -0300
committerMatheus <matheus.guedes.mg.m@gmail.com>2025-08-17 19:32:36 -0300
commitca782d7310f2df854a8727535ac92337c85ed445 (patch)
tree7df571c01d48cac56fb8362d4d091bdd2a55c09a /scripts
parentadb86dd1725276457c13e9a4d37372fda628dea3 (diff)
Gigantesco refactor
Diffstat (limited to 'scripts')
-rw-r--r--scripts/Actor.cs36
-rw-r--r--scripts/Character.cs48
-rw-r--r--scripts/DungeonLevel.cs21
-rw-r--r--scripts/DungeonLevel.cs.uid2
-rw-r--r--scripts/Enemy.cs7
-rw-r--r--scripts/Game.cs28
-rw-r--r--scripts/Game.cs.uid1
-rw-r--r--scripts/InputHandler.cs22
-rw-r--r--scripts/InputHandler.cs.uid1
-rw-r--r--scripts/TurnManager.cs55
-rw-r--r--scripts/TurnManager.cs.uid1
-rw-r--r--scripts/Utils/Grid.cs14
-rw-r--r--scripts/Utils/Grid.cs.uid1
-rw-r--r--scripts/actors/Actor.cs23
-rw-r--r--scripts/actors/Actor.cs.uid (renamed from scripts/Actor.cs.uid)0
-rw-r--r--scripts/actors/Player.cs8
-rw-r--r--scripts/actors/Player.cs.uid (renamed from scripts/Character.cs.uid)0
-rw-r--r--scripts/actors/actions/Action.cs6
-rw-r--r--scripts/actors/actions/Action.cs.uid1
-rw-r--r--scripts/actors/actions/MovementAction.cs22
-rw-r--r--scripts/actors/actions/MovementAction.cs.uid1
21 files changed, 143 insertions, 155 deletions
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/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/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>("TurnManager");
+ private Node2D actors;
- turnManager.Tick();
- }
+ public override void _Ready()
+ {
+ base._Ready();
- public void OnTurnEnd() {
- turnManager.Tick();
+ buildingLayer = GetNode<TileMapLayer>("Dungeon");
+ actors = GetNode<Node2D>("Actors");
+ player = actors.GetNode<Player>("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<DungeonLevel>("Map");
+
+ inputHandler = GetNode<InputHandler>("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<Node> 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/Actor.cs.uid b/scripts/actors/Actor.cs.uid
index cf29b40..cf29b40 100644
--- a/scripts/Actor.cs.uid
+++ b/scripts/actors/Actor.cs.uid
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/Character.cs.uid b/scripts/actors/Player.cs.uid
index 8229b7f..8229b7f 100644
--- a/scripts/Character.cs.uid
+++ b/scripts/actors/Player.cs.uid
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