Untitled

mail@pastecode.io avatar
unknown
csharp
a year ago
2.0 kB
4
Indexable
Never
using System;
using Sirenix.OdinInspector;
using Studio.Collections;
using Studio.Data;
using Studio.Interfaces;
using UnityEngine;

namespace Studio.Controllers
{
    public class DroneController : MonoBehaviour, IHaveID
    {
        [field: SerializeField] public DroneID ID { get; set; }
        
        public ControlType ControlType => _controlType; 

        private ILauncher _iLauncher;
        private IDroneAction _iDroneAction;

        [ReadOnly] [SerializeField] private ControlType _controlType;
        
        public Action OnBulletLaunchedEvent { get; set; }

        private void OnDisable()
        {
            if (_iDroneAction != null)
                _iDroneAction.OnNormalShotTriggerEvent -= FireBullet;
        }

        /// <summary>
        /// Inject the event 
        /// </summary>
        /// <param name="droneAction"></param>
        public void Initialize(IDroneAction droneAction)
        {
            droneAction.OnNormalShotTriggerEvent += FireBullet;
            _iDroneAction = GetComponent<IDroneAction>();
            _iDroneAction = droneAction; // cache for future disabling.
        }
        public void SetControlType(ControlType controlType)
        {
            _controlType = controlType;
        }

        public void SetLauncherStats(DroneData droneData, int damage)
        {
            _iLauncher = GetComponent<ILauncher>();
            _iLauncher.Damage = damage;
            _iLauncher.BulletSpeed = droneData.BulletSpeed;
            _iLauncher.ReloadTime = droneData.ReloadTime;
            _iLauncher.AmmoClipSize = droneData.AmmoClipSize;
            _iLauncher.ShotInterval = droneData.ShotInterval;
        }

        /// <summary>
        /// Call to fire a shot. Subscribing to IDroneAction events.
        /// </summary>
        [Button]
        public void FireBullet()
        {
            _iLauncher.Launch();
            OnBulletLaunchedEvent?.Invoke();
        }
    }
}