From b691c43c44ff180d10e7d4a9afc83b98551ff586 Mon Sep 17 00:00:00 2001 From: daoge_cmd <3523206925@qq.com> Date: Sun, 1 Mar 2026 12:16:08 +0800 Subject: Initial commit --- Minecraft.World/FollowParentGoal.cpp | 65 ++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Minecraft.World/FollowParentGoal.cpp (limited to 'Minecraft.World/FollowParentGoal.cpp') diff --git a/Minecraft.World/FollowParentGoal.cpp b/Minecraft.World/FollowParentGoal.cpp new file mode 100644 index 00000000..3e347d62 --- /dev/null +++ b/Minecraft.World/FollowParentGoal.cpp @@ -0,0 +1,65 @@ +#include "stdafx.h" +#include "net.minecraft.world.entity.animal.h" +#include "net.minecraft.world.entity.ai.navigation.h" +#include "net.minecraft.world.level.h" +#include "net.minecraft.world.phys.h" +#include "BasicTypeContainers.h" +#include "FollowParentGoal.h" + +FollowParentGoal::FollowParentGoal(Animal *animal, float speed) +{ + timeToRecalcPath = 0; + + this->animal = animal; + this->speed = speed; +} + +bool FollowParentGoal::canUse() +{ + if (animal->getAge() >= 0) return false; + + vector > *parents = animal->level->getEntitiesOfClass(typeid(*animal), animal->bb->grow(8, 4, 8)); + + shared_ptr closest = nullptr; + double closestDistSqr = Double::MAX_VALUE; + for(AUTO_VAR(it, parents->begin()); it != parents->end(); ++it) + { + shared_ptr parent = dynamic_pointer_cast(*it); + if (parent->getAge() < 0) continue; + double distSqr = animal->distanceToSqr(parent); + if (distSqr > closestDistSqr) continue; + closestDistSqr = distSqr; + closest = parent; + } + delete parents; + + if (closest == NULL) return false; + if (closestDistSqr < 3 * 3) return false; + parent = weak_ptr(closest); + return true; +} + +bool FollowParentGoal::canContinueToUse() +{ + if (parent.lock() == NULL || !parent.lock()->isAlive()) return false; + double distSqr = animal->distanceToSqr(parent.lock()); + if (distSqr < 3 * 3 || distSqr > 16 * 16) return false; + return true; +} + +void FollowParentGoal::start() +{ + timeToRecalcPath = 0; +} + +void FollowParentGoal::stop() +{ + parent = weak_ptr(); +} + +void FollowParentGoal::tick() +{ + if (--timeToRecalcPath > 0) return; + timeToRecalcPath = 10; + animal->getNavigation()->moveTo(parent.lock(), speed); +} \ No newline at end of file -- cgit v1.2.3