diff --git a/l2-unity/Assets/Scripts/Combat/TargetManager.cs b/l2-unity/Assets/Scripts/Combat/TargetManager.cs index 6fbbf2a6d..aad8a469b 100644 --- a/l2-unity/Assets/Scripts/Combat/TargetManager.cs +++ b/l2-unity/Assets/Scripts/Combat/TargetManager.cs @@ -11,25 +11,33 @@ public class TargetManager : MonoBehaviour private static TargetManager _instance; public static TargetManager Instance { get { return _instance; } } - private void Awake() { - if (_instance == null) { + private void Awake() + { + if (_instance == null) + { _instance = this; - } else { + } + else + { Destroy(this); } } - void OnDestroy() { + void OnDestroy() + { _instance = null; } - private void Start() { + private void Start() + { _target = null; _attackTarget = null; } - public void SetTarget(ObjectData target) { - if(target == null) { + public void SetTarget(ObjectData target) + { + if (target == null) + { ClearTarget(); return; } @@ -41,25 +49,37 @@ public void SetTarget(ObjectData target) { GameClient.Instance.ClientPacketHandler.SendRequestSetTarget(_target.Identity.Id); } - public void SetAttackTarget() { + public void SetAttackTarget() + { _attackTarget = _target; } - public void ClearAttackTarget() { + public void ClearAttackTarget() + { _attackTarget = null; } - public bool HasTarget() { + public bool IsAttackTargetSet() + { + return _target != null && _attackTarget != null && _target == _attackTarget; + } + + public bool HasTarget() + { return _target != null && _target.Data.ObjectTransform != null; } - public bool HasAttackTarget() { + public bool HasAttackTarget() + { return _attackTarget != null; } - public void ClearTarget() { - if (HasTarget()) { - if(PlayerEntity.Instance.TargetId != -1) { + public void ClearTarget() + { + if (HasTarget()) + { + if (PlayerEntity.Instance.TargetId != -1) + { GameClient.Instance.ClientPacketHandler.SendRequestSetTarget(-1); PlayerEntity.Instance.TargetId = -1; PlayerEntity.Instance.Target = null; @@ -70,16 +90,21 @@ public void ClearTarget() { } } - void Update() { - if (PlayerEntity.Instance == null) { + void Update() + { + if (PlayerEntity.Instance == null) + { return; } - if(HasTarget()) { + if (HasTarget()) + { _target.Distance = Vector3.Distance( - PlayerController.Instance.transform.position, + PlayerController.Instance.transform.position, _target.Data.ObjectTransform.position); - } else { + } + else + { ClearTarget(); } } diff --git a/l2-unity/Assets/Scripts/Controller/StateMachine/Intention/AttackIntention.cs b/l2-unity/Assets/Scripts/Controller/StateMachine/Intention/AttackIntention.cs index 3a2e31e0d..1bae494cb 100644 --- a/l2-unity/Assets/Scripts/Controller/StateMachine/Intention/AttackIntention.cs +++ b/l2-unity/Assets/Scripts/Controller/StateMachine/Intention/AttackIntention.cs @@ -11,10 +11,25 @@ public override void Enter(object arg0) if (target == null) { Debug.Log("Target is null, CANCEL event sent"); - _stateMachine.NotifyEvent(Event.CANCEL); + // _stateMachine.NotifyEvent(Event.CANCEL); return; } + if (_stateMachine.State == PlayerState.ATTACKING) + { + if (TargetManager.Instance.IsAttackTargetSet()) + { + // Already attacking target + return; + } + else + { + //Stop attacking current target + _stateMachine.ChangeIntention(Intention.INTENTION_IDLE); + return; + } + } + TargetManager.Instance.SetAttackTarget(); float attackRange = ((PlayerStats)PlayerEntity.Instance.Stats).AttackRange; diff --git a/l2-unity/Assets/Scripts/Controller/StateMachine/Intention/IdleIntention.cs b/l2-unity/Assets/Scripts/Controller/StateMachine/Intention/IdleIntention.cs index a3bb12566..267549385 100644 --- a/l2-unity/Assets/Scripts/Controller/StateMachine/Intention/IdleIntention.cs +++ b/l2-unity/Assets/Scripts/Controller/StateMachine/Intention/IdleIntention.cs @@ -1,15 +1,29 @@ +using UnityEngine; + public class IdleIntention : IntentionBase { public IdleIntention(PlayerStateMachine stateMachine) : base(stateMachine) { } - public override void Exit() { } - public override void Update() + public override void Enter(object arg0) { - //Arrived to destination - if (!InputManager.Instance.Move && !PlayerController.Instance.RunningToDestination) + if (_stateMachine.State == PlayerState.RUNNING || _stateMachine.State == PlayerState.IDLE) { _stateMachine.ChangeState(PlayerState.IDLE); } + else if (!_stateMachine.WaitingForServerReply) + { + _stateMachine.SetWaitingForServerReply(true); + GameClient.Instance.ClientPacketHandler.UpdateMoveDirection(Vector3.zero); + } + else + { + PlayerController.Instance.StopMoving(); + } + } + + public override void Exit() { } + public override void Update() + { } } \ No newline at end of file diff --git a/l2-unity/Assets/Scripts/Controller/StateMachine/State/AttackingState.cs b/l2-unity/Assets/Scripts/Controller/StateMachine/State/AttackingState.cs index 56583a78b..bf9e0dc77 100644 --- a/l2-unity/Assets/Scripts/Controller/StateMachine/State/AttackingState.cs +++ b/l2-unity/Assets/Scripts/Controller/StateMachine/State/AttackingState.cs @@ -27,6 +27,14 @@ public override void HandleEvent(Event evt) _stateMachine.ChangeState(PlayerState.RUNNING); } } + + if (evt == Event.ACTION_ALLOWED) + { + if (_stateMachine.Intention == Intention.INTENTION_IDLE) + { + _stateMachine.ChangeState(PlayerState.IDLE); + } + } }