summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--assets/definitions/actor/Player.tres5
-rw-r--r--assets/definitions/actor/Skeleton.tres12
-rw-r--r--scenes/Skeleton.tscn2
-rw-r--r--scripts/Enemy.cs9
-rw-r--r--scripts/Game.cs5
-rw-r--r--scripts/actors/AI/BaseAI.cs20
-rw-r--r--scripts/actors/AI/BaseAI.cs.uid1
-rw-r--r--scripts/actors/AI/HostileEnemyAI.cs38
-rw-r--r--scripts/actors/AI/HostileEnemyAI.cs.uid1
-rw-r--r--scripts/actors/Actor.cs60
-rw-r--r--scripts/actors/ActorDefinition.cs12
-rw-r--r--scripts/actors/Enemy.cs34
-rw-r--r--scripts/actors/Enemy.cs.uid (renamed from scripts/Enemy.cs.uid)0
-rw-r--r--scripts/actors/EnemyDefinition.cs8
-rw-r--r--scripts/actors/EnemyDefinition.cs.uid1
-rw-r--r--scripts/actors/actions/MovementAction.cs2
-rw-r--r--scripts/map/DungeonGenerator.cs7
-rw-r--r--scripts/map/MapData.cs36
18 files changed, 225 insertions, 28 deletions
diff --git a/assets/definitions/actor/Player.tres b/assets/definitions/actor/Player.tres
index 798bfde..e1c0bd6 100644
--- a/assets/definitions/actor/Player.tres
+++ b/assets/definitions/actor/Player.tres
@@ -8,4 +8,9 @@ script = ExtResource("1_2k33r")
name = "Jogador"
texture = ExtResource("2_2xyvf")
blocksMovement = true
+Hp = 12
+Mp = 12
+Atk = 6
+Def = 2
+Men = 3
metadata/_custom_type_script = "uid://crxw1e37xlrrt"
diff --git a/assets/definitions/actor/Skeleton.tres b/assets/definitions/actor/Skeleton.tres
index be38337..6bc66f5 100644
--- a/assets/definitions/actor/Skeleton.tres
+++ b/assets/definitions/actor/Skeleton.tres
@@ -1,11 +1,17 @@
-[gd_resource type="Resource" script_class="ActorDefinition" load_steps=3 format=3 uid="uid://bphsl6o2nsb8x"]
+[gd_resource type="Resource" script_class="EnemyDefinition" load_steps=3 format=3 uid="uid://gt6xqa737dyt"]
-[ext_resource type="Script" uid="uid://crxw1e37xlrrt" path="res://scripts/actors/ActorDefinition.cs" id="1_m5x88"]
+[ext_resource type="Script" uid="uid://dkfdm2m2scyks" path="res://scripts/actors/EnemyDefinition.cs" id="1_m5x88"]
[ext_resource type="Texture2D" uid="uid://dh5sgjdwkps88" path="res://assets/sprites/skeleton.png" id="2_hhidw"]
[resource]
script = ExtResource("1_m5x88")
+AI = 1
name = "Eis que leto"
texture = ExtResource("2_hhidw")
blocksMovement = true
-metadata/_custom_type_script = "uid://crxw1e37xlrrt"
+Hp = 12
+Mp = 2
+Atk = 3
+Def = 2
+Men = 2
+metadata/_custom_type_script = "uid://dkfdm2m2scyks"
diff --git a/scenes/Skeleton.tscn b/scenes/Skeleton.tscn
index fce5c1e..dbccfbb 100644
--- a/scenes/Skeleton.tscn
+++ b/scenes/Skeleton.tscn
@@ -1,7 +1,7 @@
[gd_scene load_steps=3 format=3 uid="uid://byyjwp5j4byux"]
[ext_resource type="Texture2D" uid="uid://dh5sgjdwkps88" path="res://assets/sprites/skeleton.png" id="1_s4oxe"]
-[ext_resource type="Script" uid="uid://bef1fo3vgvxej" path="res://scripts/Enemy.cs" id="2_at7gs"]
+[ext_resource type="Script" uid="uid://bef1fo3vgvxej" path="res://scripts/actors/Enemy.cs" id="2_at7gs"]
[node name="Skeleton" type="Sprite2D"]
texture = ExtResource("1_s4oxe")
diff --git a/scripts/Enemy.cs b/scripts/Enemy.cs
deleted file mode 100644
index 6f9f3d9..0000000
--- a/scripts/Enemy.cs
+++ /dev/null
@@ -1,9 +0,0 @@
-using Godot;
-using System;
-
-public partial class Enemy : Actor
-{
- public Enemy(Vector2I initialPosition, MapData map, ActorDefinition definition) : base(initialPosition, map, definition)
- {
- }
-}
diff --git a/scripts/Game.cs b/scripts/Game.cs
index 5c18cd7..818b064 100644
--- a/scripts/Game.cs
+++ b/scripts/Game.cs
@@ -44,8 +44,9 @@ public partial class Game : Node {
private void HandleEnemyTurns() {
foreach (Actor actor in Map.Map_Data.Actors) {
if (actor is Player) continue;
-
- GD.Print($"O {actor.ActorName} foi cuckado e não tem como agir.");
+ if (actor is Enemy enemy && enemy.IsAlive) {
+ enemy.Soul.Perform();
+ }
}
}
}
diff --git a/scripts/actors/AI/BaseAI.cs b/scripts/actors/AI/BaseAI.cs
new file mode 100644
index 0000000..f9b5387
--- /dev/null
+++ b/scripts/actors/AI/BaseAI.cs
@@ -0,0 +1,20 @@
+using Godot;
+
+public abstract partial class BaseAI : Node {
+ protected Actor body;
+
+ public override void _Ready()
+ {
+ base._Ready();
+ body = GetParent<Actor>();
+ }
+
+ public abstract void Perform();
+
+ public Godot.Collections.Array<Vector2> GetPathTo(Vector2I destination) {
+ Godot.Collections.Array<Vector2> list = [];
+ Vector2[] path = body.Map_Data.Pathfinder.GetPointPath(body.GridPosition, destination);
+ list.AddRange(path);
+ return list;
+ }
+} \ No newline at end of file
diff --git a/scripts/actors/AI/BaseAI.cs.uid b/scripts/actors/AI/BaseAI.cs.uid
new file mode 100644
index 0000000..b23724c
--- /dev/null
+++ b/scripts/actors/AI/BaseAI.cs.uid
@@ -0,0 +1 @@
+uid://jgm5qk02hism
diff --git a/scripts/actors/AI/HostileEnemyAI.cs b/scripts/actors/AI/HostileEnemyAI.cs
new file mode 100644
index 0000000..061295f
--- /dev/null
+++ b/scripts/actors/AI/HostileEnemyAI.cs
@@ -0,0 +1,38 @@
+using Godot;
+
+public partial class HostileEnemyAI : BaseAI
+{
+ private Godot.Collections.Array<Vector2> path = [];
+
+ public override void Perform()
+ {
+ Player target = body.Map_Data.Player;
+ Vector2I offset = target.GridPosition - body.GridPosition;
+ int distance = int.Max(int.Abs(offset.X), int.Abs(offset.Y));
+
+ Action action;
+
+ if (body.Map_Data.GetTile(body.GridPosition).IsInView) {
+ if (distance <= 1) {
+ action = new MeleeAction(body, offset);
+ action.Perform();
+ return;
+ }
+ path = GetPathTo(target.GridPosition);
+ GD.Print($"Arno Breker: {path}");
+ path.RemoveAt(0);
+ }
+
+ if (path.Count > 0) {
+ Vector2I destination = (Vector2I) path[0];
+ GD.Print(destination);
+ if (body.Map_Data.GetBlockingActorAtPosition(destination) != null) {
+ return;
+ }
+
+ action = new MovementAction(body, destination - body.GridPosition);
+ action.Perform();
+ path.RemoveAt(0);
+ }
+ }
+} \ No newline at end of file
diff --git a/scripts/actors/AI/HostileEnemyAI.cs.uid b/scripts/actors/AI/HostileEnemyAI.cs.uid
new file mode 100644
index 0000000..0fa2c32
--- /dev/null
+++ b/scripts/actors/AI/HostileEnemyAI.cs.uid
@@ -0,0 +1 @@
+uid://db28cxff4pl3t
diff --git a/scripts/actors/Actor.cs b/scripts/actors/Actor.cs
index e8d728a..199c301 100644
--- a/scripts/actors/Actor.cs
+++ b/scripts/actors/Actor.cs
@@ -3,18 +3,10 @@ using Godot;
[GlobalClass]
public abstract partial class Actor : Sprite2D
{
- private ActorDefinition definition;
- private Vector2I gridPosition = Vector2I.Zero;
+ protected ActorDefinition definition;
public MapData Map_Data { get; set; }
- public Actor(Vector2I initialPosition, MapData map, ActorDefinition definition) {
- GridPosition = initialPosition;
- Map_Data = map;
- this.definition = definition;
- Texture = definition.texture;
- Centered = false;
- }
-
+ private Vector2I gridPosition = Vector2I.Zero;
public Vector2I GridPosition {
set {
gridPosition = value;
@@ -31,6 +23,30 @@ public abstract partial class Actor : Sprite2D
get => definition.name;
}
+ private int hp;
+ public int MaxHp { get; private set; }
+ public int Hp {
+ get => hp;
+ set {
+ hp = int.Clamp(value, 0, MaxHp);
+ }
+ }
+
+ private int mp;
+ public int MaxMp { get; private set; }
+ public int Mp {
+ get => mp;
+ set {
+ mp = int.Clamp(value, 0, MaxMp);
+ }
+ }
+
+ public int Atk { get; private set; }
+
+ public int Def { get; private set; }
+
+ public int Men { get; private set; }
+
public override void _Ready()
{
base._Ready();
@@ -38,6 +54,30 @@ public abstract partial class Actor : Sprite2D
}
public void Walk(Vector2I offset) {
+ Map_Data.UnregisterBlockingActor(this);
GridPosition += offset;
+ Map_Data.RegisterBlockingActor(this);
+ }
+
+ public Actor(Vector2I initialPosition, MapData map, ActorDefinition definition) {
+ GridPosition = initialPosition;
+ Map_Data = map;
+ Centered = false;
+
+ SetDefinition(definition);
+ }
+
+ public virtual void SetDefinition(ActorDefinition definition) {
+ this.definition = definition;
+ Texture = definition.texture;
+
+ MaxHp = definition.Hp;
+ Hp = definition.Hp;
+ MaxMp = definition.Mp;
+ Mp = definition.Mp;
+
+ Atk = definition.Atk;
+ Def = definition.Def;
+ Men = definition.Men;
}
} \ No newline at end of file
diff --git a/scripts/actors/ActorDefinition.cs b/scripts/actors/ActorDefinition.cs
index 5678921..5c0ece9 100644
--- a/scripts/actors/ActorDefinition.cs
+++ b/scripts/actors/ActorDefinition.cs
@@ -13,4 +13,16 @@ public partial class ActorDefinition : Resource
[ExportCategory("Mechanics")]
[Export]
public bool blocksMovement = true;
+
+ [ExportCategory("Stats")]
+ [Export]
+ public int Hp;
+ [Export]
+ public int Mp;
+ [Export]
+ public int Atk;
+ [Export]
+ public int Def;
+ [Export]
+ public int Men;
}
diff --git a/scripts/actors/Enemy.cs b/scripts/actors/Enemy.cs
new file mode 100644
index 0000000..6c111b1
--- /dev/null
+++ b/scripts/actors/Enemy.cs
@@ -0,0 +1,34 @@
+using Godot;
+using System;
+
+public enum AIType
+{
+ None,
+ DefaultHostile
+};
+
+public partial class Enemy : Actor
+{
+ public BaseAI Soul { get; private set; }
+
+ public bool IsAlive { get => Soul != null; }
+
+ public Enemy(Vector2I initialPosition, MapData map, EnemyDefinition definition) : base(initialPosition, map, definition)
+ {
+ SetDefinition(definition);
+ }
+
+ public void SetDefinition(EnemyDefinition definition)
+ {
+ base.SetDefinition(definition);
+
+ switch(definition.AI) {
+ case AIType.None:
+ break;
+ case AIType.DefaultHostile:
+ Soul = new HostileEnemyAI();
+ AddChild(Soul);
+ break;
+ }
+ }
+}
diff --git a/scripts/Enemy.cs.uid b/scripts/actors/Enemy.cs.uid
index 93255b7..93255b7 100644
--- a/scripts/Enemy.cs.uid
+++ b/scripts/actors/Enemy.cs.uid
diff --git a/scripts/actors/EnemyDefinition.cs b/scripts/actors/EnemyDefinition.cs
new file mode 100644
index 0000000..21d4ca0
--- /dev/null
+++ b/scripts/actors/EnemyDefinition.cs
@@ -0,0 +1,8 @@
+using Godot;
+
+[GlobalClass]
+public partial class EnemyDefinition : ActorDefinition {
+ [ExportCategory("AI")]
+ [Export]
+ public AIType AI;
+} \ No newline at end of file
diff --git a/scripts/actors/EnemyDefinition.cs.uid b/scripts/actors/EnemyDefinition.cs.uid
new file mode 100644
index 0000000..1ba03e1
--- /dev/null
+++ b/scripts/actors/EnemyDefinition.cs.uid
@@ -0,0 +1 @@
+uid://dkfdm2m2scyks
diff --git a/scripts/actors/actions/MovementAction.cs b/scripts/actors/actions/MovementAction.cs
index 54bb99c..704dd4f 100644
--- a/scripts/actors/actions/MovementAction.cs
+++ b/scripts/actors/actions/MovementAction.cs
@@ -15,6 +15,8 @@ public partial class MovementAction : DirectionalAction
if (GetBlockingActorAtPosition(finalDestination) != null) return;
+ GD.Print("What?");
+
actor.Walk(Offset);
}
}
diff --git a/scripts/map/DungeonGenerator.cs b/scripts/map/DungeonGenerator.cs
index 9a2ceae..ebb7a3d 100644
--- a/scripts/map/DungeonGenerator.cs
+++ b/scripts/map/DungeonGenerator.cs
@@ -2,8 +2,8 @@ using Godot;
public partial class DungeonGenerator : Node
{
- private static readonly Godot.Collections.Array<ActorDefinition> enemies = [
- GD.Load<ActorDefinition>("res://assets/definitions/actor/Skeleton.tres")
+ private static readonly Godot.Collections.Array<EnemyDefinition> enemies = [
+ GD.Load<EnemyDefinition>("res://assets/definitions/actor/Skeleton.tres")
];
[ExportCategory("Dimension")]
@@ -87,6 +87,7 @@ public partial class DungeonGenerator : Node
PlaceEntities(data, room);
}
+ data.SetupPathfinding();
return data;
}
@@ -108,7 +109,7 @@ public partial class DungeonGenerator : Node
}
if (canPlace) {
- ActorDefinition definition = enemies.PickRandom();
+ EnemyDefinition definition = enemies.PickRandom();
Enemy enemy = new Enemy(position, data, definition);
data.InsertActor(enemy);
}
diff --git a/scripts/map/MapData.cs b/scripts/map/MapData.cs
index ea18b4b..be466ba 100644
--- a/scripts/map/MapData.cs
+++ b/scripts/map/MapData.cs
@@ -15,6 +15,42 @@ public partial class MapData : RefCounted
public Player Player { get; set; }
public Godot.Collections.Array<Actor> Actors { get; private set; } = [];
+ private AStarGrid2D pathfinder;
+ public AStarGrid2D Pathfinder { get => pathfinder; }
+ private static float ActorWeight = 10.0f;
+
+ public void SetupPathfinding() {
+ pathfinder = new AStarGrid2D
+ {
+ Region = new Rect2I(0, 0, Width, Height)
+ };
+
+ pathfinder.Update();
+
+ for (int y = 0; y < Height; y++) {
+ for (int x = 0; x < Width; x++) {
+ Vector2I pos = new Vector2I(x, y);
+ Tile tile = GetTile(pos);
+ pathfinder.SetPointSolid(pos, !tile.IsWalkable);
+ }
+ }
+
+ foreach (Actor actor in Actors) {
+ if (actor.BlocksMovement) {
+ RegisterBlockingActor(actor);
+ }
+ }
+
+ }
+
+ public void RegisterBlockingActor(Actor actor) {
+ pathfinder.SetPointWeightScale(actor.GridPosition, ActorWeight);
+ }
+
+ public void UnregisterBlockingActor(Actor actor) {
+ pathfinder.SetPointWeightScale(actor.GridPosition, 0);
+ }
+
public MapData(int width, int height, Player player) {
Width = width;
Height = height;