2024-04-19 00:40:34 +08:00
|
|
|
using System;
|
2024-04-16 04:39:26 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using BITFALL.AI.States;
|
2024-04-19 00:40:34 +08:00
|
|
|
using BITFALL.Entities.Equipment;
|
2024-04-16 04:39:26 +08:00
|
|
|
using BITKit;
|
|
|
|
using BITKit.AI.States;
|
|
|
|
using BITKit.Entities;
|
|
|
|
using BITKit.Sensors;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.AI;
|
|
|
|
using UnityEngine.PlayerLoop;
|
|
|
|
|
|
|
|
namespace BITFALL.AI
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// AI服务
|
|
|
|
/// </summary>
|
|
|
|
[CustomType(typeof(AIService))]
|
|
|
|
public class AIService : StateBasedBehavior<AI_Base>
|
|
|
|
{
|
|
|
|
[SerializeReference,SubclassSelector] private ITicker ticker;
|
|
|
|
[SerializeField] private NavMeshAgent agent;
|
|
|
|
[SerializeField] private RangeSensor rangeSensor;
|
|
|
|
[SerializeField] private AudioSensor audioSensor;
|
|
|
|
[SerializeField] private Tag selfTag;
|
|
|
|
[SerializeField] private Tag friendTag;
|
|
|
|
[SerializeField] private Tag enemyTag;
|
|
|
|
|
|
|
|
public IEntity CombatTarget { get; set; }
|
|
|
|
|
|
|
|
public RangeSensor RangeSensor => rangeSensor;
|
|
|
|
public AudioSensor AudioSensor => audioSensor;
|
|
|
|
public NavMeshAgent Agent => agent;
|
|
|
|
public ITag SelfTag => selfTag;
|
|
|
|
public ITag FriendTag => friendTag;
|
|
|
|
public ITag EnemyTag => enemyTag;
|
2024-04-19 00:40:34 +08:00
|
|
|
public IHealth Health => _health;
|
|
|
|
[Inject] private IHealth _health;
|
|
|
|
private event Action<float> tick;
|
|
|
|
public void RegisterTick(Action<float> action)
|
|
|
|
{
|
|
|
|
tick += action;
|
|
|
|
}
|
|
|
|
public void UnRegisterTick(Action<float> action)
|
|
|
|
{
|
|
|
|
tick -= action;
|
|
|
|
}
|
2024-04-16 04:39:26 +08:00
|
|
|
public override void OnStart()
|
|
|
|
{
|
|
|
|
base.OnStart();
|
|
|
|
ticker.Add(OnTick);
|
2024-04-19 00:40:34 +08:00
|
|
|
TransitionState<States.Idle>();
|
|
|
|
|
|
|
|
Health.OnSetAlive += OnSetAlive;
|
|
|
|
}
|
|
|
|
private void OnSetAlive(bool obj)
|
|
|
|
{
|
|
|
|
RangeSensor.enabled = obj;
|
|
|
|
audioSensor.enabled = obj;
|
2024-04-16 04:39:26 +08:00
|
|
|
}
|
|
|
|
private void OnTick(float obj)
|
|
|
|
{
|
2024-04-19 00:40:34 +08:00
|
|
|
if (Health.IsAlive is false) return;
|
2024-04-16 04:39:26 +08:00
|
|
|
UpdateState(obj);
|
2024-04-19 00:40:34 +08:00
|
|
|
tick?.Invoke(obj);
|
2024-04-16 04:39:26 +08:00
|
|
|
}
|
|
|
|
public override void OnDestroyComponent()
|
|
|
|
{
|
|
|
|
base.OnDestroyComponent();
|
|
|
|
ticker.Remove(OnTick);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|