48 lines
1.3 KiB
C#
48 lines
1.3 KiB
C#
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 单位阵营标记:用阵营 ID 替代 Unity Tag 判断敌我关系。
|
|
/// 支持 PVP:客户端只需设置 GameManager.LocalPlayerTeamId,所有单位的敌我判断自动正确。
|
|
/// </summary>
|
|
public class UnitTeam : MonoBehaviour
|
|
{
|
|
[Header("阵营 ID")]
|
|
[Tooltip("0 = 玩家1阵营,1 = 玩家2阵营,2+ = 其他阵营")]
|
|
public int teamId = 0;
|
|
|
|
/// <summary>
|
|
/// 判断目标是否为敌人(不同阵营)
|
|
/// </summary>
|
|
public bool IsEnemy(GameObject other)
|
|
{
|
|
if (other == null) return false;
|
|
|
|
var otherTeam = other.GetComponent<UnitTeam>();
|
|
if (otherTeam == null) return false;
|
|
|
|
return otherTeam.teamId != this.teamId;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断目标是否为友军(相同阵营)
|
|
/// </summary>
|
|
public bool IsFriendly(GameObject other)
|
|
{
|
|
if (other == null) return false;
|
|
|
|
var otherTeam = other.GetComponent<UnitTeam>();
|
|
if (otherTeam == null) return false;
|
|
|
|
return otherTeam.teamId == this.teamId;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断是否属于本地玩家阵营
|
|
/// </summary>
|
|
public bool IsLocalPlayerTeam()
|
|
{
|
|
if (GameManager.Instance == null) return teamId == 0;
|
|
return teamId == GameManager.Instance.LocalPlayerTeamId;
|
|
}
|
|
}
|