added new animations

This commit is contained in:
Vladimir Koshevarov
2023-02-22 18:06:13 +02:00
parent a1e0316db5
commit 31eb9fb04a
14 changed files with 25407 additions and 66 deletions
+51 -19
View File
@@ -6,11 +6,11 @@ using UnityEngine.AI;
public enum Tasks { Move, Interact };
public enum TaskStatus { Waiting, InProgress, Complete };
public enum ActionStates { Idle, Walking, Sleeping, Sitting };
public class Player : MonoBehaviour
{
public event EventHandler<OnPlayerMovesEventArgs> OnPlayerMoves;
private enum States { Idle, Walking, Sleeping };
public static Player Instance { get; private set; }
@@ -20,7 +20,7 @@ public class Player : MonoBehaviour
public Animator _animator;
public bool allowMovement = true;
private States _state;
private ActionStates _currentState;
private Vector3 _groundDeltaPosition;
public Dictionary<StatsId, Stat> PlayerStats;
@@ -29,6 +29,12 @@ public class Player : MonoBehaviour
private readonly Queue<PlayerTasks> _tasks = new Queue<PlayerTasks>();
private PlayerTasks _currentTask;
private const string FALL_DOWN = "Fall";
private const string SIT_DOWN = "SitDown";
private const string STAND_UP = "StandUp";
private const string WALK = "Walk";
private const string WALK_VELOCITY = "WalkVelocity";
private void Awake()
{
if (Instance != null)
@@ -71,9 +77,10 @@ public class Player : MonoBehaviour
break;
}
}
}
private TaskStatus MoveToPoint()
{
var worldDeltaPosition = _navAgent.nextPosition - transform.position;
@@ -82,12 +89,12 @@ public class Player : MonoBehaviour
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)
if (_currentState != ActionStates.Sleeping)
{
_state = shouldMove ? States.Walking : States.Idle;
_currentState = shouldMove ? ActionStates.Walking : ActionStates.Idle;
_animator.SetBool("Move", shouldMove);
_animator.SetFloat("velY", velocity.y);
SetPlayerState(ActionStates.Walking);
_animator.SetFloat(WALK_VELOCITY, velocity.y);
}
return pathComplete() ? TaskStatus.Complete : TaskStatus.InProgress;
@@ -99,6 +106,7 @@ public class Player : MonoBehaviour
{
if (!_navAgent.hasPath || _navAgent.velocity.sqrMagnitude == 0f)
{
SetPlayerState(ActionStates.Idle);
return true;
}
}
@@ -108,10 +116,34 @@ public class Player : MonoBehaviour
private TaskStatus InteractWithObject(BaseInteractableObject interactableObject)
{
interactableObject.Interact();
interactableObject.Interact(this);
return TaskStatus.Complete;
}
public void SetPlayerState(ActionStates newState)
{
switch (newState)
{
case ActionStates.Idle:
_animator.SetBool(WALK, false);
break;
case ActionStates.Walking when _currentState == ActionStates.Sitting:
_animator.SetBool(SIT_DOWN, false);
_animator.SetBool(SIT_DOWN, true);
_animator.SetBool(WALK, true);
break;
case ActionStates.Walking:
_animator.SetBool(WALK, true);
break;
case ActionStates.Sleeping:
break;
break;
case ActionStates.Sitting:
break;
}
_currentState = newState;
}
private void OnAnimatorMove()
{
transform.position = _navAgent.nextPosition;
@@ -125,27 +157,27 @@ public class Player : MonoBehaviour
public void UpdatePlayerState()
{
PlayerStats[StatsId.Food].deduct(0.034m); // 48 hours it's 100, 100/2880=~0.034 per minute
switch (_state)
switch (_currentState)
{
case States.Idle:
case States.Walking:
case ActionStates.Idle:
case ActionStates.Walking:
PlayerStats[StatsId.Energy].deduct(0.1m); // 24 hours it's 100, 100/1440=~0.096 per minute
break;
case States.Sleeping:
case ActionStates.Sleeping:
PlayerStats[StatsId.Energy].increase(1m);
break;
}
if (PlayerStats[StatsId.Energy].Value <= 10 && _state != States.Sleeping)
if (PlayerStats[StatsId.Energy].Value <= 10 && _currentState != ActionStates.Sleeping)
{
_state = States.Sleeping;
_currentState = ActionStates.Sleeping;
allowMovement = false;
_animator.SetBool("Fall", true);
_animator.SetBool(FALL_DOWN, true);
}
if (PlayerStats[StatsId.Energy].Value >= 100 && _state == States.Sleeping)
if (PlayerStats[StatsId.Energy].Value >= 100 && _currentState == ActionStates.Sleeping)
{
_state = States.Idle;
_currentState = ActionStates.Idle;
allowMovement = true;
_animator.SetBool("Fall", false);
_animator.SetBool(FALL_DOWN, false);
}
}