summaryrefslogtreecommitdiff
path: root/scripts/map
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/map')
-rw-r--r--scripts/map/MapData.cs87
-rw-r--r--scripts/map/MapData.cs.uid1
-rw-r--r--scripts/map/Tile.cs22
-rw-r--r--scripts/map/Tile.cs.uid1
-rw-r--r--scripts/map/TileDefinition.cs14
-rw-r--r--scripts/map/TileDefinition.cs.uid1
6 files changed, 126 insertions, 0 deletions
diff --git a/scripts/map/MapData.cs b/scripts/map/MapData.cs
new file mode 100644
index 0000000..25d6e09
--- /dev/null
+++ b/scripts/map/MapData.cs
@@ -0,0 +1,87 @@
+using Godot;
+using System;
+using System.Runtime.InteropServices;
+
+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");
+
+ public int Width { get; private set; }
+ public int Height { get; private set; }
+
+ public Godot.Collections.Array<Tile> Tiles { get; private set; } = [];
+
+ public Godot.Collections.Array<Actor> Actors { get; private set; } = [];
+
+ public MapData(int width, int height) {
+ Width = width;
+ Height = height;
+
+ SetupTiles();
+ }
+
+ private void SetupTiles() {
+ for (int i = 0; i < Width; i++)
+ {
+ for (int j = 0; j < Height; j++)
+ {
+ Tile tile;
+ if (i == 0 || j == 0 || i == (Width - 1) || j == (Height - 1)) {
+ tile = new Tile(new Vector2I(i, j), wallDefinition);
+ } else {
+ tile = new Tile(new Vector2I(i, j), floorDefinition);
+ }
+
+ Tiles.Add(tile);
+ }
+ }
+ }
+
+ public void InsertActor(Vector2I pos, Actor actor) {
+ actor.GridPosition = pos;
+ Actors.Add(actor);
+ }
+
+ private int GridToIndex(Vector2I pos) {
+ if (!IsInBounds(pos)) return -1;
+
+ return pos.Y * Width + pos.X;
+ }
+
+ private bool IsInBounds(Vector2I pos) {
+ if (pos.X < 0 || pos.Y < 0) {
+ return false;
+ }
+ if (pos.X >= Width || pos.Y >= Height) {
+ return false;
+ }
+
+ return true;
+ }
+
+ public Tile GetTile(Vector2I pos) {
+ int index = GridToIndex(pos);
+
+ if (index < 0) return null;
+
+ return Tiles[index];
+ }
+
+ public Actor GetBlockingActorAtPosition(Vector2I pos) {
+ foreach (Actor actor in Actors) {
+ if (actor.GridPosition == pos && actor.BlocksMovement) {
+ return actor;
+ }
+ }
+ return null;
+ }
+
+ public bool IsTileWalkable(Vector2I pos) {
+ Tile tile = GetTile(pos);
+
+ if (tile == null) return false;
+
+ return tile.IsWalkable;
+ }
+}
diff --git a/scripts/map/MapData.cs.uid b/scripts/map/MapData.cs.uid
new file mode 100644
index 0000000..6c226e7
--- /dev/null
+++ b/scripts/map/MapData.cs.uid
@@ -0,0 +1 @@
+uid://0vbcl1etfcbg
diff --git a/scripts/map/Tile.cs b/scripts/map/Tile.cs
new file mode 100644
index 0000000..c910b01
--- /dev/null
+++ b/scripts/map/Tile.cs
@@ -0,0 +1,22 @@
+using Godot;
+using System;
+
+public partial class Tile : Sprite2D
+{
+ private TileDefinition definition;
+
+ public bool IsWalkable { get; set; }
+
+ public Tile(Vector2I pos, TileDefinition definition)
+ {
+ Centered = false;
+ Position = Grid.GridToWorld(pos);
+ SetDefinition(definition);
+ }
+
+ public void SetDefinition(TileDefinition definition) {
+ this.definition = definition;
+ Texture = definition.Texture;
+ this.IsWalkable = definition.IsWalkable;
+ }
+}
diff --git a/scripts/map/Tile.cs.uid b/scripts/map/Tile.cs.uid
new file mode 100644
index 0000000..9fa3c81
--- /dev/null
+++ b/scripts/map/Tile.cs.uid
@@ -0,0 +1 @@
+uid://b2dkecc01tqc0
diff --git a/scripts/map/TileDefinition.cs b/scripts/map/TileDefinition.cs
new file mode 100644
index 0000000..548fda7
--- /dev/null
+++ b/scripts/map/TileDefinition.cs
@@ -0,0 +1,14 @@
+using Godot;
+using System;
+
+[GlobalClass]
+public partial class TileDefinition : Resource
+{
+ [ExportCategory("Visuals")]
+ [Export]
+ public Texture2D Texture { get; set; }
+
+ [ExportCategory("Mechanics")]
+ [Export]
+ public bool IsWalkable { get; set; }
+}
diff --git a/scripts/map/TileDefinition.cs.uid b/scripts/map/TileDefinition.cs.uid
new file mode 100644
index 0000000..14f2903
--- /dev/null
+++ b/scripts/map/TileDefinition.cs.uid
@@ -0,0 +1 @@
+uid://ba82a33ov6uuo