21 auto r = std::make_shared<BTNativeRegistry>();
22 r->RegisterCondition(
"CanSeeTarget", [](
BTContext& ctx) {
24 if (!s || !s->GetEntity().GetScene())
return false;
25 const auto targets=s->GetEntity().GetScene()->GetPerceivedTargets(s->GetEntity().GetHandle());
26 for (
const auto& target : targets)
if (target.IsVisible) {
28 ctx.
Blackboard().
Set(
"TargetPosition", target.LastKnownPosition);
33 r->RegisterCondition(
"HasRememberedTarget", [](
BTContext& ctx) {
35 if (!s || !s->GetEntity().GetScene())
return false;
36 auto last=s->GetEntity().GetScene()->GetLastSeenTarget(s->GetEntity().GetHandle());
37 if (!last)
return false;
41 r->RegisterAction(
"ChaseTarget", [](
BTContext& ctx,
float) {
45 const auto entity = script->GetEntity();
46 auto* scene = entity.GetScene();
53 glm::vec3 destination{};
54 bool hasDestination =
false;
55 if (
const auto* handle = ctx.
Blackboard().
TryGet<std::uint32_t>(
"TargetHandle")) {
56 if (scene->IsValid(*handle)) {
57 for (
const auto& perceived : scene->GetPerceivedTargets(entity.GetHandle())) {
58 if (perceived.EntityHandle == *handle && perceived.IsVisible) {
59 destination = perceived.LastKnownPosition;
61 hasDestination =
true;
68 if (!hasDestination) {
69 if (
const auto* last = ctx.
Blackboard().
TryGet<glm::vec3>(
"TargetPosition")) {
71 hasDestination =
true;
76 const glm::vec3 current = glm::vec3(scene->GetWorldTransform(entity)[3]);
77 glm::vec3 delta = destination - current;
79 const float stoppingDistance = std::max(nav.StoppingDistance, 0.05f);
80 if (glm::dot(delta, delta) <= stoppingDistance * stoppingDistance) {
81 nav.HasDestination =
false;
87 nav.Destination = destination;
88 nav.HasDestination =
true;
90 if (auto* owner = BTGetScript(c); owner && owner->HasComponent<NavAgentComponent>())
91 owner->GetComponent<NavAgentComponent>().HasDestination = false;
97 r->RegisterAction(
"Patrol", [](
BTContext& ctx,
float dt) {
101 auto* scene = script->GetEntity().GetScene();
105 const glm::vec3 position = glm::vec3(scene->GetWorldTransform(script->GetEntity())[3]);
107 if (!bb.Contains(
"PatrolOrigin")) bb.
Set(
"PatrolOrigin", position);
108 const glm::vec3 origin = bb.Get<glm::vec3>(
"PatrolOrigin");
109 const float radius = std::max(bb.GetOr<
float>(
"PatrolRadius", 3.f), 0.25f);
110 const float pause = std::max(bb.GetOr<
float>(
"PatrolWaitSeconds", 0.4f), 0.f);
111 int index = bb.GetOr<
int>(
"PatrolIndex", 0) % 4;
112 if (index < 0) index += 4;
113 const glm::vec3 offsets[4] = {
114 {radius,0.f,0.f}, {radius,0.f,radius}, {0.f,0.f,radius}, {0.f,0.f,0.f}
116 const glm::vec3 destination = origin + offsets[index];
117 glm::vec3 difference = destination - position;
119 const float stoppingDistance = std::max(nav.StoppingDistance, 0.15f);
120 if (glm::dot(difference,difference) <= stoppingDistance*stoppingDistance) {
121 nav.HasDestination =
false;
122 const float elapsed = bb.GetOr<
float>(
"PatrolWaitElapsed", 0.f) + std::max(dt,0.f);
123 if (elapsed >= pause) {
124 bb.Set(
"PatrolIndex", (index+1)%4);
125 bb.Set(
"PatrolWaitElapsed", 0.f);
126 }
else bb.Set(
"PatrolWaitElapsed", elapsed);
128 bb.Set(
"PatrolWaitElapsed", 0.f);
129 nav.Destination = destination;
130 nav.HasDestination =
true;
133 if (auto* owner = BTGetScript(c); owner && owner->HasComponent<NavAgentComponent>())
134 owner->GetComponent<NavAgentComponent>().HasDestination = false;
138 r->RegisterAction(
"StopMoving", [](
BTContext& ctx,
float) {
144 r->RegisterAction(
"MoveForward", [](
BTContext& ctx,
float dt) {
void SetCancellation(BTNodeID id, std::function< void(BTContext &)> fn)
BTBlackboard & Blackboard()
BTNodeID ActiveNode() const noexcept