112 lines
3.6 KiB
C#
112 lines
3.6 KiB
C#
using Assets.Scripts.Actions.Interfaces;
|
|
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;
|
|
|
|
public bool allowMovement = true;
|
|
private States _state;
|
|
private Vector3 _groundDeltaPosition;
|
|
|
|
public Dictionary<StatsId, Stat> PlayerStats;
|
|
public IWorkPlace WorkPlace { get; set; }
|
|
private Vector3 _pointToMove;
|
|
private bool _movePointReceived=false;
|
|
|
|
private void Start()
|
|
{
|
|
TimeManager.OnMinuteChanged += UpdatePlayerState;
|
|
allowMovement = true;
|
|
_navAgent.updatePosition = false;
|
|
PlayerStats = GameManager.Instance.PlayerStats;
|
|
MouseInputManager.Instance.OnMovementTargetSelected += Mouse_OnMovementTargetSelected;
|
|
}
|
|
|
|
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);
|
|
_groundDeltaPosition.y = Vector3.Dot(transform.forward, worldDeltaPosition);
|
|
|
|
Vector2 velocity = (Time.deltaTime > 1e-5f) ? _groundDeltaPosition / Time.deltaTime : Vector2.zero;
|
|
var shouldMove = velocity.magnitude > 0.025f && _navAgent.remainingDistance > _navAgent.radius;
|
|
if (_state != States.Sleeping)
|
|
{
|
|
_state = shouldMove ? States.Walking : States.Idle;
|
|
|
|
_animator.SetBool("Move", shouldMove);
|
|
_animator.SetFloat("velY", velocity.y);
|
|
}
|
|
}
|
|
private void OnAnimatorMove()
|
|
{
|
|
transform.position = _navAgent.nextPosition;
|
|
}
|
|
|
|
public void UpdatePlayerState()
|
|
{
|
|
PlayerStats[StatsId.Food].deduct(0.034m); // 48 hours it's 100, 100/2880=~0.034 per minute
|
|
switch (_state)
|
|
{
|
|
case States.Idle:
|
|
case States.Walking:
|
|
PlayerStats[StatsId.Energy].deduct(0.1m); // 24 hours it's 100, 100/1440=~0.096 per minute
|
|
break;
|
|
case States.Sleeping:
|
|
PlayerStats[StatsId.Energy].increase(1m);
|
|
break;
|
|
}
|
|
if (PlayerStats[StatsId.Energy].Value <= 10 && _state != States.Sleeping)
|
|
{
|
|
_state = States.Sleeping;
|
|
allowMovement = false;
|
|
_animator.SetBool("Fall", true);
|
|
}
|
|
if (PlayerStats[StatsId.Energy].Value >= 100 && _state == States.Sleeping)
|
|
{
|
|
_state = States.Idle;
|
|
allowMovement = true;
|
|
_animator.SetBool("Fall", false);
|
|
}
|
|
}
|
|
|
|
public void BuyAction(IPlayerAction action)
|
|
{
|
|
PlayerStats[StatsId.Money].deduct(((ISellable)action).Price);
|
|
action.ApplyAction(this);
|
|
}
|
|
|
|
}
|