Skip to content

Commit

Permalink
switch between targets
Browse files Browse the repository at this point in the history
  • Loading branch information
shnok committed Sep 4, 2024
1 parent a64e64f commit e3b86b5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 24 deletions.
63 changes: 44 additions & 19 deletions l2-unity/Assets/Scripts/Combat/TargetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}


Expand Down

0 comments on commit e3b86b5

Please sign in to comment.