Auto stash before merge of "main" and "origin/main"

This commit is contained in:
Glence 2022-11-11 14:17:47 +08:00
parent 4f9015995c
commit fbec2bf866
6 changed files with 215 additions and 37 deletions

View File

@ -7,7 +7,7 @@ public class PlayerController : Script
{ {
public enum RaccoonStates public enum RaccoonStates
{ {
IDILE, IDLE,
WALKING, WALKING,
RUNNING, RUNNING,
JUMP, JUMP,
@ -31,21 +31,18 @@ public class PlayerController : Script
private Camera cam; private Camera cam;
private PickAndThrow pat; private PickAndThrow pat;
//to be remove public StateMachine stateMachine;
public float drag = 2.0f;
public bool holdItem { get; set; } public bool holdItem { get; set; }
[SerializeField] [SerializeField]
[Tooltip("The current state fo the raccoon")] [Tooltip("The current state fo the raccoon")]
public RaccoonStates currentState = RaccoonStates.IDILE; public RaccoonStates currentState = RaccoonStates.IDLE;
//Movement variables============================================================ //Movement variables============================================================
[SerializeField]
[Tooltip("Max vel for walking")] [Tooltip("Max vel for walking")]
public float maxMoveVel = 2.0f; public float maxMoveVel = 2.0f;
[SerializeField]
[Tooltip("how much force is apply for walking")] [Tooltip("how much force is apply for walking")]
public float moveForce = 50.0f; public float moveForce = 50.0f;
[SerializeField]
[Tooltip("increase the moveForce and maxMoveVel by its amt")] [Tooltip("increase the moveForce and maxMoveVel by its amt")]
public float sprintMultiplier = 2.0f; public float sprintMultiplier = 2.0f;
@ -60,7 +57,6 @@ public class PlayerController : Script
public bool isMoveKeyPress { get; set; } public bool isMoveKeyPress { get; set; }
[SerializeField]
[Tooltip("curr not working")] [Tooltip("curr not working")]
public float rotationFactorPerFrame = 1.0f; public float rotationFactorPerFrame = 1.0f;
@ -107,7 +103,6 @@ public class PlayerController : Script
rb.FreezeRotationX = true; rb.FreezeRotationX = true;
rb.FreezeRotationY = true; rb.FreezeRotationY = true;
rb.FreezeRotationZ = true; rb.FreezeRotationZ = true;
rb.Drag = drag;
rb.Interpolating = false; rb.Interpolating = false;
} }
@ -121,9 +116,15 @@ public class PlayerController : Script
if (pat == null) if (pat == null)
Debug.LogError("PickAndThrow is NULL!"); Debug.LogError("PickAndThrow is NULL!");
//toRemove stateMachine = GetComponent<StateMachine>();
tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f); if (stateMachine)
tranform.LocalRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f); {
Dictionary<Type, BaseState> dictionary = new Dictionary<Type, BaseState>();
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() protected override void update()
@ -216,10 +217,16 @@ public class PlayerController : Script
isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0; isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0;
if (isMoveKeyPress && currentState != RaccoonStates.RUNNING && isGrounded) if (isMoveKeyPress && currentState != RaccoonStates.RUNNING && isGrounded)
{
currentState = RaccoonStates.WALKING; currentState = RaccoonStates.WALKING;
stateMachine.SetState(typeof(WalkState));
}
if (!isMoveKeyPress && isGrounded) if (!isMoveKeyPress && isGrounded)
currentState = RaccoonStates.IDILE; {
currentState = RaccoonStates.IDLE;
stateMachine.SetState(typeof(IdleState));
}
} }
private void Move() private void Move()
@ -252,6 +259,7 @@ public class PlayerController : Script
if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress && isGrounded) if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress && isGrounded)
{ {
currentState = RaccoonStates.RUNNING; currentState = RaccoonStates.RUNNING;
stateMachine.SetState(typeof(RunState));
holdItem = false; holdItem = false;
if (!sprintIncreaseOnce) if (!sprintIncreaseOnce)
{ {
@ -267,7 +275,10 @@ public class PlayerController : Script
if (Input.GetKeyUp(Input.KeyCode.LeftShift)) if (Input.GetKeyUp(Input.KeyCode.LeftShift))
{ {
if (isMoveKeyPress) if (isMoveKeyPress)
{
currentState = RaccoonStates.WALKING; currentState = RaccoonStates.WALKING;
stateMachine.SetState(typeof(WalkState));
}
sprintIncreaseOnce = false; sprintIncreaseOnce = false;
moveForce = oldForce; moveForce = oldForce;
maxMoveVel = maxOldVel; maxMoveVel = maxOldVel;
@ -277,7 +288,7 @@ public class PlayerController : Script
//press and hold jump //press and hold jump
private void 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) if (Input.GetKeyDown(Input.KeyCode.Space) && isGrounded && rb != null)
{ {
@ -358,7 +369,8 @@ public class PlayerController : Script
{ {
if (currentState == RaccoonStates.CAUGHT && tranform != null) 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); 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");
}
} }

View File

@ -8,7 +8,7 @@ public abstract class BaseState
protected StateMachine machine; protected StateMachine machine;
protected string animationName = ""; protected string animationName = "";
public BaseState(StateMachine stateMachine, string animName) public BaseState(StateMachine stateMachine, string animName = "")
{ {
machine = stateMachine; machine = stateMachine;
animationName = animName; animationName = animName;
@ -19,7 +19,7 @@ public abstract class BaseState
} }
public abstract void Update(float dt); public abstract void Update();
public virtual void OnExit() public virtual void OnExit()
{ {
@ -41,22 +41,26 @@ public abstract class BaseState
return 1.0f; 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 onCollisionExit(CollisionInfo info)
{ {
} }
public virtual void onTriggerEnter(CollisionInfo info) public virtual void onTriggerEnter(CollisionInfo info)
{ {
} }
public virtual void onTriggerStay(CollisionInfo info) public virtual void onTriggerStay(CollisionInfo info)
{ {
} }
public virtual void onTriggerExit(CollisionInfo info) public virtual void onTriggerExit(CollisionInfo info)
{ {
} }

View File

@ -0,0 +1,118 @@
using SHADE;
using System;
using System.Collections.Generic;
using System.Linq;
public abstract class StateMachine : BaseComponent
{
private Dictionary<Type, BaseState> stateDictionary;
public BaseState currentState = null;
public string currentStateName;
public string currentAnimName;
public StateMachine(uint entity) : base(entity) { }
public void InitStateMachine(Dictionary<Type, BaseState> 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);
}
}

View File

@ -1,15 +0,0 @@
using SHADE;
using System;
using System.Collections.Generic;
public abstract class StateMachine : Script
{
private Dictionary<Type, BaseState> stateDictionary;
public BaseState currentState = null;
public string currentStateName;
public string currentAnimName;
public StateMachine(GameObject gameObj) : base(gameObj) { }
}