using System.Collections; using UnityEngine; public class ActiveWeapon : Singleton { public MonoBehaviour CurrentActiveWeapon { get; private set; } // Current active weapon private PlayerControls playerControls; // Player input controls private float timeBetweenAttacks; // Time delay between attacks private bool attackButtonDown, isAttacking = false; // Flags for attack state protected override void Awake() { base.Awake(); playerControls = new PlayerControls(); // Initialize PlayerControls } private void OnEnable() { playerControls.Enable(); // Enable player input controls } private void Start() { // Subscribe to attack events from player input controls playerControls.Combat.Attack.started += _ => StartAttacking(); playerControls.Combat.Attack.canceled += _ => StopAttacking(); AttackCooldown(); // Start initial attack cooldown } private void Update() { Attack(); // Check for attack input and initiate attack } // Set a new weapon as the current active weapon public void NewWeapon(MonoBehaviour newWeapon) { CurrentActiveWeapon = newWeapon; // Assign the new weapon AttackCooldown(); // Start attack cooldown based on weapon's cooldown time timeBetweenAttacks = (CurrentActiveWeapon as IWeapon).GetWeaponInfo().weaponCooldown; } // Set the current active weapon to null public void WeaponNull() { CurrentActiveWeapon = null; } // Initiate attack cooldown process private void AttackCooldown() { isAttacking = true; // Set attacking flag StopAllCoroutines(); // Stop any ongoing attack routines StartCoroutine(TimeBetweenAttacksRoutine()); // Start attack cooldown routine } // Coroutine for attack cooldown private IEnumerator TimeBetweenAttacksRoutine() { yield return new WaitForSeconds(timeBetweenAttacks); // Wait for cooldown time isAttacking = false; // Reset attacking flag } // Callback when attack input is started private void StartAttacking() { attackButtonDown = true; // Set attack button pressed flag } // Callback when attack input is stopped private void StopAttacking() { attackButtonDown = false; // Reset attack button pressed flag } // Process attacking based on player input and weapon state private void Attack() { // Check if attack button is pressed, not currently attacking, and there is an active weapon if (attackButtonDown && !isAttacking && CurrentActiveWeapon) { AttackCooldown(); // Start attack cooldown (CurrentActiveWeapon as IWeapon).Attack(); // Execute attack through active weapon } } }