using System.Collections; using UnityEngine; public class PlayerController : Singleton { // Public property to check if the player is facing left public bool FacingLeft { get { return facingLeft; } } // Serialized fields visible in the Unity Inspector [SerializeField] private float moveSpeed = 1f; [SerializeField] private float dashSpeed = 4f; [SerializeField] private TrailRenderer myTrailRenderer; [SerializeField] private Transform weaponCollider; // Private variables for player control and physics private PlayerControls playerControls; private Vector2 movement; private Rigidbody2D rb; private Animator myAnimator; private SpriteRenderer mySpriteRender; private Knockback knockback; private float startingMoveSpeed; // Flags to track player states private bool facingLeft = false; private bool isDashing = false; protected override void Awake() { base.Awake(); // Initialize references playerControls = new PlayerControls(); rb = GetComponent(); myAnimator = GetComponent(); mySpriteRender = GetComponent(); knockback = GetComponent(); } private void Start() { // Subscribe to dash input event and initialize starting move speed playerControls.Combat.Dash.performed += _ => Dash(); startingMoveSpeed = moveSpeed; // Equip starting weapon from inventory ActiveInventory.Instance.EquipStartingWeapon(); } private void OnEnable() { // Enable player controls playerControls.Enable(); } private void OnDisable() { // Disable player controls playerControls.Disable(); } private void Update() { // Handle player input every frame PlayerInput(); } private void FixedUpdate() { // Adjust player facing direction and move player physics AdjustPlayerFacingDirection(); Move(); } public Transform GetWeaponCollider() { // Get the weapon collider transform return weaponCollider; } private void PlayerInput() { // Read movement input movement = playerControls.Movement.Move.ReadValue(); // Set animator parameters based on movement direction myAnimator.SetFloat("moveX", movement.x); myAnimator.SetFloat("moveY", movement.y); } private void Move() { // Move the player based on movement input, considering knockback and death state if (knockback.GettingKnockedBack || PlayerHealth.Instance.isDead) { return; } rb.MovePosition(rb.position + movement * (moveSpeed * Time.fixedDeltaTime)); } private void AdjustPlayerFacingDirection() { // Flip player sprite based on mouse position relative to player position Vector3 mousePos = Input.mousePosition; Vector3 playerScreenPoint = Camera.main.WorldToScreenPoint(transform.position); if (mousePos.x < playerScreenPoint.x) { mySpriteRender.flipX = true; facingLeft = true; } else { mySpriteRender.flipX = false; facingLeft = false; } } private void Dash() { // Initiate dash if not already dashing and sufficient stamina if (!isDashing && Stamina.Instance.CurrentStamina > 0) { Stamina.Instance.UseStamina(); isDashing = true; moveSpeed *= dashSpeed; myTrailRenderer.emitting = true; StartCoroutine(EndDashRoutine()); } } private IEnumerator EndDashRoutine() { // End dash routine: reset speed and trail after dash duration and cooldown float dashTime = .2f; float dashCD = .25f; yield return new WaitForSeconds(dashTime); moveSpeed = startingMoveSpeed; myTrailRenderer.emitting = false; yield return new WaitForSeconds(dashCD); isDashing = false; } }