Traveller/scripts/map/room/RoomFactory.cs
Cold-Mint 544c5303f3
Fixed an issue where the return path of the room template set was invalid, the initial room placement was extracted separately from the new method and added to the seed of map generation.
修复房间模板集返回路径无效的问题,初始房间的放置被单独提取出新的方法,加入地图生成时的种子。
2024-05-21 22:50:33 +08:00

81 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using System.IO;
using Godot;
using FileAccess = Godot.FileAccess;
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
{
/// <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)
{
//Detects whether it is a folder
//检测是否为文件夹
if (DirAccess.DirExistsAbsolute(roomTemplate))
{
using var dir = DirAccess.Open(roomTemplate);
if (dir != null)
{
dir.ListDirBegin();
var fileName = dir.GetNext();
while (!string.IsNullOrEmpty(fileName))
{
if (!dir.CurrentIsDir())
{
resList.Add(Path.Join(roomTemplate, fileName));
}
fileName = dir.GetNext();
}
}
}
if (FileAccess.FileExists(roomTemplate))
{
resList.Add(roomTemplate);
}
}
return resList.ToArray();
}
/// <summary>
/// <para>CreateRoom</para>
/// <para>创建房间模板</para>
/// </summary>
/// <param name="resPath"></param>
/// <returns></returns>
public static Room? CreateRoom(string resPath)
{
//If the file does not exist, null is returned
//如果文件不存在则返回null
var exists = FileAccess.FileExists(resPath);
if (!exists)
{
return null;
}
var room = new Room
{
RoomScene = GD.Load<PackedScene>(resPath)
};
return room;
}
}