Update changes to scripts and AudioSystem #224

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

View File

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

View File

@ -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<PickAndThrow>();
if (pat == null)
Debug.LogError("PickAndThrow is NULL!");
stateMachine = GetComponent<StateMachine>();
if (stateMachine)
{
/* stateMachine = AddScript<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);
}
stateMachine.InitStateMachine(dictionary);*/
}
protected override void update()
@ -132,24 +132,15 @@ public class PlayerController : Script
if (cam == null)
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();
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,15 +208,19 @@ 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;
if(stateMachine && !stateMachine.IsState(typeof(WalkState)))
stateMachine.SetState(typeof(WalkState));
}
if (!isMoveKeyPress && isGrounded)
{
currentState = RaccoonStates.IDLE;
if(stateMachine && !stateMachine.IsState(typeof(IdleState)))
stateMachine.SetState(typeof(IdleState));
}
}
@ -259,6 +255,7 @@ public class PlayerController : Script
if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress && isGrounded)
{
currentState = RaccoonStates.RUNNING;
if (stateMachine && !stateMachine.IsState(typeof(RunState)))
stateMachine.SetState(typeof(RunState));
holdItem = false;
if (!sprintIncreaseOnce)
@ -277,6 +274,7 @@ public class PlayerController : Script
if (isMoveKeyPress)
{
currentState = RaccoonStates.WALKING;
if(stateMachine && !stateMachine.IsState(typeof(WalkState)))
stateMachine.SetState(typeof(WalkState));
}
sprintIncreaseOnce = false;
@ -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<Item>() != null && holdItem)
if (holdItem && pat != null && pat.item.GetScript<Item>() != null)
{
Item item = pat.item.GetScript<Item>();
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,6 +368,7 @@ public class PlayerController : Script
if (currentState == RaccoonStates.CAUGHT && tranform != null)
{
currentState = RaccoonStates.IDLE;
if (stateMachine && !stateMachine.IsState(typeof(IdleState)))
stateMachine.SetState(typeof(IdleState));
tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f);
}
@ -385,22 +384,31 @@ 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
{
public RunState(StateMachine stateMachine) : base(stateMachine)
@ -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");

View File

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

View File

@ -3,14 +3,14 @@ using System;
using System.Collections.Generic;
using System.Linq;
public abstract class StateMachine : BaseComponent
public class StateMachine : Script
{
private Dictionary<Type, BaseState> 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<Type, BaseState> 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);