BITFALL/Assets/BITKit/UnityPluginsSupport/NodeCanvas/EntityBehaviourTree.cs

55 lines
1.1 KiB
C#
Raw Normal View History

2023-08-12 01:43:24 +08:00
using System.Collections;
using System.Collections.Generic;
using BITKit.Entities;
using NodeCanvas.BehaviourTrees;
using NodeCanvas.Framework;
using UnityEngine;
namespace BITKit
{
2023-10-20 19:31:12 +08:00
public class EntityBehaviourTree :EntityComponent,IEntityOverrideCallback
2023-08-12 01:43:24 +08:00
{
public BehaviourTreeOwner behaviourTree;
[SerializeField] private Blackboard blackboard;
private readonly ValidHandle _allow=new();
2023-10-20 19:31:12 +08:00
[Inject] private IHealth _health;
2023-08-12 01:43:24 +08:00
public override void OnAwake()
{
entity.RegisterCallback<IEntityOverrideCallback>(this);
_allow.AddListener(OnAllow);
2023-10-20 19:31:12 +08:00
_health.OnSetAlive += OnSetAlive;
_health.OnSetHealthPoint += OnSetHP;
2023-08-12 01:43:24 +08:00
}
public void OnSetAlive(bool alive)
{
_allow.SetElements(this,alive);
blackboard.SetVariableValue("IsAlive", alive);
}
public void OnSetHP(int hp)
{
blackboard.SetVariableValue("HP", hp);
}
public void OnEntryOverride(bool @override)
{
_allow.SetDisableElements(this,@override);
}
private void OnAllow(bool allow)
{
if (allow)
{
behaviourTree.StartBehaviour();
}
else
{
behaviourTree.StopBehaviour();
}
}
}
}