mirror of
http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite.git
synced 2025-11-20 11:37:12 +08:00
(client) feat:健康给予,路径优化,结算界面,商店界面 (#60)
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/60
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Tilemaps;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Data;
|
||||
using Managers;
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Tilemaps;
|
||||
|
||||
namespace Map
|
||||
{
|
||||
public class BuildingGeneratorConfig
|
||||
{
|
||||
[JsonProperty("defName")] public string DefName { get; set; } // 图案定义名称
|
||||
[JsonProperty("mapping")] public Dictionary<int, string> Mapping; // 图案值到定义名称的映射
|
||||
[JsonProperty("defName")] public string DefName { get; set; } // 图案定义名称
|
||||
[JsonProperty("pattern")] public int[,] Pattern { get; set; } // 建筑图案
|
||||
[JsonProperty("baseTileDefName")] public string BaseTileDefName { get; set; } // 基底所需瓦片的定义名称
|
||||
[JsonProperty("autoExpand")] public bool AutoExpand { get; set; } // 是否自动扩展搜索区域
|
||||
@@ -29,7 +29,7 @@ namespace Map
|
||||
private string _requiredBaseTileName; // 缓存基底瓦片TileBase的运行时名称
|
||||
|
||||
/// <summary>
|
||||
/// 初始化建筑生成器,解析JSON配置。
|
||||
/// 初始化建筑生成器,解析JSON配置。
|
||||
/// </summary>
|
||||
/// <param name="value">包含BuildingGeneratorConfig的JSON字符串。</param>
|
||||
public override void Init(string value)
|
||||
@@ -53,14 +53,10 @@ namespace Map
|
||||
if (string.IsNullOrEmpty(_config.BaseTileDefName)) return;
|
||||
var baseAsset = TileManager.Instance.GetTile(_config.BaseTileDefName);
|
||||
if (baseAsset != null)
|
||||
{
|
||||
_requiredBaseTileName = baseAsset.name; // 缓存TileBase的运行时名称
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning(
|
||||
$"建筑生成器: 通过TileManager未找到定义'{_config.BaseTileDefName}'的基底瓦片资产。");
|
||||
}
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
@@ -69,30 +65,23 @@ namespace Map
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取建筑生成器将影响的区域大小。
|
||||
/// 获取建筑生成器将影响的区域大小。
|
||||
/// </summary>
|
||||
/// <returns>表示区域宽和高的Vector2Int。</returns>
|
||||
public override Vector2Int GetSize()
|
||||
{
|
||||
if (_config == null)
|
||||
{
|
||||
return Vector2Int.zero;
|
||||
}
|
||||
if (_config == null) return Vector2Int.zero;
|
||||
|
||||
if (_config.Pattern != null)
|
||||
{
|
||||
return new Vector2Int(_config.Pattern.GetLength(1),
|
||||
_config.Pattern.GetLength(0)); // GetLength(1)是宽度,GetLength(0)是高度
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果没有图案,默认为单格建筑
|
||||
return new Vector2Int(1, 1);
|
||||
}
|
||||
|
||||
// 如果没有图案,默认为单格建筑
|
||||
return new Vector2Int(1, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行建筑生成逻辑。
|
||||
/// 执行建筑生成逻辑。
|
||||
/// </summary>
|
||||
/// <param name="landform">MapGenerator实例,提供Tilemap和坐标转换功能。</param>
|
||||
public override async Task Process(Landform landform)
|
||||
@@ -124,7 +113,7 @@ namespace Map
|
||||
{
|
||||
// 如果不满足且允许自动扩展,则搜索新的有效起始位置
|
||||
var foundPos = FindSuitableStartPosition(landform.baseTilemap, actualStartPosition, patternSize,
|
||||
_requiredBaseTileName, searchRadius: 5); // 搜索半径可配置
|
||||
_requiredBaseTileName, 5); // 搜索半径可配置
|
||||
if (foundPos.HasValue)
|
||||
{
|
||||
actualStartPosition = foundPos.Value;
|
||||
@@ -153,29 +142,25 @@ namespace Map
|
||||
if (_config.Pattern != null)
|
||||
{
|
||||
for (var y = 0; y < patternSize.y; y++) // 行 (height)
|
||||
for (var x = 0; x < patternSize.x; x++) // 列 (width)
|
||||
{
|
||||
for (var x = 0; x < patternSize.x; x++) // 列 (width)
|
||||
var patternValue = _config.Pattern[y, x];
|
||||
if (patternValue == 0) // 0通常表示空,不生成
|
||||
continue;
|
||||
|
||||
if (_config.Mapping == null || !_config.Mapping.TryGetValue(patternValue, out var defName))
|
||||
{
|
||||
var patternValue = _config.Pattern[y, x];
|
||||
if (patternValue == 0) // 0通常表示空,不生成
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (_config.Mapping == null || !_config.Mapping.TryGetValue(patternValue, out var defName))
|
||||
{
|
||||
Debug.LogWarning(
|
||||
$"建筑生成器: 图案值{patternValue}在相对位置({x},{y})没有映射。跳过。");
|
||||
continue;
|
||||
}
|
||||
|
||||
// 计算当前建筑单元的世界/网格坐标
|
||||
var currentGridPos =
|
||||
new Vector3Int(actualStartPosition.x + x, actualStartPosition.y + y,
|
||||
0); // 网格坐标与 position(X, Y) 对应
|
||||
var currentWorldPos = landform.GetWorldCoordinates(currentGridPos);
|
||||
await GenerateIndividualBuilding(landform, defName, currentGridPos, currentWorldPos);
|
||||
Debug.LogWarning(
|
||||
$"建筑生成器: 图案值{patternValue}在相对位置({x},{y})没有映射。跳过。");
|
||||
continue;
|
||||
}
|
||||
|
||||
// 计算当前建筑单元的世界/网格坐标
|
||||
var currentGridPos =
|
||||
new Vector3Int(actualStartPosition.x + x, actualStartPosition.y + y,
|
||||
0); // 网格坐标与 position(X, Y) 对应
|
||||
var currentWorldPos = landform.GetWorldCoordinates(currentGridPos);
|
||||
await GenerateIndividualBuilding(landform, defName, currentGridPos, currentWorldPos);
|
||||
}
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(_config.DefName))
|
||||
@@ -192,7 +177,7 @@ namespace Map
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成单个建筑(瓦片或实体)。
|
||||
/// 生成单个建筑(瓦片或实体)。
|
||||
/// </summary>
|
||||
/// <param name="landform">MapGenerator实例。</param>
|
||||
/// <param name="defName">建筑定义名称。</param>
|
||||
@@ -214,15 +199,11 @@ namespace Map
|
||||
{
|
||||
var tileToPlace = TileManager.Instance.GetTile(buildingDef.tile.defName);
|
||||
if (tileToPlace != null)
|
||||
{
|
||||
landform.buildingTilemap.SetTile(gridPos, tileToPlace);
|
||||
// Debug.Log($"建筑生成器: 在网格位置{gridPos}放置了静态建筑'{defName}'('{buildingDef.tile.defName}')。");
|
||||
}
|
||||
// Debug.Log($"建筑生成器: 在网格位置{gridPos}放置了静态建筑'{defName}'('{buildingDef.tile.defName}')。");
|
||||
else
|
||||
{
|
||||
Debug.LogWarning(
|
||||
$"建筑生成器: 未找到静态建筑'{defName}'的瓦片资产('{buildingDef.tile.defName}')。跳过。");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -240,7 +221,7 @@ namespace Map
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查给定区域的基底瓦片条件是否满足。
|
||||
/// 检查给定区域的基底瓦片条件是否满足。
|
||||
/// </summary>
|
||||
/// <param name="baseTilemap">基底瓦片地图。</param>
|
||||
/// <param name="startPos">区域的起始网格位置 (X, Y)。</param>
|
||||
@@ -258,21 +239,19 @@ namespace Map
|
||||
}
|
||||
|
||||
for (var y = 0; y < size.y; y++)
|
||||
for (var x = 0; x < size.x; x++)
|
||||
{
|
||||
for (var x = 0; x < size.x; x++)
|
||||
{
|
||||
var currentCheckPos = new Vector3Int(startPos.x + x, startPos.y + y, 0);
|
||||
var tileBase = baseTilemap.GetTile(currentCheckPos);
|
||||
if (tileBase == null || !tileBase.name.Equals(requiredTileBaseName)) return false; // 任何一个位置不满足则整个区域不满足
|
||||
}
|
||||
var currentCheckPos = new Vector3Int(startPos.x + x, startPos.y + y, 0);
|
||||
var tileBase = baseTilemap.GetTile(currentCheckPos);
|
||||
if (tileBase == null || !tileBase.name.Equals(requiredTileBaseName)) return false; // 任何一个位置不满足则整个区域不满足
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在给定范围内搜索适合放置建筑的起始位置。
|
||||
/// 使用广度优先搜索 (BFS) 算法。
|
||||
/// 在给定范围内搜索适合放置建筑的起始位置。
|
||||
/// 使用广度优先搜索 (BFS) 算法。
|
||||
/// </summary>
|
||||
/// <param name="baseTilemap">基底瓦片地图。</param>
|
||||
/// <param name="originalStartPos">最初指定的起始位置。</param>
|
||||
@@ -292,17 +271,15 @@ namespace Map
|
||||
var currentPos = queue.Dequeue();
|
||||
// 检查当前位置是否满足条件
|
||||
if (CheckBaseTileCondition(baseTilemap, currentPos, patternSize, requiredTileBaseName))
|
||||
{
|
||||
return currentPos;
|
||||
}
|
||||
|
||||
// 探索邻近位置
|
||||
Vector2Int[] directions =
|
||||
{
|
||||
new Vector2Int(0, 1), // 上
|
||||
new Vector2Int(0, -1), // 下
|
||||
new Vector2Int(1, 0), // 右
|
||||
new Vector2Int(-1, 0) // 左
|
||||
new(0, 1), // 上
|
||||
new(0, -1), // 下
|
||||
new(1, 0), // 右
|
||||
new(-1, 0) // 左
|
||||
};
|
||||
foreach (var dir in directions)
|
||||
{
|
||||
@@ -310,9 +287,7 @@ namespace Map
|
||||
// 检查是否在搜索半径内 (曼哈顿距离)
|
||||
if (Mathf.Abs(nextPos.x - originalStartPos.x) + Mathf.Abs(nextPos.y - originalStartPos.y) >
|
||||
searchRadius)
|
||||
{
|
||||
continue; // 超出搜索范围
|
||||
}
|
||||
|
||||
if (!visited.Contains(nextPos))
|
||||
{
|
||||
@@ -325,4 +300,4 @@ namespace Map
|
||||
return null; // 未找到合适位置
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user