diff options
| author | daoge_cmd <3523206925@qq.com> | 2026-03-01 12:16:08 +0800 |
|---|---|---|
| committer | daoge_cmd <3523206925@qq.com> | 2026-03-01 12:16:08 +0800 |
| commit | b691c43c44ff180d10e7d4a9afc83b98551ff586 (patch) | |
| tree | 3e9849222cbc6ba49f2f1fc6e5fe7179632c7390 /Minecraft.World/FollowParentGoal.cpp | |
| parent | def8cb415354ac390b7e89052a50605285f1aca9 (diff) | |
Initial commit
Diffstat (limited to 'Minecraft.World/FollowParentGoal.cpp')
| -rw-r--r-- | Minecraft.World/FollowParentGoal.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
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<shared_ptr<Entity> > *parents = animal->level->getEntitiesOfClass(typeid(*animal), animal->bb->grow(8, 4, 8)); + + shared_ptr<Animal> closest = nullptr; + double closestDistSqr = Double::MAX_VALUE; + for(AUTO_VAR(it, parents->begin()); it != parents->end(); ++it) + { + shared_ptr<Animal> parent = dynamic_pointer_cast<Animal>(*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<Animal>(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<Animal>(); +} + +void FollowParentGoal::tick() +{ + if (--timeToRecalcPath > 0) return; + timeToRecalcPath = 10; + animal->getNavigation()->moveTo(parent.lock(), speed); +}
\ No newline at end of file |
