using ColdMint.scripts.debug;
using ColdMint.scripts.map.dateBean;
using ColdMint.scripts.map.events;
using ColdMint.scripts.map.preview;
using ColdMint.scripts.utils;
using Godot;
namespace ColdMint.scripts.map.miniMap;
///
/// Mini Map
/// 迷你地图
///
public partial class MiniMap : NinePatchRect
{
private Node2D? _roomPreviewContainer;
private Vector2 _miniMapMidpointCoordinate;
///
/// The midpoint coordinates of the mini map
/// 迷你地图的中点坐标
///
public Vector2 MiniMapMidpointCoordinate => _miniMapMidpointCoordinate;
///
/// The master node of the map
/// 地图的主人节点
///
public Node2D? OwnerNode { get; set; }
public override void _Ready()
{
_roomPreviewContainer = GetNode("RoomPreviewContainer");
_miniMapMidpointCoordinate = Size / 2;
EventBus.MapGenerationPlaceRoomFinishEvent += MapGenerationPlaceRoomFinishEvent;
}
///
/// After the map generator completes placing the room
/// 地图生成器放置房间完成后
///
///
private void MapGenerationPlaceRoomFinishEvent(MapGenerationPlaceRoomFinishEvent mapGenerationPlaceRoomFinishEvent)
{
var roomPlacementData = mapGenerationPlaceRoomFinishEvent.RoomPlacementData;
if (roomPlacementData?.NewRoom == null || mapGenerationPlaceRoomFinishEvent.RoomNodeDataId == null)
{
return;
}
var tileMapLayer = roomPlacementData.NewRoom.GetTileMapLayer(Config.TileMapLayerName.Ground);
if (!CreateRoomPreview(tileMapLayer,
CalculateRelativePositionOnTheMinimap(tileMapLayer, roomPlacementData)))
{
LogCat.LogWithFormat("failed_to_create_room_preview", LogCat.LogLabel.Default, LogCat.UploadFormat,
mapGenerationPlaceRoomFinishEvent.RoomNodeDataId);
}
}
///
/// CalculateRelativePositionOnTheMinimap
/// 计算在迷你地图上的相对位置
///
///
///Returns the position relative to the point in the minimap container
///返回相对对于迷你地图容器中点的位置
///
private Vector2? CalculateRelativePositionOnTheMinimap(TileMapLayer? groundTileMapLayer,
RoomPlacementData roomPlacementData)
{
if (groundTileMapLayer == null || roomPlacementData.Position == null)
{
return null;
}
return roomPlacementData.Position.Value / 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 bool CreateRoomPreview(TileMapLayer? groundTileMapLayer, Vector2? position)
{
if (GameSceneDepend.MiniMapContainerNode == null || position == null)
{
return false;
}
var image = RoomPreview.CreateImage(groundTileMapLayer);
if (image == null)
{
return false;
}
var sprite = new TextureRect();
sprite.Scale = new Vector2(Config.RoomPreviewScale, Config.RoomPreviewScale);
sprite.Texture = image;
if (GameSceneDepend.MiniMap != null)
{
sprite.Position = GameSceneDepend.MiniMap.MiniMapMidpointCoordinate + position.Value;
}
NodeUtils.CallDeferredAddChild(GameSceneDepend.MiniMapContainerNode, sprite);
return true;
}
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.MapGenerationPlaceRoomFinishEvent -= MapGenerationPlaceRoomFinishEvent;
}
}