BITFALL/Assets/Artists/Scripts/AI/AIBehavour.cs

80 lines
1.8 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BITFALL.Player.Movement;
using BITFALL.Props;
using BITKit;
using BITKit.Entities;
using BITKit.Sensors;
using UnityEngine;
namespace BITFALL.AI
{
public class AIBehavour : EntityBehavior,IStunObject
{
[SerializeReference,SubclassSelector] private ISensor sensor;
[SerializeReference, SubclassSelector] private IReference stunAnimation;
private readonly Optional<IEntityMovement> grabTarget=new();
[Inject]
private IHealth _health;
public override void OnStart()
{
base.OnStart();
UnityEntity.AddListener<string>(OnCommand);
_health.OnSetAlive += OnSetAlive;
}
private void OnSetAlive(bool obj)
{
if (obj) return;
if(grabTarget.Allow)
{
grabTarget.Value.ExecuteCommand(new PlayerDisableMovementCommand()
{
LockFile = this,
Disable = false
});
grabTarget.Clear();
}
}
private async void OnCommand(string obj)
{
switch (obj)
{
case "Grapple":
await sensor.Execute();
if (destroyCancellationToken.IsCancellationRequested) return;
var target = sensor.Get().FirstOrDefault();
if (target && target.TryGetComponent<IEntityMovement>(out var movement))
{
movement.ExecuteCommand(new PlayerDisableMovementCommand()
{
LockFile = this,
Disable = true,
Duration = 2,
Source = "<color=yellow>被抓住了</color>"
});
UnityEntity.Invoke(Constant.Animation.Play,"Grapple");
grabTarget.SetValueThenAllow(movement);
}
break;
}
}
public float Weight { get; set; }
public event Action<float> OnWeightChanged;
public void Stun(float distance, float duration)
{
if (stunAnimation is null) return;
UnityEntity.Invoke(Constant.Animation.Play,stunAnimation.Value);
}
}
}