From d9beeda7e0073a4267db647c45b03c3ba8bb4e25 Mon Sep 17 00:00:00 2001 From: Glence Date: Sun, 13 Nov 2022 21:56:28 +0800 Subject: [PATCH] adding the base for fsm for player --- Assets/Scripts/SC_PickAndThrow.cs | 38 ++++++----- Assets/Scripts/SC_PlayerController.cs | 96 ++++++++++++++++----------- Assets/Scripts/UT_BaseSate.cs | 5 +- Assets/Scripts/UT_StateMachine.cs | 50 ++++++++------ 4 files changed, 112 insertions(+), 77 deletions(-) diff --git a/Assets/Scripts/SC_PickAndThrow.cs b/Assets/Scripts/SC_PickAndThrow.cs index 34e7cd04..0bd70d60 100644 --- a/Assets/Scripts/SC_PickAndThrow.cs +++ b/Assets/Scripts/SC_PickAndThrow.cs @@ -6,7 +6,7 @@ public class PickAndThrow : Script { public Vector3 holdPosition = new Vector3(0.0f, 1.0f, 0.0f); public Vector3 throwForce = new Vector3(100.0f, 200.0f, 100.0f); - public GameObject item; + public GameObject item { get; set; } private PlayerController pc; private Camera cam; private Transform itemTransform; @@ -28,21 +28,7 @@ public class PickAndThrow : Script } protected override void update() { - if (cam == null) - cam = GetComponentInChildren(); - else if (cam != null) - { - Vector3 camerAixs = cam.GetForward(); - camerAixs.y = 0; - camerAixs.Normalise(); - lastXDir = camerAixs.x; - lastZDir = camerAixs.z; - } - - RetrieveItemComponets(); - - if (pc != null && inRange && !pc.holdItem && Input.GetMouseButtonDown(Input.MouseCode.LeftButton)) - pc.holdItem = true; + CalculateDir(); if (pc != null && pc.holdItem && itemRidibody != null && itemTransform != null) { @@ -58,10 +44,17 @@ public class PickAndThrow : Script itemRidibody.IsGravityEnabled = true; itemRidibody.AddForce(new Vector3(throwForce.x * lastXDir, throwForce.y, throwForce.z * lastZDir)); itemRidibody.LinearVelocity += pc.rb.LinearVelocity; + ResetItemObject(); } } else if(!pc.holdItem && itemRidibody != null) itemRidibody.IsGravityEnabled = true; + + if (pc != null && !pc.holdItem && inRange && Input.GetMouseButtonDown(Input.MouseCode.LeftButton)) + { + pc.holdItem = true; + RetrieveItemComponets(); + } } private void ResetItemObject() @@ -86,6 +79,19 @@ public class PickAndThrow : Script } } + private void CalculateDir() + { + if (cam == null) + cam = GetComponentInChildren(); + else if (cam != null) + { + Vector3 camerAixs = cam.GetForward(); + camerAixs.y = 0; + camerAixs.Normalise(); + lastXDir = camerAixs.x; + lastZDir = camerAixs.z; + } + } protected override void onCollisionEnter(CollisionInfo info) { diff --git a/Assets/Scripts/SC_PlayerController.cs b/Assets/Scripts/SC_PlayerController.cs index da169014..84f7584d 100644 --- a/Assets/Scripts/SC_PlayerController.cs +++ b/Assets/Scripts/SC_PlayerController.cs @@ -31,7 +31,7 @@ public class PlayerController : Script private Camera cam; private PickAndThrow pat; - public StateMachine stateMachine; + private StateMachine stateMachine; public bool holdItem { get; set; } [SerializeField] @@ -80,7 +80,9 @@ public class PlayerController : Script public float mediumMultiper = 0.5f; public float heavyMultiper = 0.25f; - public PlayerController(GameObject gameObj) : base(gameObj) { } + public PlayerController(GameObject gameObj) : base(gameObj) + { + } protected override void awake() { @@ -111,20 +113,18 @@ public class PlayerController : Script if(tranform == null) Debug.LogError("tranform is NULL!"); - //PickAndThrow checl + //PickAndThrow check pat = GetScript(); if (pat == null) Debug.LogError("PickAndThrow is NULL!"); - 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); - } +/* stateMachine = AddScript(); + 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() @@ -132,24 +132,15 @@ public class PlayerController : Script if (cam == null) cam = GetComponentInChildren(); - //toRemove - if (Input.GetKey(Input.KeyCode.G)) - { - tranform.LocalRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f); - tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f); - } - GotCaught(); - MoveKey(); - - - + //Debug.Log($"X: {axisMove.x}" + $" Y: {axisMove.y}"); //Debug.Log(currentState.ToString() + " x:" + rb.LinearVelocity.x.ToString() + " y:" + rb.LinearVelocity.y.ToString() + " z:" + rb.LinearVelocity.z.ToString()); } protected override void fixedUpdate() { //Rotation(); + MoveKey(); Move(); Sprint(); Jump(); @@ -184,6 +175,7 @@ public class PlayerController : Script camerAixs.Normalise(); xAxisMove = camerAixs.x; zAxisMove = camerAixs.z; + axisMove += new Vector2(camerAixs.x, camerAixs.z); } if (Input.GetKey(Input.KeyCode.S)) @@ -216,16 +208,20 @@ public class PlayerController : Script axisMove.Normalise(); isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0; - if (isMoveKeyPress && currentState != RaccoonStates.RUNNING && isGrounded) + if (isMoveKeyPress && isGrounded && !Input.GetKey(Input.KeyCode.LeftShift)) { currentState = RaccoonStates.WALKING; - stateMachine.SetState(typeof(WalkState)); + + if(stateMachine && !stateMachine.IsState(typeof(WalkState))) + stateMachine.SetState(typeof(WalkState)); } if (!isMoveKeyPress && isGrounded) { currentState = RaccoonStates.IDLE; - stateMachine.SetState(typeof(IdleState)); + + if(stateMachine && !stateMachine.IsState(typeof(IdleState))) + stateMachine.SetState(typeof(IdleState)); } } @@ -259,7 +255,8 @@ public class PlayerController : Script if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress && isGrounded) { currentState = RaccoonStates.RUNNING; - stateMachine.SetState(typeof(RunState)); + if (stateMachine && !stateMachine.IsState(typeof(RunState))) + stateMachine.SetState(typeof(RunState)); holdItem = false; if (!sprintIncreaseOnce) { @@ -277,7 +274,8 @@ public class PlayerController : Script if (isMoveKeyPress) { currentState = RaccoonStates.WALKING; - stateMachine.SetState(typeof(WalkState)); + if(stateMachine && !stateMachine.IsState(typeof(WalkState))) + stateMachine.SetState(typeof(WalkState)); } sprintIncreaseOnce = false; moveForce = oldForce; @@ -295,14 +293,14 @@ public class PlayerController : Script currentState = RaccoonStates.JUMP; Vector3 v = rb.LinearVelocity; v.y = initialJumpVel * 0.5f; - if (pat != null && pat.item.GetScript() != null && holdItem) + if (holdItem && pat != null && pat.item.GetScript() != null) { Item item = pat.item.GetScript(); - if (item.currCategory == ItemCategory.LIGHT) + if (item != null && item.currCategory == ItemCategory.LIGHT) v.y *= lightMultiper; - if (item.currCategory == ItemCategory.MEDIUM) + if (item != null && item.currCategory == ItemCategory.MEDIUM) v.y *= mediumMultiper; - if (item.currCategory == ItemCategory.HEAVY) + if (item != null && item.currCategory == ItemCategory.HEAVY) v.y *= heavyMultiper; } rb.LinearVelocity = v; @@ -370,7 +368,8 @@ public class PlayerController : Script if (currentState == RaccoonStates.CAUGHT && tranform != null) { currentState = RaccoonStates.IDLE; - stateMachine.SetState(typeof(IdleState)); + if (stateMachine && !stateMachine.IsState(typeof(IdleState))) + stateMachine.SetState(typeof(IdleState)); tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f); } } @@ -385,21 +384,30 @@ public class WalkState : BaseState { public WalkState(StateMachine stateMachine) : base(stateMachine) { - stateName = "Patrol State"; + stateName = "Walk State"; } public override void OnEnter() { Debug.Log("WALK ENTER"); } - public override void Update() + public override void update() { Debug.Log("WALKING"); } + public override void fixedUpdate() + { + Debug.Log("FIXED WALKING"); + } public override void OnExit() { Debug.Log("WALK EXIT"); } - } + public override void onTriggerEnter(CollisionInfo info) + { + Debug.Log("TRIGGER"); + } + +} public class RunState : BaseState { @@ -411,9 +419,13 @@ public class RunState : BaseState { Debug.Log("Run ENTER"); } - public override void Update() + public override void update() { - Debug.Log("RUNNNING"); + Debug.Log("RUNING"); + } + public override void fixedUpdate() + { + Debug.Log("FIXED RUNNING"); } public override void OnExit() { @@ -425,16 +437,20 @@ public class IdleState : BaseState { public IdleState(StateMachine stateMachine) : base(stateMachine) { - stateName = "Run State"; + stateName = "Idle State"; } public override void OnEnter() { Debug.Log("IDLE ENTER"); } - public override void Update() + public override void update() { Debug.Log("IDLING"); } + public override void fixedUpdate() + { + Debug.Log("FIXED IDLING"); + } public override void OnExit() { Debug.Log("IDLE EXIT"); diff --git a/Assets/Scripts/UT_BaseSate.cs b/Assets/Scripts/UT_BaseSate.cs index ab5ea7f2..521f5936 100644 --- a/Assets/Scripts/UT_BaseSate.cs +++ b/Assets/Scripts/UT_BaseSate.cs @@ -16,10 +16,11 @@ public abstract class BaseState public virtual void OnEnter() { - } - public abstract void Update(); + public abstract void update(); + + public abstract void fixedUpdate(); public virtual void OnExit() { diff --git a/Assets/Scripts/UT_StateMachine.cs b/Assets/Scripts/UT_StateMachine.cs index 9c82f256..b0881cee 100644 --- a/Assets/Scripts/UT_StateMachine.cs +++ b/Assets/Scripts/UT_StateMachine.cs @@ -3,14 +3,14 @@ using System; using System.Collections.Generic; using System.Linq; -public abstract class StateMachine : BaseComponent +public class StateMachine : Script { private Dictionary stateDictionary; public BaseState currentState = null; public string currentStateName; public string currentAnimName; - public StateMachine(uint entity) : base(entity) { } + public StateMachine(GameObject gameObj) : base(gameObj) { } public void InitStateMachine(Dictionary dictionary) { @@ -61,53 +61,65 @@ public abstract class StateMachine : BaseComponent 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) + protected override void update() + { + Debug.Log("updating"); + if (currentState != (null)) + { + currentStateName = currentState.GetStateName(); + currentAnimName = currentState.GetAnimName(); + currentState.update(); + } + + } + + protected override void fixedUpdate() + { + Debug.Log("fix update"); + if (currentState != (null)) + { + currentStateName = currentState.GetStateName(); + currentAnimName = currentState.GetAnimName(); + currentState.fixedUpdate(); + } + } + + protected override void onCollisionEnter(CollisionInfo info) { if (currentState != (null)) currentState.onCollisionEnter(info); } - public void onCollisionStay(CollisionInfo info) + protected override void onCollisionStay(CollisionInfo info) { if (currentState != (null)) currentState.onCollisionStay(info); } - public void onCollisionExit(CollisionInfo info) + protected override void onCollisionExit(CollisionInfo info) { if (currentState != (null)) currentState.onCollisionExit(info); } - public void onTriggerEnter(CollisionInfo info) + protected override void onTriggerEnter(CollisionInfo info) { if (currentState != (null)) currentState.onTriggerEnter(info); } - public void onTriggerStay(CollisionInfo info) + protected override void onTriggerStay(CollisionInfo info) { if (currentState != (null)) currentState.onTriggerStay(info); } - public void onTriggerExit(CollisionInfo info) + protected override void onTriggerExit(CollisionInfo info) { if (currentState != (null)) currentState.onTriggerExit(info);