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

61 lines
1.6 KiB
C#
Raw Normal View History

2021-01-05 13:10:20 +01:00
using UnityEngine;
using UnityEngine.Events;
using Weapons.Scripts;
public class WeaponHandler : MonoBehaviour
{
[SerializeField] private ObjectPool<Transform> bulletPool;
[SerializeField] private Weapon weapon;
[SerializeField] private Transform bulletPos;
[SerializeField] private ShootInputBase input;
[SerializeField] private UnityEvent OnFire;
[SerializeField] private Vector3Event OnBulletCollide;
2021-01-09 17:18:29 +01:00
2021-01-05 13:10:20 +01:00
private bool _canShoot = false;
private void Awake()
{
weapon = Instantiate(weapon);
weapon.Init();
UpdateManager.OnUpdate += weapon.Update;
UpdateManager.OnFixedUpdate += weapon.FixedUpdate;
UpdateManager.OnLateUpdate += weapon.LateUpdate;
bulletPool.Initialised += SceneChangerOnFinishedLoading;
weapon.BulletCollision += BulletCollide;
}
private void OnDestroy()
{
UpdateManager.OnUpdate -= weapon.Update;
UpdateManager.OnFixedUpdate -= weapon.FixedUpdate;
UpdateManager.OnLateUpdate -= weapon.LateUpdate;
bulletPool.Initialised -= SceneChangerOnFinishedLoading;
weapon.BulletCollision -= BulletCollide;
}
private void BulletCollide(Vector3 pos)
{
OnBulletCollide?.Invoke(pos);
}
private void SceneChangerOnFinishedLoading()
{
_canShoot = true;
}
private void Update()
{
if (!_canShoot) return;
if (!input.IsShooting()) return;
if (weapon.Fire(bulletPool, bulletPos))
OnFire?.Invoke();
}
2021-01-09 17:18:29 +01:00
2021-01-05 13:10:20 +01:00
private void OnDrawGizmos()
{
weapon.DrawGizmos(bulletPos);
}
}