using UnityEngine; /// /// 拆家版出兵管理:从城堡前方出兵点生成单位,消耗能量。 /// 替代旧的召唤师身边召唤逻辑。 /// 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; } /// 从指定阵营城堡出兵,消耗能量。teamId: 0=蓝, 1=红。 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(); if (unitTeam != null) { unitTeam.teamId = teamId; unit.tag = teamId == 0 ? "Friend" : "Enemy"; } // 初始化属性(默认 1 级) UnitBase unitBase = unit.GetComponent(); 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); } Debug.Log($"城堡出兵: {data.characterName} teamId={teamId} pos={spawnPoint.position}"); return true; } public bool CanAfford(CharacterData data) { return data != null && data.unitPrefab != null; } }