mirror of
http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite.git
synced 2025-11-20 06:57:12 +08:00
(client) feat:支持定义实体的碰撞体大小和偏移;建筑支持定义实体建筑和瓦片建筑,建筑支持指定按钮回调;添加存档管理器;Dev支持设置是否暂停;实体允许定义事件组;添加基地界面 (#57)
Co-authored-by: m0_75251201 <m0_75251201@noreply.gitcode.com> Reviewed-on: http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite/pulls/57
This commit is contained in:
@@ -21,24 +21,24 @@ namespace Managers
|
||||
private Dictionary<string, RuleTile> _cachedTiles = new();
|
||||
|
||||
// 地图生成器工作类映射,Key: MapGeneratorDef.defName, Value: MapGeneratorWorkClassBase
|
||||
private Dictionary<string,MapGeneratorWorkClassBase> _mapGeneratorWorkClassMap = new();
|
||||
private Dictionary<string, MapGeneratorWorkClassBase> _mapGeneratorWorkClassMap = new();
|
||||
|
||||
public void Init()
|
||||
{
|
||||
// 如果已缓存瓦片,则直接返回
|
||||
if (_cachedTiles.Any())
|
||||
return;
|
||||
|
||||
|
||||
var tileDefs = DefineManager.Instance.QueryDefinesByType<TileDef>();
|
||||
|
||||
// 第一次遍历:创建RuleTile实例,并填充除特定瓦片引用(specificTileName)外的所有简单字段
|
||||
foreach (var def in tileDefs)
|
||||
{
|
||||
if (string.IsNullOrEmpty(def.defName))
|
||||
{
|
||||
Debug.LogWarning($"发现未定义名称或defName的TileDef,已跳过。");
|
||||
Debug.LogWarning($"发现未定义名称或defName的TileDef,已跳过。");
|
||||
continue;
|
||||
}
|
||||
|
||||
var tileName = def.defName;
|
||||
var ruleTile = ScriptableObject.CreateInstance<RuleTile>();
|
||||
ruleTile.name = tileName; // 设置ScriptableObject的名称
|
||||
@@ -46,7 +46,7 @@ namespace Managers
|
||||
ruleTile.m_DefaultColliderType = def.collider;
|
||||
ruleTile.m_DefaultSprite = PackagesImageManager.Instance.GetSprite(def.texture);
|
||||
ruleTile.m_TilingRules = GetTileRules(def.rules);
|
||||
|
||||
|
||||
_cachedTiles[tileName] = ruleTile;
|
||||
}
|
||||
|
||||
@@ -54,11 +54,12 @@ namespace Managers
|
||||
foreach (var mapGeneratorDef in generatorDefs)
|
||||
{
|
||||
var workClass = StringUtils.CreateMapGeneratorInstance(mapGeneratorDef.workClass);
|
||||
if(workClass == null)
|
||||
if (workClass == null)
|
||||
{
|
||||
Debug.LogWarning($"无法为地图生成器 '{mapGeneratorDef.defName}' 创建工作类 '{mapGeneratorDef.workClass}',已跳过。");
|
||||
continue;
|
||||
}
|
||||
|
||||
workClass.Init(mapGeneratorDef.value);
|
||||
_mapGeneratorWorkClassMap[mapGeneratorDef.defName] = workClass;
|
||||
}
|
||||
@@ -70,16 +71,17 @@ namespace Managers
|
||||
{
|
||||
if (tile != null)
|
||||
{
|
||||
Object.Destroy(tile);
|
||||
Object.Destroy(tile);
|
||||
}
|
||||
}
|
||||
|
||||
_cachedTiles.Clear();
|
||||
_mapGeneratorWorkClassMap.Clear(); // 清空地图生成器工作类映射
|
||||
}
|
||||
|
||||
private static List<RuleTile.TilingRule> GetTileRules(RuleTileRuleDef[] rules)
|
||||
{
|
||||
return rules.Select(GetTileRule).ToList();
|
||||
return rules?.Select(GetTileRule).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -89,6 +91,9 @@ namespace Managers
|
||||
/// <returns>转换后的 RuleTile.TilingRule 实例。</returns>
|
||||
private static RuleTile.TilingRule GetTileRule(RuleTileRuleDef ruleDef)
|
||||
{
|
||||
if (ruleDef == null)
|
||||
return null;
|
||||
|
||||
var tilingRule = new RuleTile.TilingRule
|
||||
{
|
||||
m_Sprites = PackagesImageManager.Instance.GetSprites(ruleDef.animationTextures.ToArray()),
|
||||
@@ -100,29 +105,77 @@ namespace Managers
|
||||
m_RuleTransform = ruleDef.transform
|
||||
};
|
||||
|
||||
var tilePositions = new List<Vector3Int>();
|
||||
// 使用 Dictionary 存储邻居条件,键为位置,值为邻居类型
|
||||
var neighborConditionsMap = new Dictionary<Vector3Int, int>();
|
||||
|
||||
// 预设的8个邻居位置
|
||||
var defaultPositions = new Vector3Int[]
|
||||
{
|
||||
new Vector3Int(-1, 1, 0),
|
||||
new Vector3Int(0, 1, 0),
|
||||
new Vector3Int(1, 1, 0),
|
||||
new Vector3Int(-1, 0, 0),
|
||||
new Vector3Int(1, 0, 0),
|
||||
new Vector3Int(-1, -1, 0),
|
||||
new Vector3Int(0, -1, 0),
|
||||
new Vector3Int(1, -1, 0)
|
||||
};
|
||||
|
||||
// 处理标准8个邻居条件
|
||||
for (var i = 0; i < ruleDef.neighborConditions.Length && i < 8; i++)
|
||||
{
|
||||
switch(ruleDef.neighborConditions[i])
|
||||
int neighborType;
|
||||
switch (ruleDef.neighborConditions[i])
|
||||
{
|
||||
case RuleTileRuleDef.NeighborConditionType.NotThis:
|
||||
tilingRule.m_Neighbors.Add(RuleTile.TilingRuleOutput.Neighbor.NotThis);
|
||||
neighborType = RuleTile.TilingRuleOutput.Neighbor.NotThis;
|
||||
neighborConditionsMap[defaultPositions[i]] = neighborType; // 添加或更新
|
||||
break;
|
||||
case RuleTileRuleDef.NeighborConditionType.This:
|
||||
tilingRule.m_Neighbors.Add(RuleTile.TilingRuleOutput.Neighbor.This);
|
||||
neighborType = RuleTile.TilingRuleOutput.Neighbor.This;
|
||||
neighborConditionsMap[defaultPositions[i]] = neighborType; // 添加或更新
|
||||
break;
|
||||
case RuleTileRuleDef.NeighborConditionType.Any:
|
||||
default:
|
||||
// 忽略 Any 类型,因为其不会添加到m_Neighbors中
|
||||
continue;
|
||||
// 如果是Any或默认,则不添加到map中,表示不关心此位置
|
||||
break;
|
||||
}
|
||||
|
||||
tilePositions.Add(tilingRule.m_NeighborPositions[i]);
|
||||
}
|
||||
tilingRule.m_NeighborPositions = tilePositions;
|
||||
|
||||
// 处理扩展邻居条件,如果位置重复,则覆盖标准8个邻居的定义
|
||||
if (ruleDef.neighborConditionExtend != null)
|
||||
{
|
||||
foreach (var neighborConditionDef in ruleDef.neighborConditionExtend)
|
||||
{
|
||||
var pos = Utils.StringUtils.StringToVector3Int(neighborConditionDef.position);
|
||||
int neighborType;
|
||||
switch (neighborConditionDef.Type)
|
||||
{
|
||||
case RuleTileRuleDef.NeighborConditionType.NotThis:
|
||||
neighborType = RuleTile.TilingRuleOutput.Neighbor.NotThis;
|
||||
neighborConditionsMap[pos] = neighborType; // 添加或覆盖
|
||||
break;
|
||||
case RuleTileRuleDef.NeighborConditionType.This:
|
||||
neighborType = RuleTile.TilingRuleOutput.Neighbor.This;
|
||||
neighborConditionsMap[pos] = neighborType; // 添加或覆盖
|
||||
break;
|
||||
case RuleTileRuleDef.NeighborConditionType.Any:
|
||||
default:
|
||||
// 如果是Any或默认,则不添加到map中
|
||||
// 如果这个位置之前有定义(来自defaultPositions),那么Any会有效地“移除”这个定义
|
||||
// 如果我们希望Any清除之前的显式定义,可以在这里neighborConditionsMap.Remove(pos);
|
||||
// 但通常Any意味着不关心,所以保持不添加即可
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
tilingRule.m_NeighborPositions = new List<Vector3Int>(neighborConditionsMap.Keys);
|
||||
tilingRule.m_Neighbors = new List<int>(neighborConditionsMap.Values);
|
||||
|
||||
return tilingRule;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 根据名称获取一个 RuleTile 实例。
|
||||
/// </summary>
|
||||
@@ -134,6 +187,7 @@ namespace Managers
|
||||
{
|
||||
return tile;
|
||||
}
|
||||
|
||||
Debug.LogWarning($"瓦片 '{tileName}' 未在TileManager缓存中找到。");
|
||||
return null;
|
||||
}
|
||||
@@ -142,36 +196,41 @@ namespace Managers
|
||||
/// 应用指定的地图生成器,现在是非阻塞的异步操作。
|
||||
/// </summary>
|
||||
/// <param name="generatorName">要应用的生成器名称。</param>
|
||||
/// <param name="mapGenerator">可选:指定要使用的 MapGenerator 实例。如果为 null,将尝试从 Program.Instance.FocusedDimension 获取。</param>
|
||||
/// <param name="landform">可选:指定要使用的 MapGenerator 实例。如果为 null,将尝试从 Program.Instance.FocusedDimension 获取。</param>
|
||||
/// <returns>一个表示异步操作完成的 Task。</returns>
|
||||
public async Task ApplyMapGenerator(string generatorName, MapGenerator mapGenerator = null) // <-- 返回 Task,并标记为 async
|
||||
public async Task ApplyMapGenerator(string generatorName, Map.Landform landform = null)
|
||||
{
|
||||
if (mapGenerator == null)
|
||||
if (landform == null)
|
||||
{
|
||||
// 尝试从 Program.Instance.FocusedDimension 获取 mapGenerator
|
||||
// 假设 Program 和 FocusedDimension 结构存在
|
||||
mapGenerator = Program.Instance?.FocusedDimension?.mapGenerator;
|
||||
if (mapGenerator == null)
|
||||
landform = Program.Instance?.FocusedDimension?.landform;
|
||||
if (landform == null)
|
||||
{
|
||||
Debug.LogError($"ApplyMapGenerator: 无法找到地图生成器 '{generatorName}' 对应的 mapGenerator。Program.Instance、FocusedDimension 或其 mapGenerator 可能为空。");
|
||||
Debug.LogError(
|
||||
$"ApplyMapGenerator: 无法找到地图生成器 '{generatorName}' 对应的 mapGenerator。Program.Instance、FocusedDimension 或其 mapGenerator 可能为空。");
|
||||
return; // async Task 方法直接 return; 意味着返回 Task.CompletedTask
|
||||
}
|
||||
}
|
||||
|
||||
if (_mapGeneratorWorkClassMap == null)
|
||||
{
|
||||
Debug.LogError($"ApplyMapGenerator: _mapGeneratorWorkClassMap 未初始化,无法找到生成器 '{generatorName}'。");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_mapGeneratorWorkClassMap.TryGetValue(generatorName, out var mapGeneratorWorkClass))
|
||||
{
|
||||
if (mapGeneratorWorkClass == null)
|
||||
{
|
||||
Debug.LogError($"ApplyMapGenerator: 为生成器 '{generatorName}' 找到了一个空的 IMapGeneratorWorkClass 实例。这表明 _mapGeneratorWorkClassMap 配置存在问题。");
|
||||
Debug.LogError(
|
||||
$"ApplyMapGenerator: 为生成器 '{generatorName}' 找到了一个空的 IMapGeneratorWorkClass 实例。这表明 _mapGeneratorWorkClassMap 配置存在问题。");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await mapGeneratorWorkClass.Process(mapGenerator); // <-- 使用 await 调用异步 Process 方法
|
||||
await mapGeneratorWorkClass.Process(landform); // <-- 使用 await 调用异步 Process 方法
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -182,8 +241,14 @@ namespace Managers
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"ApplyMapGenerator: 未找到名为 '{generatorName}' 的 IMapGeneratorWorkClass。请确保该生成器已在 _mapGeneratorWorkClassMap 中注册。");
|
||||
Debug.LogError(
|
||||
$"ApplyMapGenerator: 未找到名为 '{generatorName}' 的 IMapGeneratorWorkClass。请确保该生成器已在 _mapGeneratorWorkClassMap 中注册。");
|
||||
}
|
||||
}
|
||||
|
||||
public MapGeneratorWorkClassBase GetMapGeneratorWorkClass(string defName)
|
||||
{
|
||||
return _mapGeneratorWorkClassMap?.GetValueOrDefault(defName,null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user