diff options
| -rw-r--r-- | scenes/DungeonLevel.tscn | 6 | ||||
| -rw-r--r-- | scripts/Game.cs | 4 | ||||
| -rw-r--r-- | scripts/InputHandler.cs | 12 | ||||
| -rw-r--r-- | scripts/actors/Actor.cs | 5 | ||||
| -rw-r--r-- | scripts/actors/actions/Action.cs | 13 | ||||
| -rw-r--r-- | scripts/actors/actions/BumpAction.cs | 12 | ||||
| -rw-r--r-- | scripts/actors/actions/DirectionalAction.cs | 6 | ||||
| -rw-r--r-- | scripts/actors/actions/MeleeAction.cs | 6 | ||||
| -rw-r--r-- | scripts/actors/actions/MovementAction.cs | 8 |
9 files changed, 46 insertions, 26 deletions
diff --git a/scenes/DungeonLevel.tscn b/scenes/DungeonLevel.tscn index 83a33bb..4a5fda0 100644 --- a/scenes/DungeonLevel.tscn +++ b/scenes/DungeonLevel.tscn @@ -16,8 +16,10 @@ tile_set = ExtResource("1_bxfih") [node name="Actors" type="Node2D" parent="."] -[node name="Player" parent="Actors" instance=ExtResource("2_u6mlv")] +[node name="Player" parent="Actors" node_paths=PackedStringArray("Map") instance=ExtResource("2_u6mlv")] position = Vector2(144, 144) +Map = NodePath("../..") -[node name="Skeleton" parent="Actors" instance=ExtResource("4_8elbs")] +[node name="Skeleton" parent="Actors" node_paths=PackedStringArray("Map") instance=ExtResource("4_8elbs")] position = Vector2(160, 128) +Map = NodePath("../..") diff --git a/scripts/Game.cs b/scripts/Game.cs index 21531fc..067f4a4 100644 --- a/scripts/Game.cs +++ b/scripts/Game.cs @@ -19,10 +19,10 @@ public partial class Game : Node { public override void _PhysicsProcess(double delta) { base._PhysicsProcess(delta); - Action action = inputHandler.GetAction(); + Action action = inputHandler.GetAction(player); if (action != null) { - action.Perform(this, player); + action.Perform(); HandleEnemyTurns(); } } diff --git a/scripts/InputHandler.cs b/scripts/InputHandler.cs index 5194841..807ec83 100644 --- a/scripts/InputHandler.cs +++ b/scripts/InputHandler.cs @@ -2,19 +2,19 @@ using Godot; using System; public partial class InputHandler : Node { - public Action GetAction() { + public Action GetAction(Player player) { Action action = null; if (Input.IsActionJustPressed("walk-up")) { - action = new BumpAction(Vector2I.Up); + action = new BumpAction(player, Vector2I.Up); } else if (Input.IsActionJustPressed("walk-down")) { - action = new BumpAction(Vector2I.Down); + action = new BumpAction(player, Vector2I.Down); } else if (Input.IsActionJustPressed("walk-left")) { - action = new BumpAction(Vector2I.Left); + action = new BumpAction(player, Vector2I.Left); } else if (Input.IsActionJustPressed("walk-right")) { - action = new BumpAction(Vector2I.Right); + action = new BumpAction(player, Vector2I.Right); } else if (Input.IsActionJustPressed("skip-turn")) { - action = new BumpAction(Vector2I.Zero); + action = new BumpAction(player, Vector2I.Zero); } return action; diff --git a/scripts/actors/Actor.cs b/scripts/actors/Actor.cs index f35939a..f429ead 100644 --- a/scripts/actors/Actor.cs +++ b/scripts/actors/Actor.cs @@ -1,10 +1,13 @@ using Godot; [GlobalClass] -public abstract partial class Actor : Sprite2D { +public abstract partial class Actor : Sprite2D +{ [Export] private ActorDefinition definition; private Vector2I gridPosition = Vector2I.Zero; + [Export] + public DungeonLevel Map { get; private set; } public Vector2I GridPosition { set { gridPosition = value; diff --git a/scripts/actors/actions/Action.cs b/scripts/actors/actions/Action.cs index 21efa1a..76ae5d5 100644 --- a/scripts/actors/actions/Action.cs +++ b/scripts/actors/actions/Action.cs @@ -1,6 +1,17 @@ using Godot; using System; +using System.Data; public abstract partial class Action : RefCounted { - public abstract void Perform(Game game, Actor actor); + protected Actor actor; + + public Action(Actor actor) { + this.actor = actor; + } + + public abstract void Perform(); + + protected DungeonLevel Map { + get => actor.Map; + } } diff --git a/scripts/actors/actions/BumpAction.cs b/scripts/actors/actions/BumpAction.cs index e1fb706..5958b11 100644 --- a/scripts/actors/actions/BumpAction.cs +++ b/scripts/actors/actions/BumpAction.cs @@ -3,22 +3,22 @@ using System; public partial class BumpAction : DirectionalAction { - public BumpAction(Vector2I offset) : base(offset) + public BumpAction(Actor actor, Vector2I offset) : base(actor, offset) { } - public override void Perform(Game game, Actor actor) + public override void Perform() { Vector2I destination = actor.GridPosition + Offset; Action action; - if (game.Map.GetBlockingActorAtPosition(destination) != null) { - action = new MeleeAction(Offset); + if (GetBlockingActorAtPosition(destination) != null) { + action = new MeleeAction(actor, Offset); } else { - action = new MovementAction(Offset); + action = new MovementAction(actor, Offset); } - action.Perform(game, actor); + action.Perform(); } } diff --git a/scripts/actors/actions/DirectionalAction.cs b/scripts/actors/actions/DirectionalAction.cs index 7512141..077f475 100644 --- a/scripts/actors/actions/DirectionalAction.cs +++ b/scripts/actors/actions/DirectionalAction.cs @@ -4,8 +4,12 @@ using System; public abstract partial class DirectionalAction : Action { public Vector2I Offset { get; private set; } - public DirectionalAction(Vector2I offset) + public DirectionalAction(Actor actor, Vector2I offset) : base(actor) { Offset = offset; } + + protected Actor GetBlockingActorAtPosition(Vector2I pos) { + return Map.GetBlockingActorAtPosition(pos); + } } diff --git a/scripts/actors/actions/MeleeAction.cs b/scripts/actors/actions/MeleeAction.cs index 1a74c22..c6d0960 100644 --- a/scripts/actors/actions/MeleeAction.cs +++ b/scripts/actors/actions/MeleeAction.cs @@ -4,15 +4,15 @@ using System.Net.NetworkInformation; public partial class MeleeAction : DirectionalAction { - public MeleeAction(Vector2I offset) : base(offset) + public MeleeAction(Actor actor, Vector2I offset) : base(actor, offset) { } - public override void Perform(Game game, Actor actor) + public override void Perform() { Vector2I destination = actor.GridPosition + Offset; - Actor target = game.Map.GetBlockingActorAtPosition(destination); + Actor target = GetBlockingActorAtPosition(destination); if (target == null) return; diff --git a/scripts/actors/actions/MovementAction.cs b/scripts/actors/actions/MovementAction.cs index 3b203ca..c9474b0 100644 --- a/scripts/actors/actions/MovementAction.cs +++ b/scripts/actors/actions/MovementAction.cs @@ -3,17 +3,17 @@ using System; public partial class MovementAction : DirectionalAction { - public MovementAction(Vector2I offset) : base(offset) + public MovementAction(Actor actor, Vector2I offset) : base(actor, offset) { } - public override void Perform(Game game, Actor actor) + public override void Perform() { Vector2I finalDestination = actor.GridPosition + Offset; - if (!game.Map.IsTileWalkable(finalDestination)) return; + if (!Map.IsTileWalkable(finalDestination)) return; - if (game.Map.GetBlockingActorAtPosition(finalDestination) != null) return; + if (GetBlockingActorAtPosition(finalDestination) != null) return; actor.Walk(Offset); } |
