BITFALL/Assets/Artists/Scripts/Equip/AIGunController.cs

77 lines
2.0 KiB
C#

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<int> customFireRate;
[SerializeField] private Optional<IntervalUpdate> 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<BITConstant.Command.AttackCommand>(OnAttack);
}
public override void Exit()
{
base.Exit();
UnityEntity.RemoveListener<BITConstant.Command.AttackCommand>(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);
}
}
}