From fbec2bf866562264696edfe2111403b628bc04f7 Mon Sep 17 00:00:00 2001 From: Glence Date: Fri, 11 Nov 2022 14:17:47 +0800 Subject: [PATCH] Auto stash before merge of "main" and "origin/main" --- .../Scripts}/SC_Item.cs | 0 .../Scripts}/SC_PickAndThrow.cs | 0 .../Scripts}/SC_PlayerController.cs | 107 +++++++++++++--- .../Scripts}/UT_BaseSate.cs | 12 +- Assets/Scripts/UT_StateMachine.cs | 118 ++++++++++++++++++ TempScriptsFolder/UT_StateMachine.cs | 15 --- 6 files changed, 215 insertions(+), 37 deletions(-) rename {TempScriptsFolder => Assets/Scripts}/SC_Item.cs (100%) rename {TempScriptsFolder => Assets/Scripts}/SC_PickAndThrow.cs (100%) rename {TempScriptsFolder => Assets/Scripts}/SC_PlayerController.cs (81%) rename {TempScriptsFolder => Assets/Scripts}/UT_BaseSate.cs (78%) create mode 100644 Assets/Scripts/UT_StateMachine.cs delete mode 100644 TempScriptsFolder/UT_StateMachine.cs diff --git a/TempScriptsFolder/SC_Item.cs b/Assets/Scripts/SC_Item.cs similarity index 100% rename from TempScriptsFolder/SC_Item.cs rename to Assets/Scripts/SC_Item.cs diff --git a/TempScriptsFolder/SC_PickAndThrow.cs b/Assets/Scripts/SC_PickAndThrow.cs similarity index 100% rename from TempScriptsFolder/SC_PickAndThrow.cs rename to Assets/Scripts/SC_PickAndThrow.cs diff --git a/TempScriptsFolder/SC_PlayerController.cs b/Assets/Scripts/SC_PlayerController.cs similarity index 81% rename from TempScriptsFolder/SC_PlayerController.cs rename to Assets/Scripts/SC_PlayerController.cs index 6b5e38d7..da169014 100644 --- a/TempScriptsFolder/SC_PlayerController.cs +++ b/Assets/Scripts/SC_PlayerController.cs @@ -7,7 +7,7 @@ public class PlayerController : Script { public enum RaccoonStates { - IDILE, + IDLE, WALKING, RUNNING, JUMP, @@ -31,21 +31,18 @@ public class PlayerController : Script private Camera cam; private PickAndThrow pat; - //to be remove - public float drag = 2.0f; + public StateMachine stateMachine; + public bool holdItem { get; set; } [SerializeField] [Tooltip("The current state fo the raccoon")] - public RaccoonStates currentState = RaccoonStates.IDILE; + public RaccoonStates currentState = RaccoonStates.IDLE; //Movement variables============================================================ - [SerializeField] [Tooltip("Max vel for walking")] public float maxMoveVel = 2.0f; - [SerializeField] [Tooltip("how much force is apply for walking")] public float moveForce = 50.0f; - [SerializeField] [Tooltip("increase the moveForce and maxMoveVel by its amt")] public float sprintMultiplier = 2.0f; @@ -60,7 +57,6 @@ public class PlayerController : Script public bool isMoveKeyPress { get; set; } - [SerializeField] [Tooltip("curr not working")] public float rotationFactorPerFrame = 1.0f; @@ -107,7 +103,6 @@ public class PlayerController : Script rb.FreezeRotationX = true; rb.FreezeRotationY = true; rb.FreezeRotationZ = true; - rb.Drag = drag; rb.Interpolating = false; } @@ -121,9 +116,15 @@ public class PlayerController : Script if (pat == null) Debug.LogError("PickAndThrow is NULL!"); - //toRemove - tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f); - tranform.LocalRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f); + stateMachine = GetComponent(); + if (stateMachine) + { + Dictionary dictionary = new Dictionary(); + dictionary.Add(typeof(IdleState), new IdleState(stateMachine)); + dictionary.Add(typeof(WalkState), new WalkState(stateMachine)); + dictionary.Add(typeof(RunState), new RunState(stateMachine)); + stateMachine.InitStateMachine(dictionary); + } } protected override void update() @@ -215,11 +216,17 @@ public class PlayerController : Script axisMove.Normalise(); isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0; - if(isMoveKeyPress && currentState != RaccoonStates.RUNNING && isGrounded) + if (isMoveKeyPress && currentState != RaccoonStates.RUNNING && isGrounded) + { currentState = RaccoonStates.WALKING; + stateMachine.SetState(typeof(WalkState)); + } if (!isMoveKeyPress && isGrounded) - currentState = RaccoonStates.IDILE; + { + currentState = RaccoonStates.IDLE; + stateMachine.SetState(typeof(IdleState)); + } } private void Move() @@ -252,6 +259,7 @@ public class PlayerController : Script if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress && isGrounded) { currentState = RaccoonStates.RUNNING; + stateMachine.SetState(typeof(RunState)); holdItem = false; if (!sprintIncreaseOnce) { @@ -266,8 +274,11 @@ public class PlayerController : Script if (Input.GetKeyUp(Input.KeyCode.LeftShift)) { - if(isMoveKeyPress) + if (isMoveKeyPress) + { currentState = RaccoonStates.WALKING; + stateMachine.SetState(typeof(WalkState)); + } sprintIncreaseOnce = false; moveForce = oldForce; maxMoveVel = maxOldVel; @@ -277,7 +288,7 @@ public class PlayerController : Script //press and hold jump private void Jump() { - if (currentState == RaccoonStates.WALKING || currentState == RaccoonStates.RUNNING || currentState == RaccoonStates.IDILE) + if (currentState == RaccoonStates.WALKING || currentState == RaccoonStates.RUNNING || currentState == RaccoonStates.IDLE) { if (Input.GetKeyDown(Input.KeyCode.Space) && isGrounded && rb != null) { @@ -358,7 +369,8 @@ public class PlayerController : Script { if (currentState == RaccoonStates.CAUGHT && tranform != null) { - currentState = RaccoonStates.IDILE; + currentState = RaccoonStates.IDLE; + stateMachine.SetState(typeof(IdleState)); tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f); } } @@ -367,6 +379,65 @@ public class PlayerController : Script { } - +} + +public class WalkState : BaseState +{ + public WalkState(StateMachine stateMachine) : base(stateMachine) + { + stateName = "Patrol State"; + } + public override void OnEnter() + { + Debug.Log("WALK ENTER"); + } + public override void Update() + { + Debug.Log("WALKING"); + } + public override void OnExit() + { + Debug.Log("WALK EXIT"); + } + } + +public class RunState : BaseState +{ + public RunState(StateMachine stateMachine) : base(stateMachine) + { + stateName = "Run State"; + } + public override void OnEnter() + { + Debug.Log("Run ENTER"); + } + public override void Update() + { + Debug.Log("RUNNNING"); + } + public override void OnExit() + { + Debug.Log("Run EXIT"); + } +} + +public class IdleState : BaseState +{ + public IdleState(StateMachine stateMachine) : base(stateMachine) + { + stateName = "Run State"; + } + public override void OnEnter() + { + Debug.Log("IDLE ENTER"); + } + public override void Update() + { + Debug.Log("IDLING"); + } + public override void OnExit() + { + Debug.Log("IDLE EXIT"); + } } diff --git a/TempScriptsFolder/UT_BaseSate.cs b/Assets/Scripts/UT_BaseSate.cs similarity index 78% rename from TempScriptsFolder/UT_BaseSate.cs rename to Assets/Scripts/UT_BaseSate.cs index a2842eee..ab5ea7f2 100644 --- a/TempScriptsFolder/UT_BaseSate.cs +++ b/Assets/Scripts/UT_BaseSate.cs @@ -8,7 +8,7 @@ public abstract class BaseState protected StateMachine machine; protected string animationName = ""; - public BaseState(StateMachine stateMachine, string animName) + public BaseState(StateMachine stateMachine, string animName = "") { machine = stateMachine; animationName = animName; @@ -19,7 +19,7 @@ public abstract class BaseState } - public abstract void Update(float dt); + public abstract void Update(); public virtual void OnExit() { @@ -41,22 +41,26 @@ public abstract class BaseState return 1.0f; } - public virtual void onCollisionEnter(CollisionInfo other) + public virtual void onCollisionEnter(CollisionInfo info) { } - public virtual void onCollisionStay(CollisionInfo other) + public virtual void onCollisionStay(CollisionInfo info) { } + public virtual void onCollisionExit(CollisionInfo info) { } + public virtual void onTriggerEnter(CollisionInfo info) { } + public virtual void onTriggerStay(CollisionInfo info) { } + public virtual void onTriggerExit(CollisionInfo info) { } diff --git a/Assets/Scripts/UT_StateMachine.cs b/Assets/Scripts/UT_StateMachine.cs new file mode 100644 index 00000000..9c82f256 --- /dev/null +++ b/Assets/Scripts/UT_StateMachine.cs @@ -0,0 +1,118 @@ +using SHADE; +using System; +using System.Collections.Generic; +using System.Linq; + +public abstract class StateMachine : BaseComponent +{ + private Dictionary stateDictionary; + public BaseState currentState = null; + public string currentStateName; + public string currentAnimName; + + public StateMachine(uint entity) : base(entity) { } + + public void InitStateMachine(Dictionary dictionary) + { + + stateDictionary = dictionary; + currentState = stateDictionary.First().Value; + currentStateName = currentState.GetStateName(); + currentAnimName = currentState.GetAnimName(); + currentState.OnEnter(); + } + + public bool HasState(Type type) + { + if (!type.IsSubclassOf(typeof(BaseState))) + { + return false; + } + else + { + return stateDictionary.ContainsKey(type); + } + } + + public void SetState(Type type) + { + if (!type.IsSubclassOf(typeof(BaseState))) + { + return; + } + + + if (stateDictionary.ContainsKey(type)) + { + currentState.OnExit(); + currentState = stateDictionary[type]; + currentState.OnEnter(); + } + else + { + SetState(stateDictionary.First().Key); + } + } + + public BaseState GetState(Type type) + { + if (!stateDictionary.ContainsKey(type)) + return null; + + return stateDictionary[type]; + } + + public void Update() + { + if (currentState != (null)) + { + currentStateName = currentState.GetStateName(); + currentAnimName = currentState.GetAnimName(); + currentState.Update(); + } + + } + public bool IsState(Type type) + { + return (currentState.GetType() == type); + } + + public void onCollisionEnter(CollisionInfo info) + { + if (currentState != (null)) + currentState.onCollisionEnter(info); + } + + public void onCollisionStay(CollisionInfo info) + { + if (currentState != (null)) + currentState.onCollisionStay(info); + } + + public void onCollisionExit(CollisionInfo info) + { + if (currentState != (null)) + currentState.onCollisionExit(info); + } + + public void onTriggerEnter(CollisionInfo info) + { + if (currentState != (null)) + currentState.onTriggerEnter(info); + } + + public void onTriggerStay(CollisionInfo info) + { + if (currentState != (null)) + currentState.onTriggerStay(info); + } + + public void onTriggerExit(CollisionInfo info) + { + if (currentState != (null)) + currentState.onTriggerExit(info); + } + + +} + diff --git a/TempScriptsFolder/UT_StateMachine.cs b/TempScriptsFolder/UT_StateMachine.cs deleted file mode 100644 index 393c6a45..00000000 --- a/TempScriptsFolder/UT_StateMachine.cs +++ /dev/null @@ -1,15 +0,0 @@ -using SHADE; -using System; -using System.Collections.Generic; - -public abstract class StateMachine : Script -{ - private Dictionary stateDictionary; - public BaseState currentState = null; - public string currentStateName; - public string currentAnimName; - - public StateMachine(GameObject gameObj) : base(gameObj) { } - -} -