Files
Zombie/Assets/_Zombie/Scripts/Towers/Tower.cs

226 lines
4.3 KiB
C#

using System.Reflection;
using System;
using System.Collections;
using System.Collections.Generic;
using BehaviorDesigner.Runtime.Tasks.Unity.UnityQuaternion;
using Common;
using DarkTonic.MasterAudio;
using DG.Tweening;
using DragonLi.Core;
using Mirror;
using UnityEngine;
public enum TowerState
{
Normal,
Broken
}
public enum TowerMode
{
Auto,
Manual,
Joint
}
// -- 受伤,修复
public class Tower : Agent
{
public Transform body;
public Transform head;
public Transform broken;
public Transform enter;
public State useState;
/// <summary>
/// 索敌范围
/// </summary>
#if UNITY_EDITOR
[DisplayOnly]
#endif
public TowerType type;
/// <summary>
/// 索敌范围
/// </summary>
#if UNITY_EDITOR
[DisplayOnly]
#endif
public float searchArea = 5.0f;
/// <summary>
/// 转速
/// </summary>
#if UNITY_EDITOR
[DisplayOnly]
#endif
public float rotateSpeed = 0.1f;
/// <summary>
/// 状态
/// </summary>
#if UNITY_EDITOR
[DisplayOnly]
#endif
public TowerState state = TowerState.Normal;
/// <summary>
/// 当前模式
/// </summary>
#if UNITY_EDITOR
[DisplayOnly]
#endif
public TowerMode mode = TowerMode.Auto;
/// <summary>
/// 原始模式
/// </summary>
#if UNITY_EDITOR
[DisplayOnly]
#endif
public TowerMode originalMode = TowerMode.Auto;
/// <summary>
/// 公共冷却
/// </summary>
#if UNITY_EDITOR
[DisplayOnly]
#endif
public float gcd = 0;
/// <summary>
/// 受控id
/// </summary>
#if UNITY_EDITOR
[DisplayOnly]
#endif
public int controllerId;
[SoundGroup] public string enterSound;
[Server]
public virtual void OnSpawn(int id, TowerType type, int lvl)
{
this.type = type;
TowerInfo towerInfo = GameManager.Ins.TowerInfos[type][lvl];
health = towerInfo.Hp;
originHealth = towerInfo.Hp;
searchArea = towerInfo.SearchArea;
rotateSpeed = towerInfo.RotateSpeed;
gcd = towerInfo.GCD;
state = TowerState.Normal;
}
/// <summary>
/// 受击
/// </summary>
public override void ApplyDamage(float value, object info, Transform _sender)
{
if (IsAlive && state == TowerState.Normal)
{
Health -= OnReceiveDamage(value, info, _sender);
if (!IsAlive)
{
Die(info, _sender);
}
}
}
[Server]
public virtual void SetControllerId(int id)
{
controllerId = id;
if (controllerId != -1)
{
EnterTower();
}
else
{
ExitTower();
}
}
[Server]
public virtual void EnterTower()
{
PlaySound(enterSound);
}
[Server]
public virtual void ExitTower() { }
[ClientRpc]
public void PlaySound(string sound)
{
MasterAudio.PlaySound3DAtVector3(sound, head.position);
}
/// <summary>
/// 损坏
/// </summary>
public void Broken()
{
state = TowerState.Broken;
head.localEulerAngles = new Vector3(60, 0, 0);
SetBrokenRPC(true);
}
/// <summary>
/// 修复完毕
/// </summary>
public void RepairOK()
{
state = TowerState.Normal;
head.localEulerAngles = new Vector3(0, 0, 0);
SetBrokenRPC(false);
}
/// <summary>
/// 死亡
/// </summary>
public override void Die(object info, Transform _sender)
{
Debug.Log($"炮塔损坏");
ColliderComponent.enabled = false;
Broken();
// 启动修复
// Repair();
}
[ClientRpc]
public void SetBrokenRPC(bool isBroken)
{
if (broken != null)
{
broken.gameObject.SetActive(isBroken);
}
}
/// <summary>
/// 耗时修复
/// </summary>
public void RepairLate()
{
Debug.Log("开始修复");
DOVirtual.Float(0, originHealth, 30, (res) =>
{
health = res;
if (health == originHealth)
{
Debug.Log("修复完成");
RepairOK();
}
});
}
/// <summary>
/// 瞬间修复
/// </summary>
public void RepairImm()
{
health = originHealth;
ColliderComponent.enabled = true;
RepairOK();
}
}