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; public CameraArm camArm { get; set; } private PickAndThrow pat; private StateMachine stateMachine; public bool holdItem { get; set; } public bool isAiming { get; set; } [Tooltip("The game object for where the player will respawn to")] public GameObject respawnPoint; private float delayTimer = 0.0f; [Tooltip("The current state fo the raccoon")] public RaccoonStates currentState = RaccoonStates.IDLE; //Movement variables============================================================ [Tooltip("Max vel for walking")] public float maxMoveVel = 3.0f; [Tooltip("how much force is apply for walking")] public float moveForce = 50.0f; [Tooltip("increase the moveForce and maxMoveVel by its amt")] public float sprintMultiplier = 1.5f; private float oldForce; private float maxOldVel; private bool sprintIncreaseOnce = false; public Vector2 axisMove { get; set; } public bool isMoveKeyPress { get; set; } [Tooltip("How fast player will turn")] public float rotationFactorPerFrame = 5.0f; //Jumping vars================================================================== [Tooltip("max height of the jump")] public float maxJumpHeight = 1.0f; [Tooltip("max amt of time it will take for the jump")] public float maxJumpTime = 0.5f; [Tooltip("increase gravity when falling")] public float fallMultipler = 3.0f; private float initialJumpVel; private bool isGrounded = true; private float gravity = -9.8f; private float groundGravity = -0.5f; //ItemMultipler================================================================== [Tooltip("How light item will affect player jump")] public float lightMultiper = 0.75f; [Tooltip("How medium item will affect player jump")] public float mediumMultiper = 0.5f; [Tooltip("How heavy item will affect player jump")] public float heavyMultiper = 0.25f; protected override void awake() { //default setup isMoveKeyPress = false; holdItem = false; isAiming = false; //Jump setup float timeToApex = maxJumpTime / 2; gravity = (-2 * maxJumpHeight) / MathF.Pow(timeToApex, 2); initialJumpVel = (2 * maxJumpHeight) / timeToApex; //rigidbody check rb = GetComponent(); if (!rb) Debug.LogError("RigidBody is NULL!"); //Transform check tranform = GetComponent(); if(!tranform) Debug.LogError("tranform is NULL!"); stateMachine = AddScript(); Dictionary dictionary = new Dictionary(); dictionary.Add(typeof(PlayerIdleState), new PlayerIdleState(stateMachine)); dictionary.Add(typeof(PlayerWalkState), new PlayerWalkState(stateMachine)); dictionary.Add(typeof(PlayerRunState), new PlayerRunState(stateMachine)); dictionary.Add(typeof(PlayerJumpState), new PlayerJumpState(stateMachine)); dictionary.Add(typeof(PlayerFallState), new PlayerFallState(stateMachine)); dictionary.Add(typeof(PlayerLandState), new PlayerLandState(stateMachine)); dictionary.Add(typeof(PlayerCaughtState), new PlayerCaughtState(stateMachine)); stateMachine.InitStateMachine(dictionary); } protected override void lateUpdate() { //rb.FreezePositionY = false; } protected override void update() { if (delayTimer <= 1) delayTimer += Time.DeltaTimeF; if (delayTimer < 1) { if (tranform && respawnPoint && rb) { rb.LinearVelocity = Vector3.Zero; tranform.LocalPosition = respawnPoint.GetComponent().LocalPosition; } } else { rb.FreezePositionY = false; } //PickAndThrow check if (!pat) { pat = GetScript(); if(!pat) Debug.LogError("PickAndThrow is NULL!"); } if (!cam) cam = GetComponentInChildren(); if(!camArm) camArm = GetComponentInChildren(); Rotation(); 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() { 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(PlayerWalkState))) stateMachine.SetState(typeof(PlayerWalkState)); } if (!isMoveKeyPress && isGrounded) { currentState = RaccoonStates.IDLE; if(stateMachine && !stateMachine.IsState(typeof(PlayerIdleState))) stateMachine.SetState(typeof(PlayerIdleState)); } } private void Move() { if (rb != null) { rb.LinearVelocity += new Vector3(axisMove.x * moveForce, 0.0f, axisMove.y * moveForce) * Time.DeltaTimeF; if (isMoveKeyPress && 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(PlayerRunState))) stateMachine.SetState(typeof(PlayerRunState)); 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(PlayerWalkState))) stateMachine.SetState(typeof(PlayerWalkState)); } 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() { if (isMoveKeyPress && tranform && !isAiming) { Quaternion currentRotation = tranform.LocalRotation; Quaternion targetRotation = Quaternion.LookRotation(new Vector3(axisMove.x, 0.0f, axisMove.y), new Vector3(0.0f, 1.0f, 0.0f)); tranform.LocalRotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * (float)Time.DeltaTime); } else if (camArm && tranform && isAiming) { Quaternion currentRotation = tranform.LocalRotation; Quaternion targetRotation = Quaternion.Euler(0.0f, SHADE.Math.DegreesToRadians(camArm.Yaw + 180.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 && respawnPoint) { currentState = RaccoonStates.IDLE; if (stateMachine && !stateMachine.IsState(typeof(PlayerIdleState))) stateMachine.SetState(typeof(PlayerIdleState)); tranform.LocalPosition = respawnPoint.GetComponent().LocalPosition; } } protected override void onCollisionEnter(CollisionInfo info) { } }