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

84 lines
2.8 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoldierStateShoot : BaseState<SoldierUnit>
{
private TimerUnit mShootTimer;
private TimerUnit mActiveTimer;
private float mAtkAniLength = 1;
private GameObject mArrowPrefab;
public override void EnterState(Type pLastState)
{
base.EnterState(pLastState);
if (mShootTimer == null)
{
mShootTimer = TimerManager.Instance.CreateTimerUnit();
}
if (mActiveTimer == null)
{
mActiveTimer = TimerManager.Instance.CreateTimerUnit();
}
if (mArrowPrefab == null)
{
mArrowPrefab = ResourceManager.Instance.LoadRes<GameObject>(Const.Path.GetBullet(mMaster.BType, mMaster.Level));
}
mAtkAniLength = Utils.GetAtkAniLength(mMaster.BType, mMaster.Level);
ShootOnce();
}
public override void ExecuteState(float pDeltaTime)
{
base.ExecuteState(pDeltaTime);
}
public override void ExitState(Type pNextState)
{
base.ExitState(pNextState);
mActiveTimer.CancelTimer();
mShootTimer.CancelTimer();
}
private void ShootOnce()
{
mMaster.ShootTarget = mMaster.AgainstGroup.GetNearestTarget(mMaster.transform.position, mMaster.AtkDistance);
if (mMaster.ShootTarget != null && !mMaster.ShootTarget.IsDead)
{
mMaster.LookToTarget(mMaster.ShootTarget.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));
mShootTimer.StartTimer(ShootOnce, Mathf.Min(mAtkAniLength, mMaster.AtkInterval));
}
else
{
ChangeState(typeof(SoldierStateIdle));
}
}
private void ActiveOnce()
{
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);
}
Arrow tArrow = GameObject.Instantiate(mArrowPrefab).GetComponent<Arrow>();
Vector3 tInitPos = mMaster.AtkTrans ? mMaster.AtkTrans.position : mMaster.RelativePos() + Vector3.up;
Vector3 tTargetPos = mMaster.ShootTarget.HitTrans ? mMaster.ShootTarget.HitTrans.position : mMaster.ShootTarget.RelativePos() + Vector3.up;
tArrow.Init(Utils.CountDamage(mMaster.Level, mMaster.ShootTarget.Level, mMaster.Damage), tInitPos, tTargetPos, mMaster.ShootTarget);
}
}