Code refactor
This commit is contained in:
@@ -3,20 +3,20 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
using static GameManager;
|
||||
using static UnityEditor.PlayerSettings;
|
||||
|
||||
public class PlayerManager : MonoBehaviour
|
||||
{
|
||||
private enum States { Idle, Walking, Sleeping };
|
||||
|
||||
|
||||
[SerializeField]
|
||||
public LayerMask _walkableLayer;
|
||||
[SerializeField]
|
||||
public NavMeshAgent _navAgent;
|
||||
[SerializeField]
|
||||
public Animator _animator;
|
||||
[SerializeField]
|
||||
private Camera _camera;
|
||||
[SerializeField]
|
||||
public ParticleSystem _targetDest;
|
||||
|
||||
public bool allowMovement = true;
|
||||
private States _state;
|
||||
@@ -24,43 +24,37 @@ public class PlayerManager : MonoBehaviour
|
||||
|
||||
public Dictionary<StatsId, Stat> PlayerStats;
|
||||
public IWorkPlace WorkPlace { get; set; }
|
||||
private Vector3 _pointToMove;
|
||||
private bool _movePointReceived=false;
|
||||
|
||||
private void OnEnable()
|
||||
private void Start()
|
||||
{
|
||||
TimeManager.OnMinuteChanged += UpdatePlayerState;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
TimeManager.OnMinuteChanged -= UpdatePlayerState;
|
||||
}
|
||||
// Start is called before the first frame update
|
||||
|
||||
void Start()
|
||||
{
|
||||
allowMovement = true;
|
||||
_navAgent.updatePosition = false;
|
||||
PlayerStats = GameManager.Instance.PlayerStats;
|
||||
MouseInputManager.Instance.OnMovementTargetSelected += Mouse_OnMovementTargetSelected;
|
||||
}
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (allowMovement)
|
||||
{
|
||||
if (Input.GetMouseButton(0))
|
||||
{
|
||||
_targetDest.Stop();
|
||||
if (Physics.Raycast(_camera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, 100))
|
||||
{
|
||||
var pos = hit.point;
|
||||
pos.y += 0.2f;
|
||||
_targetDest.transform.position = pos;
|
||||
_navAgent.SetDestination(pos);
|
||||
_targetDest.Play();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Mouse_OnMovementTargetSelected(object sender, OnMovementTargetSelectedEventArgs e)
|
||||
{
|
||||
_pointToMove= e.PointToMove;
|
||||
_movePointReceived = true;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
TimeManager.OnMinuteChanged -= UpdatePlayerState;
|
||||
MouseInputManager.Instance.OnMovementTargetSelected -= Mouse_OnMovementTargetSelected;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (allowMovement && _movePointReceived)
|
||||
{
|
||||
_movePointReceived = false;
|
||||
_navAgent.SetDestination(_pointToMove);
|
||||
}
|
||||
|
||||
var worldDeltaPosition = _navAgent.nextPosition - transform.position;
|
||||
_groundDeltaPosition.x = Vector3.Dot(transform.right, worldDeltaPosition);
|
||||
|
||||
Reference in New Issue
Block a user