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