lucid-super-dream/Assets/Scripts/FireAtTargets.cs

66 lines
1.7 KiB
C#
Raw Normal View History

2021-01-05 18:13:29 +01:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using Utils;
using Weapons.Scripts;
public class FireAtTargets : MonoBehaviour
{
[SerializeField] private Weapon weapon;
[SerializeField] private ObjectPool<Transform> bulletPool;
[SerializeField] private Transform bulletPos;
[SerializeField] private UnityEvent OnFire;
[SerializeField] private Vector3Event OnBulletCollide;
2021-01-06 16:10:11 +01:00
[SerializeField] private int numTargets = 4;
2021-01-05 18:13:29 +01:00
private void Awake()
{
weapon = Instantiate(weapon);
weapon.Init();
UpdateManager.OnUpdate += weapon.Update;
UpdateManager.OnFixedUpdate += weapon.FixedUpdate;
UpdateManager.OnLateUpdate += weapon.LateUpdate;
weapon.BulletCollision += BulletCollide;
}
private void OnDestroy()
{
UpdateManager.OnUpdate -= weapon.Update;
UpdateManager.OnFixedUpdate -= weapon.FixedUpdate;
UpdateManager.OnLateUpdate -= weapon.LateUpdate;
weapon.BulletCollision -= BulletCollide;
}
private void BulletCollide(Vector3 pos)
{
OnBulletCollide?.Invoke(pos);
}
public void Fire(List<Transform> targets)
{
FireIE(targets).Run();
}
private IEnumerator FireIE(List<Transform> targets)
{
var currentTargets = new List<Transform>(targets);
2021-01-06 16:10:11 +01:00
for (var i = 0; i < currentTargets.Count; i++)
2021-01-05 18:13:29 +01:00
{
2021-01-06 16:10:11 +01:00
bulletPos.LookAt(currentTargets[i]);
2021-01-05 18:13:29 +01:00
if (weapon.Fire(bulletPool, bulletPos))
OnFire?.Invoke();
2021-01-06 16:10:11 +01:00
if (i % numTargets == 0)
yield return null;
2021-01-05 18:13:29 +01:00
}
}
private void OnDrawGizmos()
{
weapon.DrawGizmos(bulletPos);
}
}