using System.Collections.Generic; using System.Linq; using UnityEngine; public class PlayerMove : MonoBehaviour { private KeyCode walkForward; private KeyCode walkLeft; private KeyCode walkRight; private KeyCode walkBackward; private KeyCode sprint; public float walkSpeed; public float sprintSpeed; public float timeBeforeSprintRegen; public float stepSoundRate; private float stepSoundTimer; private float movementSpeed; private float stamina = 1; private float timeSinceSprinting = 0.0f; private bool sprinting; private bool inWater; public bool lockCursor; private bool stopMovement; public GameObject playerAnchor; private Rigidbody2D rb; private Health hp; private Animator anim; private AudioSource stepSource; public AudioClip[] steps; public AudioClip[] waterSteps; private List<AudioClip> activeSounds = new List<AudioClip>(); private void Awake() { rb = GetComponent<Rigidbody2D>(); hp = GetComponent<Health>(); anim = GetComponent<Animator>(); stepSource = GetComponent<AudioSource>(); walkForward = InputManager.GetInputKey(0); walkLeft = InputManager.GetInputKey(1); walkRight = InputManager.GetInputKey(2); walkBackward = InputManager.GetInputKey(3); sprint = InputManager.GetInputKey(4); activeSounds = steps.ToList(); } private void Update() { if (lockCursor) { Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; } else { Cursor.lockState = CursorLockMode.None; Cursor.visible = true; } //decide if sprinting or walking if (Input.GetKey(sprint) && stamina > 0.01f && !stopMovement) { movementSpeed = sprintSpeed; sprinting = true; if (Input.GetKey(walkForward) || Input.GetKey(walkBackward) || Input.GetKey(walkLeft) || Input.GetKey(walkRight)) timeSinceSprinting = 0.0f; else timeSinceSprinting += Time.deltaTime; } else { movementSpeed = walkSpeed; sprinting = false; timeSinceSprinting += Time.deltaTime; } } void FixedUpdate() { if (!stopMovement) { float x; float z; if (Input.GetKey(walkLeft)) x = -1; else if (Input.GetKey(walkRight)) x = 1; else x = 0; if (Input.GetKey(walkForward)) z = 1; else if (Input.GetKey(walkBackward)) z = -1; else z = 0; Vector2 upT = new Vector2(playerAnchor.transform.up.x, playerAnchor.transform.up.y); Vector2 rightT = new Vector2(playerAnchor.transform.right.x, playerAnchor.transform.right.y); bool forwardOrBack = false; if (anim.speed != 1) anim.speed = 1; if (z == 0 && x == 0) anim.Play("Player Idle"); if (z != 0) { rb.position += z * upT * Time.fixedDeltaTime * movementSpeed; if (!anim.GetCurrentAnimatorStateInfo(0).IsName("Player Walk")) anim.Play("Player Walk"); if (sprinting) { stepSoundTimer += Time.fixedDeltaTime; if (anim.GetCurrentAnimatorStateInfo(0).IsName("Player Walk")) anim.speed = 2; ModifyStamina(-0.001f); } else stepSoundTimer += Time.fixedDeltaTime / 2; forwardOrBack = true; } if (x != 0) { rb.position += x * rightT * Time.fixedDeltaTime * movementSpeed; if (!anim.GetCurrentAnimatorStateInfo(0).IsName("Player Walk")) anim.Play("Player Walk"); if (sprinting && forwardOrBack == false) { stepSoundTimer += Time.fixedDeltaTime; if (anim.GetCurrentAnimatorStateInfo(0).IsName("Player Walk")) anim.speed = 2; ModifyStamina(-0.001f); } else if (forwardOrBack == false) stepSoundTimer += Time.fixedDeltaTime / 2; } if (stepSoundTimer >= stepSoundRate) { stepSource.clip = activeSounds[Random.Range(0, activeSounds.Count - 1)]; stepSource.pitch = Random.Range(0.85f, 1.15f); stepSource.Play(); stepSoundTimer = 0.0f; } if (timeSinceSprinting >= timeBeforeSprintRegen && stamina < 1.0f) { ModifyStamina(0.003f); } } } public void SetMovementState(bool state) { stopMovement = state; } public void SetCursorLockState(bool state) { lockCursor = state; } public void ModifyStamina(float amt) { if ((amt > 0 && stamina < 1.0f) || (amt < 0 && stamina > 0.0f)) stamina += amt; if (amt < 0 && stamina <= 0.0f) { hp.ModifyCurrentHealth(amt); } } public void SetTimeSinceSprinting(float value) { timeSinceSprinting = value; } public float GetPlayerStamina() { return stamina; } public void SetNewKey(int ID) { switch (ID) { default: walkForward = InputManager.GetInputKey(0); break; case 0: walkForward = InputManager.GetInputKey(0); break; case 1: walkLeft = InputManager.GetInputKey(1); break; case 2: walkRight = InputManager.GetInputKey(2); break; case 3: walkBackward = InputManager.GetInputKey(3); break; case 4: sprint = InputManager.GetInputKey(4); break; } } public void SetInWater(bool state) { inWater = state; if (inWater == false && activeSounds != steps.ToList()) activeSounds = steps.ToList(); else if (inWater == true && activeSounds != waterSteps.ToList()) activeSounds = waterSteps.ToList(); } public void SetMovementSpeed(float newWalkSpeed, float newSprintSpeed) { walkSpeed = newWalkSpeed; sprintSpeed = newSprintSpeed; } }