From 5fc5aa7d51e293d6f4cfc19703b8589e4bdf6afc Mon Sep 17 00:00:00 2001 From: MuhamadAjiW <16521119@mahasiswa.itb.ac.id> Date: Wed, 17 Apr 2024 17:28:01 +0700 Subject: [PATCH] feat: basic animation controller --- Assets/Scripts/Core/Player/Player.cs | 8 +++--- .../Core/Player/PlayerAnimationController.cs | 26 +++++++++++++++++++ Assets/Scripts/Core/Player/PlayerStats.cs | 2 +- .../Library/BaseClasses/RigidObject.cs | 4 ++- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/Assets/Scripts/Core/Player/Player.cs b/Assets/Scripts/Core/Player/Player.cs index 83ec7abf..24b45f97 100644 --- a/Assets/Scripts/Core/Player/Player.cs +++ b/Assets/Scripts/Core/Player/Player.cs @@ -1,3 +1,4 @@ +using System; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -5,15 +6,14 @@ using UnityEngine; public class Player : RigidObject{ private PlayerAnimationController animationController; private PlayerMovementController movementController; - private PlayerStateController stateController; + public PlayerStateController stateController; public PlayerStats stats; - public int State => stateController.state; new void Start(){ base.Start(); - animationController = new PlayerAnimationController(this); - movementController = new PlayerMovementController(this); stateController = new PlayerStateController(this); + movementController = new PlayerMovementController(this); + animationController = new PlayerAnimationController(this); stats = new PlayerStats(this); } diff --git a/Assets/Scripts/Core/Player/PlayerAnimationController.cs b/Assets/Scripts/Core/Player/PlayerAnimationController.cs index bb2c6850..b59864af 100644 --- a/Assets/Scripts/Core/Player/PlayerAnimationController.cs +++ b/Assets/Scripts/Core/Player/PlayerAnimationController.cs @@ -1,9 +1,35 @@ +using UnityEngine; + public class PlayerAnimationController{ + // Consts + private const string IDLE_TRIGGER = "idle_param"; + private const string WALK_TRIGGER = "walk_param"; + // Attributes private Player player; + private PlayerStateController playerStateController; + private Transform modelTransform; + private Animator animator; // Constructor public PlayerAnimationController(Player player){ this.player = player; + modelTransform = player.transform.Find("Model"); + animator = modelTransform.GetComponent<Animator>(); + + player.stateController.OnStateChange += Animate; + } + + public void Animate(){ + Debug.Log("State changed " + player.stateController.state); + + switch (player.stateController.state){ + case PlayerState.IDLE: + animator.SetBool(IDLE_TRIGGER, true); + break; + case PlayerState.WALKING: + animator.SetBool(WALK_TRIGGER, true); + break; + } } } diff --git a/Assets/Scripts/Core/Player/PlayerStats.cs b/Assets/Scripts/Core/Player/PlayerStats.cs index 019dd11e..e53edcca 100644 --- a/Assets/Scripts/Core/Player/PlayerStats.cs +++ b/Assets/Scripts/Core/Player/PlayerStats.cs @@ -6,7 +6,7 @@ public class PlayerStats { public float jumpForce = 600; public float snapshotSpeed = 25; - public float MaxSpeed => player.State switch{ + public float MaxSpeed => player.stateController.state switch{ PlayerState.WALKING => walkSpeed, PlayerState.SPRINTING => sprintSpeed, PlayerState.JUMPING => snapshotSpeed, diff --git a/Assets/Scripts/Library/BaseClasses/RigidObject.cs b/Assets/Scripts/Library/BaseClasses/RigidObject.cs index 145c5620..4964e29a 100644 --- a/Assets/Scripts/Library/BaseClasses/RigidObject.cs +++ b/Assets/Scripts/Library/BaseClasses/RigidObject.cs @@ -6,7 +6,9 @@ public class RigidObject : MonoBehaviour { // Readonly by others private new Rigidbody rigidbody; private new Collider collider; - private bool grounded; + + //TODO: grounded automatic detection + private bool grounded = true; public Rigidbody Rigidbody => rigidbody; -- GitLab