using System.Collections.Generic;
using ColdMint.scripts.debug;
using ColdMint.scripts.map.events;
using ColdMint.scripts.map.preview;
using ColdMint.scripts.map.room;
using ColdMint.scripts.utils;
using Godot;
namespace ColdMint.scripts.map.miniMap;
///
/// Mini Map
/// 迷你地图
///
public partial class MiniMap : NinePatchRect
{
private Node2D? _roomPreviewContainer;
///
/// The midpoint coordinates of the mini map
/// 迷你地图的中点坐标
///
private Vector2 _miniMapMidpointCoordinate;
///
/// Mapping of rooms and room preview images
/// 房间和房间预览图的映射
///
private readonly Dictionary _roomToRoomPreviews = [];
///
/// The master node of the map
/// 地图的主人节点
///
public Node2D? OwnerNode { get; set; }
public override void _Ready()
{
_roomPreviewContainer = GetNode("RoomPreviewContainer");
_miniMapMidpointCoordinate = Size / 2;
EventBus.MapGenerationCompleteEvent += MapGenerationCompleteEvent;
EventBus.MapGenerationStartEvent += MapGenerationStartEvent;
}
///
/// Clean up all room preview images in the mini map
/// 清理迷你地图内的所有房间预览图
///
private void Clear()
{
_roomToRoomPreviews.Clear();
if (_roomPreviewContainer != null)
{
NodeUtils.DeleteAllChild(_roomPreviewContainer);
}
}
private void MapGenerationStartEvent(MapGenerationStartEvent mapGenerationStartEvent)
{
Clear();
}
///
/// Display room preview image
/// 显示房间预览图
///
///
public void ShowRoomPreview(Room room)
{
if (_roomToRoomPreviews.TryGetValue(room, out var roomPreview))
{
roomPreview.Show();
}
}
///
/// After the map generator completes placing the room
/// 地图生成器放置房间完成后
///
///
private void MapGenerationCompleteEvent(MapGenerationCompleteEvent mapGenerationCompleteEvent)
{
if (mapGenerationCompleteEvent.RoomDictionary == null)
{
return;
}
foreach (var dictionaryKey in mapGenerationCompleteEvent.RoomDictionary.Keys)
{
var roomDictionaryValue = mapGenerationCompleteEvent.RoomDictionary[dictionaryKey];
var tileMapLayer = roomDictionaryValue.GetTileMapLayer(Config.TileMapLayerName.Ground);
var textureRect = CreateRoomPreview(tileMapLayer,
CalculateRelativePositionOnTheMinimap(roomDictionaryValue));
if (textureRect == null)
{
LogCat.LogErrorWithFormat("failed_to_create_room_preview", LogCat.LogLabel.Default, LogCat.UploadFormat,
dictionaryKey);
}
else
{
_roomToRoomPreviews[roomDictionaryValue] = textureRect;
}
}
}
///
/// CalculateRelativePositionOnTheMinimap
/// 计算在迷你地图上的相对位置
///
///
///Returns the position relative to the point in the minimap container
///返回相对对于迷你地图容器中点的位置
///
private Vector2? CalculateRelativePositionOnTheMinimap(Room room)
{
if (room.RootNode == null)
{
return null;
}
return room.RootNode.Position / Config.CellSize * Config.RoomPreviewScale;
}
///
/// Create a room preview image.
/// 创建房间预览图
///
///
///Layers that need to be drawn onto a minimap
///需要绘制到迷你地图上的图层
///
///
///Relative to the position of the point in the minimap container
///相对于迷你地图容器中点的位置
///
///
private TextureRect? CreateRoomPreview(TileMapLayer? groundTileMapLayer, Vector2? position)
{
if (_roomPreviewContainer == null || position == null)
{
return null;
}
var image = RoomPreview.CreateImage(groundTileMapLayer);
if (image == null)
{
return null;
}
var textureRect = new TextureRect();
textureRect.Scale = new Vector2(Config.RoomPreviewScale, Config.RoomPreviewScale);
textureRect.Texture = image;
textureRect.Position = _miniMapMidpointCoordinate + position.Value;
textureRect.Hide();
NodeUtils.CallDeferredAddChild(_roomPreviewContainer, textureRect);
return textureRect;
}
public override void _Process(double delta)
{
if (_roomPreviewContainer == null)
{
return;
}
if (OwnerNode != null)
{
_roomPreviewContainer.Position = -OwnerNode.GlobalPosition / Config.CellSize * Config.RoomPreviewScale;
}
}
public override void _ExitTree()
{
EventBus.MapGenerationCompleteEvent -= MapGenerationCompleteEvent;
EventBus.MapGenerationStartEvent -= MapGenerationStartEvent;
}
}