| 1 | package net.minecraft.world.entity.monster.breeze;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.collect.ImmutableMap;
|
| 5 | import net.minecraft.commands.arguments.EntityAnchorArgument;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.sounds.SoundEvents;
|
| 8 | import net.minecraft.util.Unit;
|
| 9 | import net.minecraft.world.entity.LivingEntity;
|
| 10 | import net.minecraft.world.entity.Pose;
|
| 11 | import net.minecraft.world.entity.ai.Brain;
|
| 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.projectile.Projectile;
|
| 16 | import net.minecraft.world.entity.projectile.hurtingprojectile.windcharge.BreezeWindCharge;
|
| 17 | import net.minecraft.world.item.ItemStack;
|
| 18 |
|
| 19 | public class Shoot extends Behavior<Breeze> {
|
| 20 | private static final int ATTACK_RANGE_MAX_SQRT = 256;
|
| 21 | private static final int UNCERTAINTY_BASE = 5;
|
| 22 | private static final int UNCERTAINTY_MULTIPLIER = 4;
|
| 23 | private static final float PROJECTILE_MOVEMENT_SCALE = 0.7F;
|
| 24 | private static final int SHOOT_INITIAL_DELAY_TICKS = Math.round(15.0F);
|
| 25 | private static final int SHOOT_RECOVER_DELAY_TICKS = Math.round(4.0F);
|
| 26 | private static final int SHOOT_COOLDOWN_TICKS = Math.round(10.0F);
|
| 27 |
|
| 28 | @VisibleForTesting
|
| 29 | public Shoot() {
|
| 30 | super(
|
| 31 | ImmutableMap.of(
|
| 32 | MemoryModuleType.ATTACK_TARGET,
|
| 33 | MemoryStatus.VALUE_PRESENT,
|
| 34 | MemoryModuleType.BREEZE_SHOOT_COOLDOWN,
|
| 35 | MemoryStatus.VALUE_ABSENT,
|
| 36 | MemoryModuleType.BREEZE_SHOOT_CHARGING,
|
| 37 | MemoryStatus.VALUE_ABSENT,
|
| 38 | MemoryModuleType.BREEZE_SHOOT_RECOVERING,
|
| 39 | MemoryStatus.VALUE_ABSENT,
|
| 40 | MemoryModuleType.BREEZE_SHOOT,
|
| 41 | MemoryStatus.VALUE_PRESENT,
|
| 42 | MemoryModuleType.WALK_TARGET,
|
| 43 | MemoryStatus.VALUE_ABSENT,
|
| 44 | MemoryModuleType.BREEZE_JUMP_TARGET,
|
| 45 | MemoryStatus.VALUE_ABSENT
|
| 46 | ),
|
| 47 | SHOOT_INITIAL_DELAY_TICKS + 1 + SHOOT_RECOVER_DELAY_TICKS
|
| 48 | );
|
| 49 | }
|
| 50 |
|
| 51 | protected boolean checkExtraStartConditions(final ServerLevel level, final Breeze breeze) {
|
| 52 | return breeze.getPose() != Pose.STANDING
|
| 53 | ? false
|
| 54 | : breeze.getBrain().getMemory(MemoryModuleType.ATTACK_TARGET).map(target -> isTargetWithinRange(breeze, target)).map(withinRange -> {
|
| 55 | if (!withinRange) {
|
| 56 | breeze.getBrain().eraseMemory(MemoryModuleType.BREEZE_SHOOT);
|
| 57 | }
|
| 58 |
|
| 59 | return (Boolean)withinRange;
|
| 60 | }).orElse(false);
|
| 61 | }
|
| 62 |
|
| 63 | protected boolean canStillUse(final ServerLevel level, final Breeze body, final long timestamp) {
|
| 64 | return body.getBrain().hasMemoryValue(MemoryModuleType.ATTACK_TARGET) && body.getBrain().hasMemoryValue(MemoryModuleType.BREEZE_SHOOT);
|
| 65 | }
|
| 66 |
|
| 67 | protected void start(final ServerLevel level, final Breeze breeze, final long timestamp) {
|
| 68 | breeze.getBrain().getMemory(MemoryModuleType.ATTACK_TARGET).ifPresent(target -> breeze.setPose(Pose.SHOOTING));
|
| 69 | breeze.getBrain().setMemoryWithExpiry(MemoryModuleType.BREEZE_SHOOT_CHARGING, Unit.INSTANCE, SHOOT_INITIAL_DELAY_TICKS);
|
| 70 | breeze.playSound(SoundEvents.BREEZE_INHALE, 1.0F, 1.0F);
|
| 71 | }
|
| 72 |
|
| 73 | protected void stop(final ServerLevel level, final Breeze breeze, final long timestamp) {
|
| 74 | if (breeze.getPose() == Pose.SHOOTING) {
|
| 75 | breeze.setPose(Pose.STANDING);
|
| 76 | }
|
| 77 |
|
| 78 | breeze.getBrain().setMemoryWithExpiry(MemoryModuleType.BREEZE_SHOOT_COOLDOWN, Unit.INSTANCE, SHOOT_COOLDOWN_TICKS);
|
| 79 | breeze.getBrain().eraseMemory(MemoryModuleType.BREEZE_SHOOT);
|
| 80 | }
|
| 81 |
|
| 82 | protected void tick(final ServerLevel level, final Breeze breeze, final long timestamp) {
|
| 83 | Brain<Breeze> brain = breeze.getBrain();
|
| 84 | LivingEntity target = brain.getMemory(MemoryModuleType.ATTACK_TARGET).orElse(null);
|
| 85 | if (target != null) {
|
| 86 | breeze.lookAt(EntityAnchorArgument.Anchor.EYES, target.position());
|
| 87 | if (!brain.getMemory(MemoryModuleType.BREEZE_SHOOT_CHARGING).isPresent() && !brain.getMemory(MemoryModuleType.BREEZE_SHOOT_RECOVERING).isPresent()) {
|
| 88 | brain.setMemoryWithExpiry(MemoryModuleType.BREEZE_SHOOT_RECOVERING, Unit.INSTANCE, SHOOT_RECOVER_DELAY_TICKS);
|
| 89 | double xd = target.getX() - breeze.getX();
|
| 90 | double yd = target.getY(target.isPassenger() ? 0.8 : 0.3) - breeze.getFiringYPosition();
|
| 91 | double zd = target.getZ() - breeze.getZ();
|
| 92 | Projectile.spawnProjectileUsingShoot(new BreezeWindCharge(breeze, level), level, ItemStack.EMPTY, xd, yd, zd, 0.7F, 5 - level.getDifficulty().getId() * 4);
|
| 93 | breeze.playSound(SoundEvents.BREEZE_SHOOT, 1.5F, 1.0F);
|
| 94 | }
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | private static boolean isTargetWithinRange(final Breeze body, final LivingEntity target) {
|
| 99 | double distanceSqrt = body.position().distanceToSqr(target.position());
|
| 100 | return distanceSqrt < 256.0;
|
| 101 | }
|
| 102 | }
|
| 103 |
|