adding the base for fsm for player

This commit is contained in:
Glence 2022-11-13 21:56:28 +08:00
parent fbec2bf866
commit d9beeda7e0
4 changed files with 112 additions and 77 deletions

View File

@ -6,7 +6,7 @@ public class PickAndThrow : Script
{ {
public Vector3 holdPosition = new Vector3(0.0f, 1.0f, 0.0f); public Vector3 holdPosition = new Vector3(0.0f, 1.0f, 0.0f);
public Vector3 throwForce = new Vector3(100.0f, 200.0f, 100.0f); public Vector3 throwForce = new Vector3(100.0f, 200.0f, 100.0f);
public GameObject item; public GameObject item { get; set; }
private PlayerController pc; private PlayerController pc;
private Camera cam; private Camera cam;
private Transform itemTransform; private Transform itemTransform;
@ -28,21 +28,7 @@ public class PickAndThrow : Script
} }
protected override void update() protected override void update()
{ {
if (cam == null) CalculateDir();
cam = GetComponentInChildren<Camera>();
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;
if (pc != null && pc.holdItem && itemRidibody != null && itemTransform != null) if (pc != null && pc.holdItem && itemRidibody != null && itemTransform != null)
{ {
@ -58,10 +44,17 @@ public class PickAndThrow : Script
itemRidibody.IsGravityEnabled = true; itemRidibody.IsGravityEnabled = true;
itemRidibody.AddForce(new Vector3(throwForce.x * lastXDir, throwForce.y, throwForce.z * lastZDir)); itemRidibody.AddForce(new Vector3(throwForce.x * lastXDir, throwForce.y, throwForce.z * lastZDir));
itemRidibody.LinearVelocity += pc.rb.LinearVelocity; itemRidibody.LinearVelocity += pc.rb.LinearVelocity;
ResetItemObject();
} }
} }
else if(!pc.holdItem && itemRidibody != null) else if(!pc.holdItem && itemRidibody != null)
itemRidibody.IsGravityEnabled = true; itemRidibody.IsGravityEnabled = true;
if (pc != null && !pc.holdItem && inRange && Input.GetMouseButtonDown(Input.MouseCode.LeftButton))
{
pc.holdItem = true;
RetrieveItemComponets();
}
} }
private void ResetItemObject() private void ResetItemObject()
@ -86,6 +79,19 @@ public class PickAndThrow : Script
} }
} }
private void CalculateDir()
{
if (cam == null)
cam = GetComponentInChildren<Camera>();
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) protected override void onCollisionEnter(CollisionInfo info)
{ {

View File

@ -31,7 +31,7 @@ public class PlayerController : Script
private Camera cam; private Camera cam;
private PickAndThrow pat; private PickAndThrow pat;
public StateMachine stateMachine; private StateMachine stateMachine;
public bool holdItem { get; set; } public bool holdItem { get; set; }
[SerializeField] [SerializeField]
@ -80,7 +80,9 @@ public class PlayerController : Script
public float mediumMultiper = 0.5f; public float mediumMultiper = 0.5f;
public float heavyMultiper = 0.25f; public float heavyMultiper = 0.25f;
public PlayerController(GameObject gameObj) : base(gameObj) { } public PlayerController(GameObject gameObj) : base(gameObj)
{
}
protected override void awake() protected override void awake()
{ {
@ -111,20 +113,18 @@ public class PlayerController : Script
if(tranform == null) if(tranform == null)
Debug.LogError("tranform is NULL!"); Debug.LogError("tranform is NULL!");
//PickAndThrow checl //PickAndThrow check
pat = GetScript<PickAndThrow>(); pat = GetScript<PickAndThrow>();
if (pat == null) if (pat == null)
Debug.LogError("PickAndThrow is NULL!"); Debug.LogError("PickAndThrow is NULL!");
stateMachine = GetComponent<StateMachine>(); /* stateMachine = AddScript<StateMachine>();
if (stateMachine)
{
Dictionary<Type, BaseState> dictionary = new Dictionary<Type, BaseState>(); Dictionary<Type, BaseState> dictionary = new Dictionary<Type, BaseState>();
dictionary.Add(typeof(IdleState), new IdleState(stateMachine)); dictionary.Add(typeof(IdleState), new IdleState(stateMachine));
dictionary.Add(typeof(WalkState), new WalkState(stateMachine)); dictionary.Add(typeof(WalkState), new WalkState(stateMachine));
dictionary.Add(typeof(RunState), new RunState(stateMachine)); dictionary.Add(typeof(RunState), new RunState(stateMachine));
stateMachine.InitStateMachine(dictionary); stateMachine.InitStateMachine(dictionary);*/
}
} }
protected override void update() protected override void update()
@ -132,24 +132,15 @@ public class PlayerController : Script
if (cam == null) if (cam == null)
cam = GetComponentInChildren<Camera>(); cam = GetComponentInChildren<Camera>();
//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(); 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()); //Debug.Log(currentState.ToString() + " x:" + rb.LinearVelocity.x.ToString() + " y:" + rb.LinearVelocity.y.ToString() + " z:" + rb.LinearVelocity.z.ToString());
} }
protected override void fixedUpdate() protected override void fixedUpdate()
{ {
//Rotation(); //Rotation();
MoveKey();
Move(); Move();
Sprint(); Sprint();
Jump(); Jump();
@ -184,6 +175,7 @@ public class PlayerController : Script
camerAixs.Normalise(); camerAixs.Normalise();
xAxisMove = camerAixs.x; xAxisMove = camerAixs.x;
zAxisMove = camerAixs.z; zAxisMove = camerAixs.z;
axisMove += new Vector2(camerAixs.x, camerAixs.z); axisMove += new Vector2(camerAixs.x, camerAixs.z);
} }
if (Input.GetKey(Input.KeyCode.S)) if (Input.GetKey(Input.KeyCode.S))
@ -216,15 +208,19 @@ public class PlayerController : Script
axisMove.Normalise(); axisMove.Normalise();
isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0; isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0;
if (isMoveKeyPress && currentState != RaccoonStates.RUNNING && isGrounded) if (isMoveKeyPress && isGrounded && !Input.GetKey(Input.KeyCode.LeftShift))
{ {
currentState = RaccoonStates.WALKING; currentState = RaccoonStates.WALKING;
if(stateMachine && !stateMachine.IsState(typeof(WalkState)))
stateMachine.SetState(typeof(WalkState)); stateMachine.SetState(typeof(WalkState));
} }
if (!isMoveKeyPress && isGrounded) if (!isMoveKeyPress && isGrounded)
{ {
currentState = RaccoonStates.IDLE; currentState = RaccoonStates.IDLE;
if(stateMachine && !stateMachine.IsState(typeof(IdleState)))
stateMachine.SetState(typeof(IdleState)); stateMachine.SetState(typeof(IdleState));
} }
} }
@ -259,6 +255,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;
if (stateMachine && !stateMachine.IsState(typeof(RunState)))
stateMachine.SetState(typeof(RunState)); stateMachine.SetState(typeof(RunState));
holdItem = false; holdItem = false;
if (!sprintIncreaseOnce) if (!sprintIncreaseOnce)
@ -277,6 +274,7 @@ public class PlayerController : Script
if (isMoveKeyPress) if (isMoveKeyPress)
{ {
currentState = RaccoonStates.WALKING; currentState = RaccoonStates.WALKING;
if(stateMachine && !stateMachine.IsState(typeof(WalkState)))
stateMachine.SetState(typeof(WalkState)); stateMachine.SetState(typeof(WalkState));
} }
sprintIncreaseOnce = false; sprintIncreaseOnce = false;
@ -295,14 +293,14 @@ public class PlayerController : Script
currentState = RaccoonStates.JUMP; currentState = RaccoonStates.JUMP;
Vector3 v = rb.LinearVelocity; Vector3 v = rb.LinearVelocity;
v.y = initialJumpVel * 0.5f; v.y = initialJumpVel * 0.5f;
if (pat != null && pat.item.GetScript<Item>() != null && holdItem) if (holdItem && pat != null && pat.item.GetScript<Item>() != null)
{ {
Item item = pat.item.GetScript<Item>(); Item item = pat.item.GetScript<Item>();
if (item.currCategory == ItemCategory.LIGHT) if (item != null && item.currCategory == ItemCategory.LIGHT)
v.y *= lightMultiper; v.y *= lightMultiper;
if (item.currCategory == ItemCategory.MEDIUM) if (item != null && item.currCategory == ItemCategory.MEDIUM)
v.y *= mediumMultiper; v.y *= mediumMultiper;
if (item.currCategory == ItemCategory.HEAVY) if (item != null && item.currCategory == ItemCategory.HEAVY)
v.y *= heavyMultiper; v.y *= heavyMultiper;
} }
rb.LinearVelocity = v; rb.LinearVelocity = v;
@ -370,6 +368,7 @@ public class PlayerController : Script
if (currentState == RaccoonStates.CAUGHT && tranform != null) if (currentState == RaccoonStates.CAUGHT && tranform != null)
{ {
currentState = RaccoonStates.IDLE; currentState = RaccoonStates.IDLE;
if (stateMachine && !stateMachine.IsState(typeof(IdleState)))
stateMachine.SetState(typeof(IdleState)); stateMachine.SetState(typeof(IdleState));
tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f); tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f);
} }
@ -385,20 +384,29 @@ public class WalkState : BaseState
{ {
public WalkState(StateMachine stateMachine) : base(stateMachine) public WalkState(StateMachine stateMachine) : base(stateMachine)
{ {
stateName = "Patrol State"; stateName = "Walk State";
} }
public override void OnEnter() public override void OnEnter()
{ {
Debug.Log("WALK ENTER"); Debug.Log("WALK ENTER");
} }
public override void Update() public override void update()
{ {
Debug.Log("WALKING"); Debug.Log("WALKING");
} }
public override void fixedUpdate()
{
Debug.Log("FIXED WALKING");
}
public override void OnExit() public override void OnExit()
{ {
Debug.Log("WALK EXIT"); Debug.Log("WALK EXIT");
} }
public override void onTriggerEnter(CollisionInfo info)
{
Debug.Log("TRIGGER");
}
} }
public class RunState : BaseState public class RunState : BaseState
@ -411,9 +419,13 @@ public class RunState : BaseState
{ {
Debug.Log("Run ENTER"); 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() public override void OnExit()
{ {
@ -425,16 +437,20 @@ public class IdleState : BaseState
{ {
public IdleState(StateMachine stateMachine) : base(stateMachine) public IdleState(StateMachine stateMachine) : base(stateMachine)
{ {
stateName = "Run State"; stateName = "Idle State";
} }
public override void OnEnter() public override void OnEnter()
{ {
Debug.Log("IDLE ENTER"); Debug.Log("IDLE ENTER");
} }
public override void Update() public override void update()
{ {
Debug.Log("IDLING"); Debug.Log("IDLING");
} }
public override void fixedUpdate()
{
Debug.Log("FIXED IDLING");
}
public override void OnExit() public override void OnExit()
{ {
Debug.Log("IDLE EXIT"); Debug.Log("IDLE EXIT");

View File

@ -16,10 +16,11 @@ public abstract class BaseState
public virtual void OnEnter() public virtual void OnEnter()
{ {
} }
public abstract void Update(); public abstract void update();
public abstract void fixedUpdate();
public virtual void OnExit() public virtual void OnExit()
{ {

View File

@ -3,14 +3,14 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
public abstract class StateMachine : BaseComponent public class StateMachine : Script
{ {
private Dictionary<Type, BaseState> stateDictionary; private Dictionary<Type, BaseState> stateDictionary;
public BaseState currentState = null; public BaseState currentState = null;
public string currentStateName; public string currentStateName;
public string currentAnimName; public string currentAnimName;
public StateMachine(uint entity) : base(entity) { } public StateMachine(GameObject gameObj) : base(gameObj) { }
public void InitStateMachine(Dictionary<Type, BaseState> dictionary) public void InitStateMachine(Dictionary<Type, BaseState> dictionary)
{ {
@ -61,53 +61,65 @@ public abstract class StateMachine : BaseComponent
return stateDictionary[type]; return stateDictionary[type];
} }
public void Update()
{
if (currentState != (null))
{
currentStateName = currentState.GetStateName();
currentAnimName = currentState.GetAnimName();
currentState.Update();
}
}
public bool IsState(Type type) public bool IsState(Type type)
{ {
return (currentState.GetType() == 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)) if (currentState != (null))
currentState.onCollisionEnter(info); currentState.onCollisionEnter(info);
} }
public void onCollisionStay(CollisionInfo info) protected override void onCollisionStay(CollisionInfo info)
{ {
if (currentState != (null)) if (currentState != (null))
currentState.onCollisionStay(info); currentState.onCollisionStay(info);
} }
public void onCollisionExit(CollisionInfo info) protected override void onCollisionExit(CollisionInfo info)
{ {
if (currentState != (null)) if (currentState != (null))
currentState.onCollisionExit(info); currentState.onCollisionExit(info);
} }
public void onTriggerEnter(CollisionInfo info) protected override void onTriggerEnter(CollisionInfo info)
{ {
if (currentState != (null)) if (currentState != (null))
currentState.onTriggerEnter(info); currentState.onTriggerEnter(info);
} }
public void onTriggerStay(CollisionInfo info) protected override void onTriggerStay(CollisionInfo info)
{ {
if (currentState != (null)) if (currentState != (null))
currentState.onTriggerStay(info); currentState.onTriggerStay(info);
} }
public void onTriggerExit(CollisionInfo info) protected override void onTriggerExit(CollisionInfo info)
{ {
if (currentState != (null)) if (currentState != (null))
currentState.onTriggerExit(info); currentState.onTriggerExit(info);