From 60f755aca579a18fc855b15b471bf3796ca26d82 Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 15 Apr 2025 12:04:22 +0200 Subject: [PATCH] Minimap --- Resources/Debug/DebugLevels.tres | 28 +++--- Scenes/Maps/Roguelike.tscn | 8 +- Scripts/Controllers/RogueliteRoomManager.cs | 10 ++ Scripts/UI/Minimap.cs | 105 ++++++++++++++++++++ Scripts/UI/Minimap.cs.uid | 1 + 5 files changed, 137 insertions(+), 15 deletions(-) create mode 100644 Scripts/UI/Minimap.cs create mode 100644 Scripts/UI/Minimap.cs.uid diff --git a/Resources/Debug/DebugLevels.tres b/Resources/Debug/DebugLevels.tres index 3a40e9bc..b412fd46 100644 --- a/Resources/Debug/DebugLevels.tres +++ b/Resources/Debug/DebugLevels.tres @@ -21,8 +21,8 @@ _name = "Intro" [sub_resource type="Resource" id="Resource_cfhv5"] script = ExtResource("1_ov731") EggIndex = 0 -StartingEquipment = [] -RemoveEquipment = [] +StartingEquipment = Array[ExtResource("2_bkci5")]([]) +RemoveEquipment = Array[ExtResource("2_bkci5")]([]) [sub_resource type="Resource" id="Resource_0k62o"] script = ExtResource("2_tnajf") @@ -34,8 +34,8 @@ StartData = SubResource("Resource_cfhv5") [sub_resource type="Resource" id="Resource_tpb7s"] script = ExtResource("1_ov731") EggIndex = 0 -StartingEquipment = [] -RemoveEquipment = [] +StartingEquipment = Array[ExtResource("2_bkci5")]([]) +RemoveEquipment = Array[ExtResource("2_bkci5")]([]) [sub_resource type="Resource" id="Resource_edoov"] script = ExtResource("2_tnajf") @@ -47,8 +47,8 @@ StartData = SubResource("Resource_tpb7s") [sub_resource type="Resource" id="Resource_1sw5g"] script = ExtResource("1_ov731") EggIndex = 255 -StartingEquipment = [] -RemoveEquipment = [] +StartingEquipment = Array[ExtResource("2_bkci5")]([]) +RemoveEquipment = Array[ExtResource("2_bkci5")]([]) [sub_resource type="Resource" id="Resource_47bot"] script = ExtResource("2_tnajf") @@ -61,7 +61,7 @@ StartData = SubResource("Resource_1sw5g") script = ExtResource("1_ov731") EggIndex = 2 StartingEquipment = Array[ExtResource("2_bkci5")]([ExtResource("3_fydgr"), ExtResource("4_38yta"), ExtResource("5_em757"), ExtResource("6_sdmg8")]) -RemoveEquipment = [] +RemoveEquipment = Array[ExtResource("2_bkci5")]([]) [sub_resource type="Resource" id="Resource_mgdm6"] script = ExtResource("2_tnajf") @@ -79,8 +79,8 @@ _name = "Rebel Base" [sub_resource type="Resource" id="Resource_maxpt"] script = ExtResource("1_ov731") EggIndex = 0 -StartingEquipment = [] -RemoveEquipment = [] +StartingEquipment = Array[ExtResource("2_bkci5")]([]) +RemoveEquipment = Array[ExtResource("2_bkci5")]([]) [sub_resource type="Resource" id="Resource_pein5"] script = ExtResource("2_tnajf") @@ -117,7 +117,7 @@ _name = "Default Scene" script = ExtResource("1_ov731") EggIndex = 0 StartingEquipment = Array[ExtResource("2_bkci5")]([ExtResource("7_b3oo5")]) -RemoveEquipment = [] +RemoveEquipment = Array[ExtResource("2_bkci5")]([]) [sub_resource type="Resource" id="Resource_6ijnv"] script = ExtResource("2_tnajf") @@ -136,8 +136,8 @@ metadata/_custom_type_script = "uid://dnthdjrx78u6m" [sub_resource type="Resource" id="Resource_7sue8"] script = ExtResource("1_ov731") EggIndex = 255 -StartingEquipment = [] -RemoveEquipment = [] +StartingEquipment = Array[ExtResource("2_bkci5")]([]) +RemoveEquipment = Array[ExtResource("2_bkci5")]([]) metadata/_custom_type_script = "uid://mja0rk7n2kln" [sub_resource type="Resource" id="Resource_ognca"] @@ -151,8 +151,8 @@ metadata/_custom_type_script = "uid://dnthdjrx78u6m" [sub_resource type="Resource" id="Resource_olpjo"] script = ExtResource("1_ov731") EggIndex = 0 -StartingEquipment = [] -RemoveEquipment = [] +StartingEquipment = Array[ExtResource("2_bkci5")]([]) +RemoveEquipment = Array[ExtResource("2_bkci5")]([]) metadata/_custom_type_script = "uid://mja0rk7n2kln" [sub_resource type="Resource" id="Resource_nbnej"] diff --git a/Scenes/Maps/Roguelike.tscn b/Scenes/Maps/Roguelike.tscn index 87fbd08b..b18d3bad 100644 --- a/Scenes/Maps/Roguelike.tscn +++ b/Scenes/Maps/Roguelike.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=16 format=3 uid="uid://bf1kqr3o6r6d4"] +[gd_scene load_steps=17 format=3 uid="uid://bf1kqr3o6r6d4"] [ext_resource type="Script" uid="uid://doxmbokehw8ci" path="res://Scripts/GameManager.cs" id="1_wbqvu"] [ext_resource type="PackedScene" uid="uid://c4pr2707hbeph" path="res://Scenes/Actors/fsm_player.tscn" id="2_3fyis"] @@ -15,6 +15,7 @@ [ext_resource type="Script" uid="uid://bdshph801ac2i" path="res://Scenes/CameraTarget.gd" id="11_4gy5m"] [ext_resource type="Script" uid="uid://cnkipcolyj61w" path="res://Scripts/AlarmManager.cs" id="12_eoca5"] [ext_resource type="PackedScene" uid="uid://b3tyacxxw88lx" path="res://Scenes/Utils/StreamPlayerWithName.tscn" id="13_4n7t6"] +[ext_resource type="Script" uid="uid://3v6q0p5krqn7" path="res://Scripts/UI/Minimap.cs" id="16_pfafs"] [node name="GameScene" type="Node2D"] process_mode = 3 @@ -65,4 +66,9 @@ script = ExtResource("12_eoca5") process_mode = 3 autoplay = true +[node name="Minimap" type="CanvasLayer" parent="." node_paths=PackedStringArray("RoomManager")] +script = ExtResource("16_pfafs") +RoomManager = NodePath("../Maps") +CellSize = 4 + [connection signal="ManagerReady" from="." to="Maps" method="InitSpawning"] diff --git a/Scripts/Controllers/RogueliteRoomManager.cs b/Scripts/Controllers/RogueliteRoomManager.cs index 2b88b160..c0affd13 100644 --- a/Scripts/Controllers/RogueliteRoomManager.cs +++ b/Scripts/Controllers/RogueliteRoomManager.cs @@ -16,6 +16,10 @@ public partial class RogueliteRoomManager : Node2D //private Godot.Collections.Dictionary _grid = new(); private Godot.Collections.Dictionary _roomGrid = new(); + public Godot.Collections.Dictionary RoomGrid => _roomGrid; + + public List SpawnedRooms { get; private set; } = []; + [Export] public Vector2I SpawnOrigin { get; private set; } = Vector2I.Zero; [Export] public int DungeonLength { get; set; } = 10; @@ -29,6 +33,8 @@ public partial class RogueliteRoomManager : Node2D [Export] public Vector2I TileSize { get; set; } = new Vector2I(16, 16); [Export] public Vector2I RoomSizeInTiles { get; set; } = new Vector2I(20, 10); + + [Signal] public delegate void MapCreatedEventHandler(); public override void _Ready() { @@ -123,6 +129,8 @@ public partial class RogueliteRoomManager : Node2D return _roomGrid.ContainsKey(neighborPos); }); } + + EmitSignalMapCreated(); } private void GenerateDungeon() @@ -315,6 +323,8 @@ public partial class RogueliteRoomManager : Node2D spawnedScene.GridPosition = gridPos; + SpawnedRooms.Add(spawnedScene); + // for reference //SpawnRoom(room, origin + (room.Size * new Vector2(i, j) * tileSize)); spawnedScene.Spawn(); diff --git a/Scripts/UI/Minimap.cs b/Scripts/UI/Minimap.cs new file mode 100644 index 00000000..8a50be4d --- /dev/null +++ b/Scripts/UI/Minimap.cs @@ -0,0 +1,105 @@ +using Godot; +using System; +using System.Collections.Generic; +using Cirno.Scripts.Controllers; +using Cirno.Scripts.Enums; + +public partial class Minimap : CanvasLayer +{ + [Export] public RogueliteRoomManager RoomManager { get; private set; } + + private Rid _canvasItemRid; + private Rid _parentCanvas; + private Godot.Collections.Dictionary RoomGrid => RoomManager.RoomGrid; + + private List SpawnedRooms => RoomManager.SpawnedRooms; + + [Export] public int CellSize { get; private set; } = 16; + [Export] public Vector2 Padding { get; private set; } = new Vector2(20, 20); + + [Export] public float Separation = 2f; + + private Vector2 _viewportSize; + + public override void _Ready() + { + RoomManager.MapCreated += RoomManagerOnMapCreated; + + _canvasItemRid = RenderingServer.CanvasItemCreate(); + _parentCanvas = this.GetCanvas(); //GetCanvasItem(); // Gets the RID of this Node2D's canvas + RenderingServer.CanvasItemSetParent(_canvasItemRid, _parentCanvas); + RenderingServer.CanvasItemSetVisible(_canvasItemRid, true); + } + + public override void _EnterTree() + { + // Get current window size to position minimap in top-right + _viewportSize = GetViewport().GetVisibleRect().Size; + } + + + private void RoomManagerOnMapCreated() + { + RedrawMinimap(); + } + + // public void SetRoomGrid(Dictionary roomGrid) + // { + // _roomGrid = roomGrid; + // RedrawMinimap(); + // } + + private void RedrawMinimap() + { + // Clear previous draw commands + RenderingServer.CanvasItemClear(_canvasItemRid); + + if (SpawnedRooms == null) + return; + + // Find bounds + Vector2I min = new(int.MaxValue, int.MaxValue); + Vector2I max = new(int.MinValue, int.MinValue); + + foreach (var room in SpawnedRooms) + { + min = new Vector2I(Math.Min(min.X, room.GridPosition.X), Math.Min(min.Y, room.GridPosition.Y)); + max = new Vector2I(Math.Max(max.X, room.GridPosition.X + room.RoomResource.Size.X), + Math.Max(max.Y, room.GridPosition.Y + room.RoomResource.Size.Y)); + } + + Vector2 mapSize = (max - min) * CellSize; + Vector2 offset = new Vector2( + _viewportSize.X - mapSize.X - Padding.X, // padding from right + Padding.Y // Top padding + ); + + //Vector2 offset = Padding; // Padding + + foreach (var room in SpawnedRooms) + { + Vector2I pos = room.GridPosition; + Vector2I size = room.RoomResource.Size; + Vector2 drawPos = offset + (new Vector2((pos.X - min.X), (pos.Y - min.Y))) * (CellSize + Separation); + Vector2 drawSize = new Vector2(size.X, size.Y) * CellSize + + new Vector2((size.X - 1), (size.Y - 1)) * Separation; + //Vector2 drawSize = new Vector2(size.X, size.Y) * CellSize; + + Color color = GetColorForRoomType(room.RoomResource.Type); + + RenderingServer.CanvasItemAddRect(_canvasItemRid, new Rect2(drawPos, drawSize), color); + } + } + + private Color GetColorForRoomType(RoomType type) + { + return type switch + { + RoomType.Boss => Colors.Red, + RoomType.Shop => Colors.Gold, + RoomType.Regular => Colors.Blue, + RoomType.Starter => Colors.Green, + _ => Colors.DimGray + }; + } +} \ No newline at end of file diff --git a/Scripts/UI/Minimap.cs.uid b/Scripts/UI/Minimap.cs.uid new file mode 100644 index 00000000..ba67f24e --- /dev/null +++ b/Scripts/UI/Minimap.cs.uid @@ -0,0 +1 @@ +uid://3v6q0p5krqn7