| 
									
										
										
										
											2022-05-23 13:39:59 +00:00
										 |  |  |  | using System; | 
					
						
							|  |  |  |  | using System.Collections; | 
					
						
							|  |  |  |  | using System.Collections.Generic; | 
					
						
							|  |  |  |  | using UnityEngine; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | public class SoldierUnit : WarUnit | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     public bool HasAttackableTarget => AttackTarget.Count > 0 && AttackTarget[0] != null; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     [Header("State")] | 
					
						
							|  |  |  |  |     public string CurrentState; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     [Header("Targets")] | 
					
						
							|  |  |  |  |     public WarUnit MoveTarget; | 
					
						
							|  |  |  |  |     public WarUnit ShootTarget; | 
					
						
							|  |  |  |  |     public List<WarUnit> AttackTarget; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     private Rigidbody mBody; | 
					
						
							|  |  |  |  |     private CapsuleCollider mCollider; | 
					
						
							|  |  |  |  |     private FSM<SoldierUnit> mFSM; | 
					
						
							|  |  |  |  |     private AvatarGroup mAvatar; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     private bool mActive = false; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     private Vector3 mMovePos = Vector3.zero; | 
					
						
							|  |  |  |  |     [SerializeField]private Vector3 mVelocity = Vector3.zero; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     private HudHp mHpBar; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     protected override void AwakeInit() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         AttackTarget = new List<WarUnit>(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         mBody = GetComponent<Rigidbody>(); | 
					
						
							|  |  |  |  |         mCollider = GetComponent<CapsuleCollider>(); | 
					
						
							|  |  |  |  |         mFSM = new FSM<SoldierUnit>(this); | 
					
						
							|  |  |  |  |         mAvatar = gameObject.AddComponent<AvatarGroup>(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         Freeze(); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     private void FixedUpdate() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         if (!mActive) | 
					
						
							|  |  |  |  |             return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if (mFSM != null) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             mFSM.Execute(Time.fixedDeltaTime); | 
					
						
							|  |  |  |  |             CurrentState = mFSM.CurrentState; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if (mBody != null) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             mBody.velocity = mVelocity; | 
					
						
							|  |  |  |  |             mVelocity = Vector3.zero; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-04 11:17:39 +00:00
										 |  |  |  |     public void SetScale(float pScale) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         mTrans.localScale = Vector3.one * pScale; | 
					
						
							|  |  |  |  |         CapsuleCollider tCol = GetComponent<CapsuleCollider>(); | 
					
						
							|  |  |  |  |         tCol.radius *= pScale; | 
					
						
							|  |  |  |  |         tCol.height *= pScale; | 
					
						
							|  |  |  |  |         tCol.center = new Vector3(0, tCol.height * 0.5f, 0); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-23 13:39:59 +00:00
										 |  |  |  |     public void Active() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         mActive = true; | 
					
						
							|  |  |  |  |         mBody.isKinematic = false; | 
					
						
							|  |  |  |  |         mCollider.enabled = true; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         mFSM.ChangeState(typeof(SoldierStateRun)); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         mHpBar = UIManager.Instance.GetUI<PanelGame>().CreateHudHp(mTrans, Utils.GetHpHOffset(BType, Level), Group == GroupType.Enemy, Utils.GetHpScale(BType, Level)); | 
					
						
							|  |  |  |  |         mHpBar.gameObject.SetActive(false); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     public void Freeze() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         mActive = false; | 
					
						
							|  |  |  |  |         mBody.isKinematic = true; | 
					
						
							|  |  |  |  |         mCollider.enabled = false; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     public void Win() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         mFSM.ChangeState(typeof(SoldierStateWin)); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     public void SetACTrigger(SoldierACTrigger pTrigger, float pSpeed = 1) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         mAvatar.SetACTrigger(pTrigger, pSpeed); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     public void LookToTarget(Vector3 pTargetPos) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         pTargetPos.y = mTrans.position.y; | 
					
						
							|  |  |  |  |         mTrans.LookAt(pTargetPos); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     public void MoveToTarget(float pDeltaTime) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         if (MoveTarget == null || MoveTarget.IsDead) | 
					
						
							|  |  |  |  |             return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         Vector3 tMyPos = RelativePos(); | 
					
						
							|  |  |  |  |         mMovePos = MoveTarget.RelativePos(tMyPos); | 
					
						
							|  |  |  |  |         MoveToPos(pDeltaTime); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     public void MoveToPos(float pDeltaTime) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         Vector3 tMyPos = RelativePos(); | 
					
						
							|  |  |  |  |         LookToTarget(mMovePos); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         Vector3 tDir = mMovePos - tMyPos; | 
					
						
							|  |  |  |  |         tDir.y = 0; | 
					
						
							|  |  |  |  |         mVelocity = tDir.normalized * MoveSpeed * pDeltaTime; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     public void StopMoving() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         mVelocity = Vector3.zero; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     protected override void HandleHurt(int pDamage) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         mHpBar.gameObject.SetActive(true); | 
					
						
							|  |  |  |  |         mHpBar.RefreshHp((float)mCurHp / MaxHP); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         VibrateManager.Instance.Vibrate(VibrateType.Light); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     protected override void HandleDie() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         Freeze(); | 
					
						
							|  |  |  |  |         mFSM.ChangeState(typeof(SoldierStateDead)); | 
					
						
							|  |  |  |  |         mHpBar.gameObject.SetActive(false); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         Utils.DelayHideGameObject(gameObject, 3); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     private void OnCollisionEnter(Collision pCollision) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         GameObject tHitOb = pCollision.gameObject; | 
					
						
							|  |  |  |  |         WarUnit tUnit = tHitOb.GetComponent<WarUnit>(); | 
					
						
							|  |  |  |  |          | 
					
						
							|  |  |  |  |         if (tUnit != null && Group != tUnit.Group && !AttackTarget.Contains(tUnit)) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             AttackTarget.Add(tUnit); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     private void OnCollisionExit(Collision pCollision) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         GameObject tHitOb = pCollision.gameObject; | 
					
						
							|  |  |  |  |         WarUnit tUnit = tHitOb.GetComponent<WarUnit>(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if (tUnit != null && Group != tUnit.Group && AttackTarget.Contains(tUnit)) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             AttackTarget.Remove(tUnit); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | } |