using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; public class WeaponScript : MonoBehaviour { public List<WeaponBehaviour> weapons = new List<WeaponBehaviour>(); [SerializeField] private float offset; [HideInInspector] public int currentWeapon = 0; [HideInInspector] public StatusEffects se; [HideInInspector] public Rigidbody rb; List<float> coolDowns = new List<float>(); SoundsManager sM; AudioSource audioSource; private void Start() { audioSource = GetComponent<AudioSource>(); rb = GetComponent<Rigidbody>(); se = GetComponent<StatusEffects>(); foreach(WeaponBehaviour w in weapons) { w.shooted= false; coolDowns.Add(0); } GameObject t = GameObject.Find("AudioManager"); if(t) sM = t.GetComponent<SoundsManager>(); } private void Update() { for(int i = 0; i < coolDowns.Count; i++) { if (coolDowns[i] >= 0) { coolDowns[i] -= Time.deltaTime; } } } public void ChangeWeapon(float weapon) { if(weapon < 0) { if (currentWeapon < weapons.Count - 1) currentWeapon++; else currentWeapon = 0; } else { if (currentWeapon > 0) currentWeapon--; else currentWeapon = weapons.Count - 1; } } public void Shoot(Vector3 direction) { if(coolDowns[currentWeapon] <= 0 && !weapons[currentWeapon].shooted) { WeaponBehaviour w = weapons[currentWeapon]; float toShootAngle = Vector3.Angle(Vector3.right, direction); if (direction.y < 0) toShootAngle = -toShootAngle; toShootAngle -= w.numberOfBulletsSpawned * w.angleBetweenBullets / 2; for (int i = 0; i < w.numberOfBulletsSpawned; i++) { Vector3 dir = (Quaternion.Euler(0, 0, toShootAngle) * Vector3.right); toShootAngle += w.angleBetweenBullets; GameObject bullet = Instantiate(w.bulletPrefab, transform.position + (dir * offset), Quaternion.identity); bullet.GetComponent<Rigidbody>().velocity = dir * w.bulletVelocity; IAmmunitonHandler ammo = bullet.GetComponent<IAmmunitonHandler>(); if (sM) sM.PlaySoundEffect(weapons[currentWeapon].shootingSound, audioSource); if(ammo != null) ammo.ReadyBullet(se.damage * w.damageMultiplier, gameObject); } coolDowns[currentWeapon] = w.coolDown; if (w.onlyShot) { w.shooted = true; } } } }