using System.Collections; using UnityEngine; public class ActiveWeapon : Singleton { public MonoBehaviour CurrentActiveWeapon { get; private set; } // Holds the currently active weapon private PlayerControls playerControls; // Manages player input private float timeBetweenAttacks; // Delay between consecutive attacks private bool attackButtonDown, isAttacking = false; // Flags to manage attack state protected override void Awake() { base.Awake(); playerControls = new PlayerControls(); // Set up player input controls } private void OnEnable() { playerControls.Enable(); // Activate player input controls } private void Start() { // Register callbacks for attack input events playerControls.Combat.Attack.started += _ => StartAttacking(); // Trigger attack start playerControls.Combat.Attack.canceled += _ => StopAttacking(); // Trigger attack stop AttackCooldown(); // Begin cooldown after starting } private void Update() { Attack(); // Process attack based on input and weapon state } // Assign a new weapon as the current active weapon public void NewWeapon(MonoBehaviour newWeapon) { CurrentActiveWeapon = newWeapon; // Update active weapon AttackCooldown(); // Reset cooldown based on new weapon's settings timeBetweenAttacks = (CurrentActiveWeapon as IWeapon).GetWeaponInfo().weaponCooldown; // Set cooldown duration } // Clear the current active weapon public void WeaponNull() { CurrentActiveWeapon = null; } // Start the attack cooldown process private void AttackCooldown() { isAttacking = true; // Set attacking flag to true StopAllCoroutines(); // Abort any ongoing attack routines StartCoroutine(TimeBetweenAttacksRoutine()); // Initiate cooldown coroutine } // Coroutine to handle the attack cooldown private IEnumerator TimeBetweenAttacksRoutine() { yield return new WaitForSeconds(timeBetweenAttacks); // Pause for the cooldown period isAttacking = false; // Reset attacking flag } // Handle the start of an attack private void StartAttacking() { attackButtonDown = true; // Mark attack button as pressed } // Handle the end of an attack private void StopAttacking() { attackButtonDown = false; // Mark attack button as released } // Manage the attack process based on input and weapon status private void Attack() { // Check if the attack button is pressed, not currently attacking, and an active weapon is present if (attackButtonDown && !isAttacking && CurrentActiveWeapon) { AttackCooldown(); // Start cooldown period (CurrentActiveWeapon as IWeapon).Attack(); // Execute attack action } } }