mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 11:15:33 +00:00
150 lines
No EOL
5.4 KiB
C#
150 lines
No EOL
5.4 KiB
C#
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<Vector2I, RogueliteRoom> RoomGrid => RoomManager.RoomGrid;
|
|
|
|
private List<RogueliteRoom> SpawnedRooms => RoomManager.SpawnedRooms;
|
|
|
|
private List<RoomConnection> Connections => RoomManager.Connections;
|
|
|
|
[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<Vector2I, RogueliteRoom> 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);
|
|
}
|
|
|
|
DrawRoomConnections( Connections, min, offset);
|
|
}
|
|
|
|
private void DrawRoomConnections(List<RoomConnection> connections, Vector2I minGrid, Vector2 baseOffset)
|
|
{
|
|
foreach (var connection in connections)
|
|
{
|
|
// Skip if either room is missing
|
|
if (!RoomGrid.ContainsKey(connection.From) || !RoomGrid.ContainsKey(connection.To))
|
|
continue;
|
|
|
|
// // Calculate center positions of each room
|
|
// Vector2 from = baseOffset + ((connection.From - minGrid) * (CellSize + (int)Separation)) + new Vector2(CellSize / 2f, CellSize / 2f);
|
|
// Vector2 to = baseOffset + ((connection.To - minGrid) * (CellSize + (int)Separation)) + new Vector2(CellSize / 2f, Separation / 2f);
|
|
|
|
// Get relative positions of the two rooms
|
|
Vector2 fromPos = baseOffset + ((connection.From - minGrid) * (CellSize + (int)Separation));
|
|
Vector2 toPos = baseOffset + ((connection.To - minGrid) * (CellSize + (int)Separation));
|
|
|
|
// Determine direction between rooms (assumes adjacent rooms only)
|
|
Vector2 delta = (toPos - fromPos).Normalized();
|
|
|
|
// Midpoint between rooms, where the gap is
|
|
Vector2 gapCenter = (fromPos + toPos) * 0.5f + new Vector2(CellSize / 2f, CellSize / 2f);
|
|
|
|
// Calculate endpoints of the connection line (centered in gap)
|
|
Vector2 halfGap = delta * (Separation / 2f);
|
|
Vector2 lineStart = gapCenter - halfGap;
|
|
Vector2 lineEnd = gapCenter + halfGap;
|
|
|
|
// Pick color based on type
|
|
Color color = Colors.White;
|
|
if (connection.IsSecret)
|
|
color = Colors.Purple;
|
|
else if (connection.IsLocked)
|
|
color = Colors.Orange;
|
|
|
|
float thickness = 1f;
|
|
|
|
// Draw the line
|
|
RenderingServer.CanvasItemAddLine(_canvasItemRid, lineStart, lineEnd, color, thickness);
|
|
}
|
|
}
|
|
|
|
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
|
|
};
|
|
}
|
|
} |