using SHADE; using System; using System.Collections.Generic; using static Item; public class PlayerController : Script { public enum RaccoonStates { IDLE, WALKING, RUNNING, JUMP, FALLING, LANDED, CAUGHT, TOTAL } public enum WalkingState { CARRY, AIMING, THROW, WALK, TOTAL } public RigidBody rb { get; set; } private Transform tranform; private Camera cam; private PickAndThrow pat; private StateMachine stateMachine; public bool holdItem { get; set; } [Tooltip("The current state fo the raccoon")] public RaccoonStates currentState = RaccoonStates.IDLE; //Movement variables============================================================ [Tooltip("Max vel for walking")] public float maxMoveVel = 2.0f; [Tooltip("how much force is apply for walking")] public float moveForce = 2.0f; [Tooltip("increase the moveForce and maxMoveVel by its amt")] public float sprintMultiplier = 2.0f; private float oldForce; private float maxOldVel; private bool sprintIncreaseOnce = false; public float xAxisMove { get; set; } public float zAxisMove { get; set; } public Vector2 axisMove { get; set; } public bool isMoveKeyPress { get; set; } [Tooltip("curr not working")] public float rotationFactorPerFrame = 1.0f; //Jumping vars================================================================== [SerializeField] [Tooltip("max height of the jump")] public float maxJumpHeight = 4.0f; [SerializeField] [Tooltip("max amt of time it will take for the jump")] public float maxJumpTime = 0.75f; [SerializeField] [Tooltip("increase gravity when falling")] public float fallMultipler = 2.0f; private float initialJumpVel; private bool isGrounded = true; private float gravity = -9.8f; private float groundGravity = -0.5f; //ItemMultipler================================================================== public float lightMultiper = 0.75f; public float mediumMultiper = 0.5f; public float heavyMultiper = 0.25f; protected override void awake() { //default setup isMoveKeyPress = false; holdItem = false; //Jump setup float timeToApex = maxJumpTime / 2; gravity = (-2 * maxJumpHeight) / MathF.Pow(timeToApex, 2); initialJumpVel = (2 * maxJumpHeight) / timeToApex; //rigidbody check rb = GetComponent(); if (rb == null) Debug.LogError("RigidBody is NULL!"); else { rb.IsGravityEnabled = false; rb.Interpolating = false; } //Transform check tranform = GetComponent(); if(tranform == null) Debug.LogError("tranform is NULL!"); //PickAndThrow check pat = GetScript(); if (pat == null) Debug.LogError("PickAndThrow is NULL!"); 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() { if (cam == null) cam = GetComponentInChildren(); GotCaught(); //Debug.Log($"{currentState}"); //Debug.Log($" axisX: {axisMove.x} axisY:{axisMove.y}"); //Debug.Log($"X: {rb.LinearVelocity.x}" + $" Z: {rb.LinearVelocity.z}"); //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(); Gravity(); //Debug.Log($"X: {rb.LinearVelocity.x}" + $" Z: {rb.LinearVelocity.z}"); } private void MoveKey() { axisMove = Vector2.Zero; if (Input.GetKey(Input.KeyCode.W)) { Vector3 camerAixs = cam.GetForward(); camerAixs.y = 0; camerAixs.Normalise(); axisMove += new Vector2(camerAixs.x, camerAixs.z); } if (Input.GetKey(Input.KeyCode.S)) { Vector3 camerAixs = cam.GetForward(); camerAixs.y = 0; camerAixs.Normalise(); axisMove -= new Vector2(camerAixs.x, camerAixs.z); } if (Input.GetKey(Input.KeyCode.A)) { Vector3 camerAixs = cam.GetRight(); camerAixs.y = 0; camerAixs.Normalise(); axisMove -= new Vector2(camerAixs.x, camerAixs.z); } if (Input.GetKey(Input.KeyCode.D)) { Vector3 camerAixs = cam.GetRight(); camerAixs.y = 0; camerAixs.Normalise(); axisMove += new Vector2(camerAixs.x, camerAixs.z); } axisMove.Normalise(); isMoveKeyPress = axisMove.x != 0 || axisMove.y != 0; 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)); } } private void Move() { if (rb != null) { rb.LinearVelocity += new Vector3(axisMove.x * moveForce, 0.0f, axisMove.y * moveForce) * Time.DeltaTimeF; if (isMoveKeyPress) { if (rb) { Vector3 velNor = rb.LinearVelocity; velNor.y = 0.0f; if (velNor.GetMagnitude() > maxMoveVel) { velNor.Normalise(); velNor *= maxMoveVel; rb.LinearVelocity = new Vector3(velNor.x, rb.LinearVelocity.y, velNor.z); } } } } } private void Sprint() { 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) { sprintIncreaseOnce = true; oldForce = moveForce; moveForce *= sprintMultiplier; maxOldVel = maxMoveVel; maxMoveVel *= sprintMultiplier; } } if (Input.GetKeyUp(Input.KeyCode.LeftShift)) { if (isMoveKeyPress) { currentState = RaccoonStates.WALKING; if(stateMachine && !stateMachine.IsState(typeof(WalkState))) stateMachine.SetState(typeof(WalkState)); } sprintIncreaseOnce = false; moveForce = oldForce; maxMoveVel = maxOldVel; } } //press and hold jump private void Jump() { if (currentState == RaccoonStates.WALKING || currentState == RaccoonStates.RUNNING || currentState == RaccoonStates.IDLE) { if (Input.GetKeyDown(Input.KeyCode.Space) && isGrounded && rb != null) { currentState = RaccoonStates.JUMP; Vector3 v = rb.LinearVelocity; v.y = initialJumpVel * 0.5f; if (holdItem && pat != null && pat.item.GetScript() != null) { Item item = pat.item.GetScript(); if (item != null && item.currCategory == ItemCategory.LIGHT) v.y *= lightMultiper; if (item != null && item.currCategory == ItemCategory.MEDIUM) v.y *= mediumMultiper; if (item != null && item.currCategory == ItemCategory.HEAVY) v.y *= heavyMultiper; } rb.LinearVelocity = v; } } if(!isGrounded && rb != null && (rb.LinearVelocity.y < 0.0f || Input.GetKeyUp(Input.KeyCode.Space))) currentState = RaccoonStates.FALLING; } private void Rotation() { Vector3 poitionToLookAt; poitionToLookAt.x = axisMove.x; poitionToLookAt.y = 0.0f; poitionToLookAt.z = axisMove.y; if (tranform != null) { Quaternion currentRotation = tranform.LocalRotation; if (currentState == RaccoonStates.WALKING || currentState == RaccoonStates.RUNNING) { Quaternion targetRotation = Quaternion.LookRotation(poitionToLookAt, new Vector3(0.0f, 1.0f, 0.0f)); tranform.LocalRotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * (float)Time.DeltaTime); } } } private void Gravity() { if (rb != null) { //check player vel.y if its close to zero its on the ground if (SHADE.Math.CompareFloat(rb.LinearVelocity.y, 0.0f)) { isGrounded = true; if (currentState == RaccoonStates.FALLING) currentState = RaccoonStates.LANDED; } else isGrounded = false; Vector3 v = rb.LinearVelocity; if (isGrounded) v.y = groundGravity; else if (currentState == RaccoonStates.FALLING) { float prevYVel = v.y; float newYVel = v.y + (gravity * fallMultipler * (float)Time.DeltaTime); float nextYVel = (prevYVel + newYVel) * 0.5f; v.y = nextYVel; } else { float prevYVel = v.y; float newYVel = v.y + (gravity * (float)Time.DeltaTime); float nextYVel = (prevYVel + newYVel) * 0.5f; v.y = nextYVel; } rb.LinearVelocity = v; } } private void GotCaught() { 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); } } protected override void onCollisionEnter(CollisionInfo info) { } } public class WalkState : BaseState { public WalkState(StateMachine stateMachine) : base(stateMachine) { stateName = "Walk State"; } public override void OnEnter() { //Debug.Log("WALK ENTER"); } 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) { stateName = "Run State"; } public override void OnEnter() { //Debug.Log("Run ENTER"); } public override void update() { //Debug.Log("RUNING"); } public override void fixedUpdate() { //Debug.Log("FIXED RUNNING"); } public override void OnExit() { //Debug.Log("Run EXIT"); } } public class IdleState : BaseState { public IdleState(StateMachine stateMachine) : base(stateMachine) { stateName = "Idle State"; } public override void OnEnter() { //Debug.Log("IDLE ENTER"); } public override void update() { //Debug.Log("IDLING"); } public override void fixedUpdate() { //Debug.Log("FIXED IDLING"); } public override void OnExit() { //Debug.Log("IDLE EXIT"); } }