Update changes to scripts and AudioSystem #224

Merged
glencelow merged 15 commits from PlayerController into main 2022-11-18 19:01:24 +08:00
6 changed files with 215 additions and 37 deletions
Showing only changes of commit fbec2bf866 - Show all commits

View File

@ -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<StateMachine>();
if (stateMachine)
{
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()
@ -216,10 +217,16 @@ public class PlayerController : Script
isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0;
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)
{
@ -267,7 +275,10 @@ public class PlayerController : Script
if (Input.GetKeyUp(Input.KeyCode.LeftShift))
{
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");
}
}

View File

@ -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)
{
}

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) { }
}