I have gone through the dependency breaking classes three times - must have missed something - can you make a suggestion here?
Fighter script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Move;
using RPG.Core;
namespace RPG.Combat
{
public class Fighter : MonoBehaviour, IAction
{
[SerializeField]
float weaponRange = 2f;
Transform target;
private void Update()
{
if (target == null) return;
if (!GetInRange())
{
GetComponent<Mover>().Moveto(target.position);
}
else
{
GetComponent<Mover>().Cancel();
}
}
private bool GetInRange()
{
return Vector3.Distance(transform.position, target.position) < weaponRange;
}
public void Attack(CombatTarget combatTarget)
{
target = combatTarget.transform;
}
public void Cancel()
{
target = null;
}
}
}
Mover script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using RPG.Core;
namespace RPG.Move
{
public class Mover : MonoBehaviour, IAction
{
[SerializeField]
private Transform target;
NavMeshAgent navMeshAgent;
private void Start()
{
navMeshAgent = GetComponent<NavMeshAgent>();
}
void Update()
{
UpdateAnimator();
}
public void Cancel()
{
navMeshAgent.isStopped = true;
}
public void StartMoveAction(Vector3 destination)
{
GetComponent<ActionScheduler>().StartAction(this);
Moveto(destination);
}
public void Moveto(Vector3 destination)
{
navMeshAgent.destination = destination;
navMeshAgent.isStopped = false;
}
private void UpdateAnimator()
{
Vector3 velocity = navMeshAgent.velocity;
Vector3 localVelocity = transform.InverseTransformDirection(velocity);
float speed = localVelocity.z;
GetComponent<Animator>().SetFloat("ForwardSpeed", speed);
}
}
}
Action Scheduler script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RPG.Core
{
public class ActionScheduler : MonoBehaviour
{
IAction currentAction;
public void StartAction(IAction action)
{
if (currentAction == action) return;
if (currentAction != null)
{
currentAction.Cancel();
}
currentAction = action;
}
}
}
Also the player has the mover, fighter and actionscheduler scripts attached.
Thanks!