From 723e66a82606ee78ca4156a0c0a850185b4c0369 Mon Sep 17 00:00:00 2001 From: Matheus Date: Mon, 18 Aug 2025 18:43:54 -0300 Subject: IT'S OVER --- scripts/DungeonGenerator.cs | 19 +++++++ scripts/DungeonGenerator.cs.uid | 1 + scripts/DungeonLevel.cs | 40 ------------- scripts/Enemy.cs | 2 +- scripts/Game.cs | 21 +++++-- scripts/Map.cs | 27 +++++++++ scripts/Map.cs.uid | 1 + scripts/actors/Actor.cs | 6 +- scripts/actors/Player.cs | 2 +- scripts/actors/actions/Action.cs | 4 +- scripts/actors/actions/DirectionalAction.cs | 2 +- scripts/actors/actions/MovementAction.cs | 2 +- scripts/map/MapData.cs | 87 +++++++++++++++++++++++++++++ scripts/map/MapData.cs.uid | 1 + scripts/map/Tile.cs | 22 ++++++++ scripts/map/Tile.cs.uid | 1 + scripts/map/TileDefinition.cs | 14 +++++ scripts/map/TileDefinition.cs.uid | 1 + 18 files changed, 199 insertions(+), 54 deletions(-) create mode 100644 scripts/DungeonGenerator.cs create mode 100644 scripts/DungeonGenerator.cs.uid delete mode 100644 scripts/DungeonLevel.cs create mode 100644 scripts/Map.cs create mode 100644 scripts/Map.cs.uid create mode 100644 scripts/map/MapData.cs create mode 100644 scripts/map/MapData.cs.uid create mode 100644 scripts/map/Tile.cs create mode 100644 scripts/map/Tile.cs.uid create mode 100644 scripts/map/TileDefinition.cs create mode 100644 scripts/map/TileDefinition.cs.uid (limited to 'scripts') diff --git a/scripts/DungeonGenerator.cs b/scripts/DungeonGenerator.cs new file mode 100644 index 0000000..99b8aac --- /dev/null +++ b/scripts/DungeonGenerator.cs @@ -0,0 +1,19 @@ +using Godot; +using System; + +public partial class DungeonGenerator : Node +{ + private TileMapLayer map; + + [ExportCategory("Parametres")] + [Export] + private int width = 80; + [Export] + private int height = 60; + + public override void _Ready() + { + base._Ready(); + map = GetParent().GetNode("Dungeon"); + } +} diff --git a/scripts/DungeonGenerator.cs.uid b/scripts/DungeonGenerator.cs.uid new file mode 100644 index 0000000..15aeef6 --- /dev/null +++ b/scripts/DungeonGenerator.cs.uid @@ -0,0 +1 @@ +uid://dwyr067lwqcsj diff --git a/scripts/DungeonLevel.cs b/scripts/DungeonLevel.cs deleted file mode 100644 index 68751c5..0000000 --- a/scripts/DungeonLevel.cs +++ /dev/null @@ -1,40 +0,0 @@ -using Godot; -using System; - -public partial class DungeonLevel : Node2D -{ - private TileMapLayer buildingLayer; - public Godot.Collections.Array Actors { get; private set; } = []; - - private Node2D actorsNode; - - public override void _Ready() - { - base._Ready(); - - buildingLayer = GetNode("Dungeon"); - actorsNode = GetNode("Actors"); - } - - public void InsertActor(Actor actor) { - Actors.Add(actor); - actorsNode.AddChild(actor); - } - - public bool IsTileWalkable(Vector2I pos) { - TileData tile = buildingLayer.GetCellTileData(pos); - - if (tile == null) return false; - - return (bool)tile.GetCustomData("isWalkable"); - } - - public Actor GetBlockingActorAtPosition(Vector2I pos) { - foreach (Actor actor in Actors) { - if (actor.GridPosition == pos && actor.BlocksMovement) { - return actor; - } - } - return null; - } -} diff --git a/scripts/Enemy.cs b/scripts/Enemy.cs index 921cd86..6f9f3d9 100644 --- a/scripts/Enemy.cs +++ b/scripts/Enemy.cs @@ -3,7 +3,7 @@ using System; public partial class Enemy : Actor { - public Enemy(Vector2I initialPosition, DungeonLevel map, ActorDefinition definition) : base(initialPosition, map, definition) + public Enemy(Vector2I initialPosition, MapData map, ActorDefinition definition) : base(initialPosition, map, definition) { } } diff --git a/scripts/Game.cs b/scripts/Game.cs index f5c5240..836af84 100644 --- a/scripts/Game.cs +++ b/scripts/Game.cs @@ -3,24 +3,35 @@ using System; public partial class Game : Node { private static readonly ActorDefinition playerDefinition = GD.Load("res://assets/definitions/actor/Player.tres"); + private static readonly ActorDefinition skeletonDefinition = GD.Load("res://assets/definitions/actor/Skeleton.tres"); private Player player; - public DungeonLevel Map { get; private set; } + private Node2D actorsNode; + public MapData Map_Data { get; private set; } private InputHandler inputHandler; public override void _Ready() { base._Ready(); - Map = GetNode("Map"); + Map Map = GetNode("Map"); + + Map_Data = Map.Map_Data; inputHandler = GetNode("InputHandler"); + actorsNode = GetNode("Actors"); - player = new Player(new Vector2I(0, 0), Map, playerDefinition); + player = new Player(Vector2I.Zero, Map_Data, playerDefinition); Camera2D camera = GetNode("Camera2D"); RemoveChild(camera); player.AddChild(camera); - Map.InsertActor(player); + actorsNode.AddChild(player); + + Enemy Skeleton = new Enemy(Vector2I.Zero, Map_Data, skeletonDefinition); + actorsNode.AddChild(Skeleton); + + Map_Data.InsertActor(new Vector2I(1, 1), player); + Map_Data.InsertActor(new Vector2I(3, 4), Skeleton); } public override void _PhysicsProcess(double delta) { @@ -35,7 +46,7 @@ public partial class Game : Node { } private void HandleEnemyTurns() { - foreach (Actor actor in Map.Actors) { + foreach (Actor actor in Map_Data.Actors) { if (actor is Player) continue; GD.Print($"O {actor.ActorName} foi cuckado e não tem como agir."); diff --git a/scripts/Map.cs b/scripts/Map.cs new file mode 100644 index 0000000..6bdd4a4 --- /dev/null +++ b/scripts/Map.cs @@ -0,0 +1,27 @@ +using Godot; +using System; + +public partial class Map : Node2D +{ + public MapData Map_Data { get; private set; } + + [Export] + public int Height; + [Export] + public int Width; + + private void PlaceTiles() { + foreach (Tile tile in Map_Data.Tiles) { + AddChild(tile); + } + } + + public override void _Ready() + { + base._Ready(); + + Map_Data = new MapData(Width, Height); + + PlaceTiles(); + } +} diff --git a/scripts/Map.cs.uid b/scripts/Map.cs.uid new file mode 100644 index 0000000..7306888 --- /dev/null +++ b/scripts/Map.cs.uid @@ -0,0 +1 @@ +uid://fe2h4is11tnw diff --git a/scripts/actors/Actor.cs b/scripts/actors/Actor.cs index 7e9ab04..a759fef 100644 --- a/scripts/actors/Actor.cs +++ b/scripts/actors/Actor.cs @@ -5,11 +5,11 @@ public abstract partial class Actor : Sprite2D { private ActorDefinition definition; private Vector2I gridPosition = Vector2I.Zero; - public DungeonLevel Map { get; private set; } + public MapData Map_Data { get; private set; } - public Actor(Vector2I initialPosition, DungeonLevel map, ActorDefinition definition) { + public Actor(Vector2I initialPosition, MapData map, ActorDefinition definition) { GridPosition = initialPosition; - Map = map; + Map_Data = map; this.definition = definition; Texture = definition.texture; Centered = false; diff --git a/scripts/actors/Player.cs b/scripts/actors/Player.cs index 590020d..c15db21 100644 --- a/scripts/actors/Player.cs +++ b/scripts/actors/Player.cs @@ -4,7 +4,7 @@ using System; [GlobalClass] public partial class Player : Actor { - public Player(Vector2I initialPosition, DungeonLevel map, ActorDefinition definition) : base(initialPosition, map, definition) + public Player(Vector2I initialPosition, MapData map, ActorDefinition definition) : base(initialPosition, map, definition) { } } diff --git a/scripts/actors/actions/Action.cs b/scripts/actors/actions/Action.cs index 76ae5d5..bb5f6f1 100644 --- a/scripts/actors/actions/Action.cs +++ b/scripts/actors/actions/Action.cs @@ -11,7 +11,7 @@ public abstract partial class Action : RefCounted { public abstract void Perform(); - protected DungeonLevel Map { - get => actor.Map; + protected MapData Map_Data { + get => actor.Map_Data; } } diff --git a/scripts/actors/actions/DirectionalAction.cs b/scripts/actors/actions/DirectionalAction.cs index 077f475..d5199f9 100644 --- a/scripts/actors/actions/DirectionalAction.cs +++ b/scripts/actors/actions/DirectionalAction.cs @@ -10,6 +10,6 @@ public abstract partial class DirectionalAction : Action } protected Actor GetBlockingActorAtPosition(Vector2I pos) { - return Map.GetBlockingActorAtPosition(pos); + return Map_Data.GetBlockingActorAtPosition(pos); } } diff --git a/scripts/actors/actions/MovementAction.cs b/scripts/actors/actions/MovementAction.cs index c9474b0..54bb99c 100644 --- a/scripts/actors/actions/MovementAction.cs +++ b/scripts/actors/actions/MovementAction.cs @@ -11,7 +11,7 @@ public partial class MovementAction : DirectionalAction { Vector2I finalDestination = actor.GridPosition + Offset; - if (!Map.IsTileWalkable(finalDestination)) return; + if (!Map_Data.IsTileWalkable(finalDestination)) return; if (GetBlockingActorAtPosition(finalDestination) != null) return; 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("res://assets/definitions/tiles/wall.tres"); + public static readonly TileDefinition floorDefinition = GD.Load("res://assets/definitions/tiles/floor.tres"); + + public int Width { get; private set; } + public int Height { get; private set; } + + public Godot.Collections.Array Tiles { get; private set; } = []; + + public Godot.Collections.Array 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 -- cgit v1.2.3