| 1 | package net.minecraft.world.entity.ai.behavior.warden;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import net.minecraft.core.particles.ParticleTypes;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.sounds.SoundEvents;
|
| 7 | import net.minecraft.util.Mth;
|
| 8 | import net.minecraft.util.Unit;
|
| 9 | import net.minecraft.world.entity.EntityAttachment;
|
| 10 | import net.minecraft.world.entity.LivingEntity;
|
| 11 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 12 | import net.minecraft.world.entity.ai.behavior.Behavior;
|
| 13 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 14 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 15 | import net.minecraft.world.entity.monster.warden.Warden;
|
| 16 | import net.minecraft.world.phys.Vec3;
|
| 17 |
|
| 18 | public class SonicBoom extends Behavior<Warden> {
|
| 19 | private static final int DISTANCE_XZ = 15;
|
| 20 | private static final int DISTANCE_Y = 20;
|
| 21 | private static final double KNOCKBACK_VERTICAL = 0.5;
|
| 22 | private static final double KNOCKBACK_HORIZONTAL = 2.5;
|
| 23 | public static final int COOLDOWN = 40;
|
| 24 | private static final int TICKS_BEFORE_PLAYING_SOUND = Mth.ceil(34.0);
|
| 25 | private static final int DURATION = Mth.ceil(60.0F);
|
| 26 |
|
| 27 | public SonicBoom() {
|
| 28 | super(
|
| 29 | ImmutableMap.of(
|
| 30 | MemoryModuleType.ATTACK_TARGET,
|
| 31 | MemoryStatus.VALUE_PRESENT,
|
| 32 | MemoryModuleType.SONIC_BOOM_COOLDOWN,
|
| 33 | MemoryStatus.VALUE_ABSENT,
|
| 34 | MemoryModuleType.SONIC_BOOM_SOUND_COOLDOWN,
|
| 35 | MemoryStatus.REGISTERED,
|
| 36 | MemoryModuleType.SONIC_BOOM_SOUND_DELAY,
|
| 37 | MemoryStatus.REGISTERED
|
| 38 | ),
|
| 39 | DURATION
|
| 40 | );
|
| 41 | }
|
| 42 |
|
| 43 | protected boolean checkExtraStartConditions(final ServerLevel level, final Warden body) {
|
| 44 | return body.closerThan(body.getBrain().getMemory(MemoryModuleType.ATTACK_TARGET).get(), 15.0, 20.0);
|
| 45 | }
|
| 46 |
|
| 47 | protected boolean canStillUse(final ServerLevel level, final Warden body, final long timestamp) {
|
| 48 | return true;
|
| 49 | }
|
| 50 |
|
| 51 | protected void start(final ServerLevel level, final Warden body, final long timestamp) {
|
| 52 | body.getBrain().setMemoryWithExpiry(MemoryModuleType.ATTACK_COOLING_DOWN, true, DURATION);
|
| 53 | body.getBrain().setMemoryWithExpiry(MemoryModuleType.SONIC_BOOM_SOUND_DELAY, Unit.INSTANCE, TICKS_BEFORE_PLAYING_SOUND);
|
| 54 | level.broadcastEntityEvent(body, (byte)62);
|
| 55 | body.playSound(SoundEvents.WARDEN_SONIC_CHARGE, 3.0F, 1.0F);
|
| 56 | }
|
| 57 |
|
| 58 | protected void tick(final ServerLevel level, final Warden body, final long timestamp) {
|
| 59 | body.getBrain().getMemory(MemoryModuleType.ATTACK_TARGET).ifPresent(target -> body.getLookControl().setLookAt(target.position()));
|
| 60 | if (!body.getBrain().hasMemoryValue(MemoryModuleType.SONIC_BOOM_SOUND_DELAY) && !body.getBrain().hasMemoryValue(MemoryModuleType.SONIC_BOOM_SOUND_COOLDOWN)) {
|
| 61 | body.getBrain().setMemoryWithExpiry(MemoryModuleType.SONIC_BOOM_SOUND_COOLDOWN, Unit.INSTANCE, DURATION - TICKS_BEFORE_PLAYING_SOUND);
|
| 62 | body.getBrain()
|
| 63 | .getMemory(MemoryModuleType.ATTACK_TARGET)
|
| 64 | .filter(body::canTargetEntity)
|
| 65 | .filter(target -> body.closerThan(target, 15.0, 20.0))
|
| 66 | .ifPresent(target -> {
|
| 67 | Vec3 source = body.position().add(body.getAttachments().get(EntityAttachment.WARDEN_CHEST, 0, body.getYRot()));
|
| 68 | Vec3 delta = target.getEyePosition().subtract(source);
|
| 69 | Vec3 normalize = delta.normalize();
|
| 70 | int steps = Mth.floor(delta.length()) + 7;
|
| 71 |
|
| 72 | for (int i = 1; i < steps; i++) {
|
| 73 | Vec3 particlePos = source.add(normalize.scale(i));
|
| 74 | level.sendParticles(ParticleTypes.SONIC_BOOM, particlePos.x, particlePos.y, particlePos.z, 1, 0.0, 0.0, 0.0, 0.0);
|
| 75 | }
|
| 76 |
|
| 77 | body.playSound(SoundEvents.WARDEN_SONIC_BOOM, 3.0F, 1.0F);
|
| 78 | if (target.hurtServer(level, level.damageSources().sonicBoom(body), 10.0F)) {
|
| 79 | double knockbackVertical = 0.5 * (1.0 - target.getAttributeValue(Attributes.KNOCKBACK_RESISTANCE));
|
| 80 | double knockbackHorizontal = 2.5 * (1.0 - target.getAttributeValue(Attributes.KNOCKBACK_RESISTANCE));
|
| 81 | target.push(normalize.x() * knockbackHorizontal, normalize.y() * knockbackVertical, normalize.z() * knockbackHorizontal);
|
| 82 | }
|
| 83 | });
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | protected void stop(final ServerLevel level, final Warden body, final long timestamp) {
|
| 88 | setCooldown(body, 40);
|
| 89 | }
|
| 90 |
|
| 91 | public static void setCooldown(final LivingEntity body, final int cooldown) {
|
| 92 | body.getBrain().setMemoryWithExpiry(MemoryModuleType.SONIC_BOOM_COOLDOWN, Unit.INSTANCE, cooldown);
|
| 93 | }
|
| 94 | }
|
| 95 |
|