108 lines
3.3 KiB
C#
108 lines
3.3 KiB
C#
using Mirror;
|
|
using UnityEngine;
|
|
|
|
// 自动搜索目标攻击
|
|
public class AutoTower : Tower
|
|
{
|
|
/// <summary>
|
|
/// 射击目标
|
|
/// </summary>
|
|
#if UNITY_EDITOR
|
|
[DisplayOnly]
|
|
#endif
|
|
public int targetId;
|
|
|
|
public override void OnSpawn(int id, TowerType type, int lvl)
|
|
{
|
|
base.OnSpawn(id, type, lvl);
|
|
mode = TowerMode.Auto;
|
|
originalMode = TowerMode.Auto;
|
|
}
|
|
|
|
public virtual void Update()
|
|
{
|
|
if (!isServer)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ControlChange();
|
|
|
|
if (!IsAlive || state == TowerState.Broken)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// 自动模式
|
|
if (mode == TowerMode.Auto)
|
|
{
|
|
// 搜索目标
|
|
if (targetId == -1)
|
|
{
|
|
targetId = GameManager.Ins.GetRangeEnemyId(transform.position, searchArea);
|
|
}
|
|
|
|
if (targetId != -1)
|
|
{
|
|
GameManager.Ins.EnemyList.TryGetValue(targetId, out Enemy enemy);
|
|
if (enemy != null && Vector3.Distance(enemy.weakness.position, transform.position) <= searchArea)
|
|
{
|
|
float angleY = Vector3.SignedAngle(body.forward, enemy.weakness.position - body.position, body.up);
|
|
float rotateY = angleY > 0 ? rotateSpeed : -rotateSpeed;
|
|
body.localEulerAngles = new Vector3(0, body.localEulerAngles.y + rotateY, 0);
|
|
|
|
float angleX = Vector3.SignedAngle(head.forward, enemy.weakness.position - head.position, head.right);
|
|
float rotateX = angleX > 0 ? rotateSpeed : -rotateSpeed;
|
|
head.localEulerAngles = new Vector3(head.localEulerAngles.x + rotateX, 0, 0);
|
|
}
|
|
else
|
|
{
|
|
targetId = -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 联控模式
|
|
else if (mode == TowerMode.Joint && controllerId != -1)
|
|
{
|
|
Player player = MRNetworkManager.Ins.roomSlots[controllerId].transform.GetComponent<Player>();
|
|
// 看向aim点
|
|
if (player.AimVec != Vector3.zero)
|
|
{
|
|
float angleY = Vector3.SignedAngle(body.forward, player.AimVec - body.position, body.up);
|
|
float rotateY = angleY > 0 ? rotateSpeed : -rotateSpeed;
|
|
body.localEulerAngles = new Vector3(0, body.localEulerAngles.y + rotateY, 0);
|
|
|
|
float angleX = Vector3.SignedAngle(head.forward, player.AimVec - head.position, head.right);
|
|
float rotateX = angleX > 0 ? rotateSpeed : -rotateSpeed;
|
|
head.localEulerAngles = new Vector3(head.localEulerAngles.x + rotateX, 0, 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
[Server]
|
|
public void ControlChange()
|
|
{
|
|
// 死亡
|
|
if ((!IsAlive || state == TowerState.Broken) && useState.nowState != 0)
|
|
{
|
|
ControlChangeRpc(0);
|
|
}
|
|
// 自动
|
|
else if (mode == TowerMode.Auto && useState.nowState != 0)
|
|
{
|
|
ControlChangeRpc(0);
|
|
}
|
|
// 联控
|
|
else if (mode == TowerMode.Joint && useState.nowState != 3)
|
|
{
|
|
ControlChangeRpc(3);
|
|
}
|
|
}
|
|
|
|
[ClientRpc]
|
|
public void ControlChangeRpc(int state)
|
|
{
|
|
useState.StateChange(state);
|
|
}
|
|
} |