| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import net.minecraft.server.level.ServerLevel;
|
| 5 | import net.minecraft.util.Mth;
|
| 6 | import net.minecraft.util.RandomSource;
|
| 7 | import net.minecraft.util.valueproviders.IntProvider;
|
| 8 | import net.minecraft.world.entity.Mob;
|
| 9 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 10 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 11 | import net.minecraft.world.phys.Vec3;
|
| 12 |
|
| 13 | public class RandomLookAround extends Behavior<Mob> {
|
| 14 | private final IntProvider interval;
|
| 15 | private final float maxYaw;
|
| 16 | private final float minPitch;
|
| 17 | private final float pitchRange;
|
| 18 |
|
| 19 | public RandomLookAround(final IntProvider interval, final float maxYaw, final float minPitch, final float maxPitch) {
|
| 20 | super(ImmutableMap.of(MemoryModuleType.LOOK_TARGET, MemoryStatus.VALUE_ABSENT, MemoryModuleType.GAZE_COOLDOWN_TICKS, MemoryStatus.VALUE_ABSENT));
|
| 21 | if (minPitch > maxPitch) {
|
| 22 | throw new IllegalArgumentException("Minimum pitch is larger than maximum pitch! " + minPitch + " > " + maxPitch);
|
| 23 | }
|
| 24 |
|
| 25 | this.interval = interval;
|
| 26 | this.maxYaw = maxYaw;
|
| 27 | this.minPitch = minPitch;
|
| 28 | this.pitchRange = maxPitch - minPitch;
|
| 29 | }
|
| 30 |
|
| 31 | protected void start(final ServerLevel level, final Mob body, final long timestamp) {
|
| 32 | RandomSource random = body.getRandom();
|
| 33 | float pitch = Mth.clamp(random.nextFloat() * this.pitchRange + this.minPitch, -90.0F, 90.0F);
|
| 34 | float rotation = Mth.wrapDegrees(body.getYRot() + 2.0F * random.nextFloat() * this.maxYaw - this.maxYaw);
|
| 35 | Vec3 newLookVec = Vec3.directionFromRotation(pitch, rotation);
|
| 36 | body.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, new BlockPosTracker(body.getEyePosition().add(newLookVec)));
|
| 37 | body.getBrain().setMemory(MemoryModuleType.GAZE_COOLDOWN_TICKS, this.interval.sample(random));
|
| 38 | }
|
| 39 | }
|
| 40 |
|