``` docs(readme): 更新文档说明 - 添加项目使用指南 - 完善API接口说明 - 修正错误的配置示例 ``` 注意:由于您提供的code differences部分为空,以上是一个示例模板。当您有具体的代码变更时,请提供详细的变更内容以便生成更准确的commit message。
118 lines
3.4 KiB
C#
118 lines
3.4 KiB
C#
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 混沌之门:被占领后按固定间隔从所属阵营卡组随机出兵。
|
|
/// </summary>
|
|
[RequireComponent(typeof(CrystalMineCapture))]
|
|
public class ChaosGateSpawner : MonoBehaviour
|
|
{
|
|
[Header("出兵配置")]
|
|
[Min(1f)] public float spawnInterval = 12f;
|
|
[Min(0f)] public float spawnRadius = 1.5f;
|
|
public bool spawnImmediatelyOnCapture = false;
|
|
|
|
[Header("蓝方卡组")]
|
|
[SerializeField] private string[] blueDeckDataNames =
|
|
{
|
|
"人类士兵Data", "弓箭手Data", "盾卫Data", "精锐士兵Data",
|
|
"法师Data", "狂暴狼人Data", "石巨人Data", "弓箭猎人Data"
|
|
};
|
|
|
|
private CrystalMineCapture capture;
|
|
private UnitTeam unitTeam;
|
|
private float spawnTimer;
|
|
private int previousTeamId = 2;
|
|
|
|
private void Awake()
|
|
{
|
|
capture = GetComponent<CrystalMineCapture>();
|
|
unitTeam = GetComponent<UnitTeam>();
|
|
spawnTimer = spawnInterval;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (unitTeam == null)
|
|
{
|
|
unitTeam = GetComponent<UnitTeam>();
|
|
if (unitTeam == null) return;
|
|
}
|
|
|
|
int currentTeamId = unitTeam.teamId;
|
|
if (currentTeamId != previousTeamId)
|
|
{
|
|
previousTeamId = currentTeamId;
|
|
spawnTimer = spawnImmediatelyOnCapture ? 0f : spawnInterval;
|
|
}
|
|
|
|
if (currentTeamId != 0 && currentTeamId != 1)
|
|
{
|
|
spawnTimer = spawnInterval;
|
|
return;
|
|
}
|
|
|
|
spawnTimer -= Time.deltaTime;
|
|
if (spawnTimer > 0f) return;
|
|
|
|
if (TrySpawn(currentTeamId))
|
|
spawnTimer = spawnInterval;
|
|
else
|
|
spawnTimer = 1f;
|
|
}
|
|
|
|
private bool TrySpawn(int teamId)
|
|
{
|
|
Vector3 position = transform.position;
|
|
position.x += Random.Range(-spawnRadius, spawnRadius);
|
|
position.z += Random.Range(-spawnRadius, spawnRadius);
|
|
|
|
if (teamId == 1)
|
|
{
|
|
EnemyCastleAI enemyAI = FindObjectOfType<EnemyCastleAI>();
|
|
return enemyAI != null && enemyAI.TrySpawnFromPortal(position, transform.rotation);
|
|
}
|
|
|
|
CharacterData card = ChooseBlueCard();
|
|
if (card == null || CastleSpawnManager.Instance == null)
|
|
return false;
|
|
|
|
return SpawnBlueUnit(card, position, transform.rotation);
|
|
}
|
|
|
|
private CharacterData ChooseBlueCard()
|
|
{
|
|
CharacterData[] cards = new CharacterData[blueDeckDataNames.Length];
|
|
int validCount = 0;
|
|
foreach (string dataName in blueDeckDataNames)
|
|
{
|
|
CharacterData data = Resources.Load<CharacterData>("Data/" + dataName);
|
|
if (data != null && data.unitPrefab != null)
|
|
cards[validCount++] = data;
|
|
}
|
|
|
|
if (validCount == 0) return null;
|
|
|
|
return cards[Random.Range(0, validCount)];
|
|
}
|
|
|
|
private bool SpawnBlueUnit(CharacterData data, Vector3 position, Quaternion rotation)
|
|
{
|
|
GameObject unit = Instantiate(data.unitPrefab, position, rotation);
|
|
UnitTeam team = unit.GetComponent<UnitTeam>();
|
|
if (team != null)
|
|
{
|
|
team.teamId = 0;
|
|
unit.tag = "Friend";
|
|
}
|
|
|
|
UnitBase unitBase = unit.GetComponent<UnitBase>();
|
|
if (unitBase != null)
|
|
{
|
|
unitBase.InitializeFromPlayerData(data.GetMaxHealth(1), data.GetAttackPower(1), data.GetMoveSpeed(1));
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
}
|