118 lines
3.6 KiB
C#
118 lines
3.6 KiB
C#
using Assets.Scripts.Actions.Interfaces;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
using static GameManager;
|
|
|
|
public class PlayerManager : MonoBehaviour
|
|
{
|
|
private enum States { Idle, Walking, Sleeping };
|
|
|
|
|
|
[SerializeField]
|
|
public NavMeshAgent _navAgent;
|
|
[SerializeField]
|
|
public Animator _animator;
|
|
[SerializeField]
|
|
private Camera _camera;
|
|
[SerializeField]
|
|
public ParticleSystem _targetDest;
|
|
|
|
public bool allowMovement = true;
|
|
private States _state;
|
|
private Vector3 _groundDeltaPosition;
|
|
|
|
public Dictionary<StatsId, Stat> PlayerStats;
|
|
public IWorkPlace WorkPlace { get; set; }
|
|
|
|
private void OnEnable()
|
|
{
|
|
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;
|
|
}
|
|
// 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();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
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);
|
|
}
|
|
|
|
}
|