feat(player): 添加玩家阵型保存功能 新增savedFormation字段用于存储玩家的阵型配置, 包括角色ID列表和对应的格子索引位置信息。 ```
76 lines
2.5 KiB
C#
76 lines
2.5 KiB
C#
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 拆家版出兵管理:从城堡前方出兵点生成单位,消耗能量。
|
|
/// 替代旧的召唤师身边召唤逻辑。
|
|
/// </summary>
|
|
public class CastleSpawnManager : MonoBehaviour
|
|
{
|
|
public static CastleSpawnManager Instance { get; private set; }
|
|
|
|
[Header("出兵点(城堡前方)")]
|
|
public Transform blueSpawnPoint;
|
|
public Transform redSpawnPoint;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance != null && Instance != this) { Destroy(gameObject); return; }
|
|
Instance = this;
|
|
}
|
|
|
|
/// <summary>从指定阵营城堡出兵,消耗能量。teamId: 0=蓝, 1=红。</summary>
|
|
public bool TrySpawn(CharacterData data, int teamId)
|
|
{
|
|
if (data == null || data.unitPrefab == null) return false;
|
|
Transform spawnPoint = teamId == 0 ? blueSpawnPoint : redSpawnPoint;
|
|
if (spawnPoint == null)
|
|
{
|
|
Debug.LogError($"找不到 teamId={teamId} 的出兵点");
|
|
return false;
|
|
}
|
|
|
|
Vector3 direction = spawnPoint.forward;
|
|
direction.y = 0f;
|
|
if (direction.sqrMagnitude < 0.001f) direction = Vector3.forward;
|
|
direction.Normalize();
|
|
|
|
GameObject unit = Instantiate(
|
|
data.unitPrefab,
|
|
spawnPoint.position,
|
|
Quaternion.LookRotation(direction, Vector3.up));
|
|
|
|
// 设置阵营
|
|
UnitTeam unitTeam = unit.GetComponent<UnitTeam>();
|
|
if (unitTeam != null)
|
|
{
|
|
unitTeam.teamId = teamId;
|
|
unit.tag = teamId == 0 ? "Friend" : "Enemy";
|
|
}
|
|
|
|
// 初始化属性(默认 1 级)
|
|
UnitBase unitBase = unit.GetComponent<UnitBase>();
|
|
if (unitBase != null)
|
|
{
|
|
PlayerCharacterData playerData = new PlayerCharacterData("castle_spawn", 1, 0);
|
|
float hp = playerData.GetMaxHealth(data);
|
|
float atk = playerData.GetAttackPower(data);
|
|
float spd = playerData.GetMoveSpeed(data);
|
|
unitBase.InitializeFromPlayerData(hp, atk, spd);
|
|
|
|
// 如果是敌人(teamId != 0),增大寻敌距离
|
|
if (teamId != 0)
|
|
{
|
|
unitBase.detectionRange = 100f; // 从默认30增加到100
|
|
}
|
|
}
|
|
|
|
Debug.Log($"城堡出兵: {data.characterName} teamId={teamId} pos={spawnPoint.position}");
|
|
return true;
|
|
}
|
|
|
|
public bool CanAfford(CharacterData data)
|
|
{
|
|
return data != null && data.unitPrefab != null;
|
|
}
|
|
}
|