using System; using System.Collections; using System.Collections.Generic; using System.Security.Cryptography; using UnityEngine; public class Player : MonoBehaviour { //player movment private Rigidbody2D body; private const int BASE_SPEED = 5; private Vector3 SavePoint = Vector3.zero; [SerializeField] private int minKeys; //gun public Gun gun; //points private int keys; //abilities //dash private bool canDash = true; private int dashAmount = 0; private int dashesCount = 0; private const int dashSpeed = 5; private float dashDuration = 0.5f; private float dashTime; private int dashCooldownTime = 10; private float dashCoolDownTimeLeft = 10; Vector2 MousePosition; // Start is called before the first frame update void Start() { body = GetComponent(); SavePoint = transform.position; } // Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { gun.Fire(); } MousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); } private void FixedUpdate() { //Rotating the player Vector2 aimDirection = MousePosition - (Vector2)transform.position; float aimAngle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg - 90f; body.rotation = aimAngle; // Reset canDash and dashTime when dash key is released if (!Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.RightShift)) { if (dashTime > 0) // Check if a dash was in progress { canDash = false; dashesCount--; dashTime = 0; } else { canDash = true; dashTime = 0; } } // Dashing logic if ((Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && dashesCount > 0 && dashTime < dashDuration && canDash) { // Set the velocity for dashing in all directions body.velocity = new Vector2(Input.GetAxis("Horizontal") * BASE_SPEED * dashSpeed, Input.GetAxis("Vertical") * BASE_SPEED * dashSpeed); dashTime += Time.deltaTime; } else { // Normal movement when not dashing body.velocity = new Vector2(Input.GetAxis("Horizontal") * BASE_SPEED, Input.GetAxis("Vertical") * BASE_SPEED); } // Reset dash state and decrement dash count when dash duration is exceeded if (dashTime >= dashDuration) { canDash = false; dashesCount--; dashTime = 0; Debug.Log(dashesCount); } if (dashesCount < dashAmount) { dashCoolDownTimeLeft -= Time.deltaTime; if (dashCoolDownTimeLeft < 0) { dashesCount++; dashCoolDownTimeLeft = 0; } } else { dashCoolDownTimeLeft = dashCooldownTime; } Debug.Log(dashesCount); } private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Death")) { transform.position = SavePoint; } } private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.CompareTag("Key")) { Destroy(collision.gameObject); keys++; } else if (collision.gameObject.CompareTag("Dash")) { Destroy(collision.gameObject); dashAmount++; dashesCount = dashAmount; } else if (collision.gameObject.CompareTag("FinishLine") && keys >= minKeys) { Debug.Log("Yes You win"); } } }