using System; using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; using BITFALL.Bullet; using BITFALL.Entities.Equipment; using BITKit; using BITKit.Entities; using BITKit.StateMachine; using Unity.Mathematics; using UnityEngine; namespace BITFALL.Guns { public class AIGunController : AIEquipController { [SerializeField] private Transform firePoint; [SerializeReference,SubclassSelector] private IBulletService bulletService; [SerializeField] private bool forceFire; [SerializeField] private Optional customFireRate; [SerializeField] private Optional customFireInterval; private AssetableGun _gun=>assetableItem as AssetableGun; private readonly IntervalUpdate fireInterval = new(); public override void Entry() { base.Entry(); fireInterval.Interval = _gun.FireMode.FireRate is 0 ? 1 : 1f/_gun.FireMode.FireRate; if (customFireRate.Allow) { fireInterval.Interval =customFireRate.Value is 0 ? 1 : 1f / customFireRate.Value; } UnityEntity.AddListener(OnAttack); } public override void Exit() { base.Exit(); UnityEntity.RemoveListener(OnAttack); } public override void OnUpdate(float deltaTime) { if (forceFire && fireInterval.AllowUpdate) { if(customFireInterval.Allow) { if (customFireInterval.Value.AllowUpdate) { OnAttack(new BITConstant.Command.AttackCommand()); } } else { OnAttack(new BITConstant.Command.AttackCommand()); } } } private void OnAttack(BITConstant.Command.AttackCommand obj) { bulletService.Spawn(new SpawnBullet() { forward = firePoint.forward, initialDamage = _gun.InitialDamage, initiator = Entity.Id, pos = firePoint.position, rot = firePoint.rotation, startSpeed = _gun.InitialBulletSpeed, InitialForce = _gun.InitialBulletForce, }); UnityEntity.Invoke(Constant.Animation.Play, BITConstant.Player.Fire); } } }