mergemilitary/Assets/Scripts/Game/Wars/Soldiers/SoldierStates/SoldierStateAttack.cs

125 lines
3.7 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoldierStateAttack : BaseState<SoldierUnit>
{
private TimerUnit mAttackTimer = null;
private TimerUnit mActiveTimer = null;
private float mAtkAniLength = 1;
public override void EnterState(Type pLastState)
{
base.EnterState(pLastState);
if (mAttackTimer == null)
{
mAttackTimer = TimerManager.Instance.CreateTimerUnit();
}
if (mActiveTimer == null)
{
mActiveTimer = TimerManager.Instance.CreateTimerUnit();
}
mAtkAniLength = Utils.GetAtkAniLength(mMaster.BType, mMaster.Level);
AttackOnce();
}
public override void ExecuteState(float pDeltaTime)
{
base.ExecuteState(pDeltaTime);
}
public override void ExitState(Type pNextState)
{
base.ExitState(pNextState);
mAttackTimer.CancelTimer();
mActiveTimer.CancelTimer();
}
private void AttackOnce()
{
ClearDead();
if (mMaster.HasAttackableTarget)
{
mMaster.LookToTarget(mMaster.AttackTarget[0].RelativePos(mMaster.RelativePos()));
mMaster.SetACTrigger(SoldierACTrigger.TriggerAttack, Mathf.Max(mAtkAniLength / mMaster.AtkInterval, mAtkAniLength));
mActiveTimer.StartTimer(ActiveOnce, Mathf.Min(mAtkAniLength * 0.6f, mMaster.AtkInterval * 0.6f));
mAttackTimer.StartTimer(AttackOnce, Mathf.Min(mAtkAniLength, mMaster.AtkInterval));
}
else
{
ChangeState(typeof(SoldierStateIdle));
}
}
private void ClearDead()
{
if (mMaster.AttackTarget.Count <= 0)
return;
List<WarUnit> tRemoveList = new List<WarUnit>();
WarUnit tTarget = null;
for (int i = 0; i < mMaster.AttackTarget.Count; i++)
{
tTarget = mMaster.AttackTarget[i];
if (tTarget.IsDead)
{
tRemoveList.Add(tTarget);
}
}
for (int i = 0; i < tRemoveList.Count; i++)
{
if (mMaster.AttackTarget.Contains(tRemoveList[i]))
mMaster.AttackTarget.Remove(tRemoveList[i]);
}
}
private void ActiveOnce()
{
if (mMaster.HasAttackableTarget)
{
List<WarUnit> tRemoveList = new List<WarUnit>();
WarUnit tTarget = null;
for (int i = 0; i < mMaster.AttackTarget.Count; i++)
{
if (mMaster.AtkArea == 0 && i > 0)
break;
tTarget = mMaster.AttackTarget[i];
if (!tTarget.IsDead)
{
tTarget.Hurt(Utils.CountDamage(mMaster.Level, tTarget.Level ,mMaster.Damage));
}
else
{
tRemoveList.Add(tTarget);
}
}
for (int i = 0; i < tRemoveList.Count; i++)
{
if(mMaster.AttackTarget.Contains(tRemoveList[i]))
mMaster.AttackTarget.Remove(tRemoveList[i]);
}
GameObject tAtkFx = ResourceManager.Instance.LoadRes<GameObject>(Const.Path.GetAtkFx(mMaster.BType, mMaster.Level));
if (tAtkFx != null)
{
Transform tFxTrans = mMaster.AtkTrans ? mMaster.AtkTrans : mMaster.transform;
GameObject.Instantiate(tAtkFx, tFxTrans.position, tFxTrans.rotation);
}
AudioClip tAC = ResourceManager.Instance.LoadRes<AudioClip>(Const.Path.GetAtkSound(mMaster.BType, mMaster.Level));
AudioManager.Instance.PlaySound(tAC, 0.6f);
}
}
}