using System.Collections.Generic;
using System.IO;
using ColdMint.scripts.utils;
using Godot;
using FileAccess = Godot.FileAccess;
namespace ColdMint.scripts.map.room;
///
/// The room template factory is used to generate room templates
/// 房间模板工厂用于生成房间模板
///
public static class RoomFactory
{
///
/// A room template sets a path to a room resource
/// 房间模板集转房间资源路径
///
///
///
/// Returned value Checked for the existence of the file.
/// 返回值已检验文件是否存在。
///
public static string[] RoomTemplateSetToRoomRes(string[] roomTemplateSet)
{
var resList = new List();
foreach (var roomTemplate in roomTemplateSet)
{
var roomTemplatePath = ResUtils.GetRunTimeResPath(roomTemplate);
//Detects whether it is a folder
//检测是否为文件夹
if (DirAccess.DirExistsAbsolute(roomTemplatePath))
{
using var dir = DirAccess.Open(roomTemplatePath);
if (dir != null)
{
dir.ListDirBegin();
var fileName = dir.GetNext();
while (!string.IsNullOrEmpty(fileName))
{
if (!dir.CurrentIsDir())
{
resList.Add(Path.Join(roomTemplatePath, fileName));
}
fileName = dir.GetNext();
}
}
}
if (FileAccess.FileExists(roomTemplatePath))
{
resList.Add(roomTemplatePath);
}
}
return resList.ToArray();
}
///
/// CreateRoom
/// 创建房间模板
///
///
///resources path
///资源路径
///
///
///The ID of the event handler when entering the room
///进入房间时的事件处理器ID
///
///
///Event handler ID when exiting the room
///退出房间时的事件处理器ID
///
///
public static Room? CreateRoom(string resPath, string? enterRoomEventHandlerId = null,
string? exitRoomEventHandlerId = null)
{
//If the file does not exist, null is returned
//如果文件不存在,则返回null
var exists = FileAccess.FileExists(resPath);
if (!exists)
{
return null;
}
var room = new Room
{
RoomScene = ResourceLoader.Load(ResUtils.GetEditorResPath(resPath)),
EnterRoomEventHandlerId = enterRoomEventHandlerId,
ExitRoomEventHandlerId = exitRoomEventHandlerId
};
return room;
}
}