Files
SimUL/Assets/Scripts/PlayerManager.cs
T
Vladimir Koshevarov 73f06d8754 added tooltips
2022-11-29 19:09:00 +02:00

116 lines
3.5 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 += DecreaseEnergy;
}
private void OnDisable()
{
TimeManager.OnMinuteChanged -= DecreaseEnergy;
}
// 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;
_state = shouldMove ? States.Walking : States.Idle;
_animator.SetBool("Move", shouldMove);
_animator.SetFloat("velY", velocity.y);
}
private void OnAnimatorMove()
{
transform.position = _navAgent.nextPosition;
}
public void DecreaseEnergy()
{
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("IsSleeping", true);
}
if (PlayerStats[StatsId.Energy].Value >= 100 && _state == States.Sleeping)
{
_state = States.Idle;
allowMovement = false;
_animator.SetBool("IsSleeping", false);
}
}
public void BuyAction(IPlayerAction action)
{
PlayerStats[StatsId.Money].deduct(((ISellable)action).Price);
action.ApplyAction(this);
}
}