| 1 | package net.minecraft.world.entity.boss.enderdragon.phases;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import net.minecraft.core.Vec3i;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.util.Mth;
|
| 7 | import net.minecraft.world.entity.LivingEntity;
|
| 8 | import net.minecraft.world.entity.boss.enderdragon.EnderDragon;
|
| 9 | import net.minecraft.world.entity.projectile.hurtingprojectile.DragonFireball;
|
| 10 | import net.minecraft.world.level.pathfinder.Node;
|
| 11 | import net.minecraft.world.level.pathfinder.Path;
|
| 12 | import net.minecraft.world.phys.Vec3;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 | import org.slf4j.Logger;
|
| 15 |
|
| 16 | public class DragonStrafePlayerPhase extends AbstractDragonPhaseInstance {
|
| 17 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 18 | private static final int FIREBALL_CHARGE_AMOUNT = 5;
|
| 19 | private int fireballCharge;
|
| 20 | private @Nullable Path currentPath;
|
| 21 | private @Nullable Vec3 targetLocation;
|
| 22 | private @Nullable LivingEntity attackTarget;
|
| 23 | private boolean holdingPatternClockwise;
|
| 24 |
|
| 25 | public DragonStrafePlayerPhase(final EnderDragon dragon) {
|
| 26 | super(dragon);
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | public void doServerTick(final ServerLevel level) {
|
| 31 | if (this.attackTarget == null) {
|
| 32 | LOGGER.warn("Skipping player strafe phase because no player was found");
|
| 33 | this.dragon.getPhaseManager().setPhase(EnderDragonPhase.HOLDING_PATTERN);
|
| 34 | } else {
|
| 35 | if (this.currentPath != null && this.currentPath.isDone()) {
|
| 36 | double xTarget = this.attackTarget.getX();
|
| 37 | double zTarget = this.attackTarget.getZ();
|
| 38 | double xTargetDist = xTarget - this.dragon.getX();
|
| 39 | double zTargetDist = zTarget - this.dragon.getZ();
|
| 40 | double dist = Math.sqrt(xTargetDist * xTargetDist + zTargetDist * zTargetDist);
|
| 41 | double heightOffset = Math.min(0.4F + dist / 80.0 - 1.0, 10.0);
|
| 42 | this.targetLocation = new Vec3(xTarget, this.attackTarget.getY() + heightOffset, zTarget);
|
| 43 | }
|
| 44 |
|
| 45 | double distToTarget = this.targetLocation == null ? 0.0 : this.targetLocation.distanceToSqr(this.dragon.getX(), this.dragon.getY(), this.dragon.getZ());
|
| 46 | if (distToTarget < 100.0 || distToTarget > 22500.0) {
|
| 47 | this.findNewTarget();
|
| 48 | }
|
| 49 |
|
| 50 | double maxDist = 64.0;
|
| 51 | if (this.attackTarget.distanceToSqr(this.dragon) < 4096.0) {
|
| 52 | if (this.dragon.hasLineOfSight(this.attackTarget)) {
|
| 53 | this.fireballCharge++;
|
| 54 | Vec3 aim = new Vec3(this.attackTarget.getX() - this.dragon.getX(), 0.0, this.attackTarget.getZ() - this.dragon.getZ()).normalize();
|
| 55 | Vec3 dir = new Vec3(Mth.sin(this.dragon.getYRot() * (float) (Math.PI / 180.0)), 0.0, -Mth.cos(this.dragon.getYRot() * (float) (Math.PI / 180.0)))
|
| 56 | .normalize();
|
| 57 | float dot = (float)dir.dot(aim);
|
| 58 | float angleDegs = (float)(Math.acos(dot) * 180.0F / (float)Math.PI);
|
| 59 | angleDegs += 0.5F;
|
| 60 | if (this.fireballCharge >= 5 && angleDegs >= 0.0F && angleDegs < 10.0F) {
|
| 61 | double d = 1.0;
|
| 62 | Vec3 viewVector = this.dragon.getViewVector(1.0F);
|
| 63 | double startingX = this.dragon.head.getX() - viewVector.x * 1.0;
|
| 64 | double startingY = this.dragon.head.getY(0.5) + 0.5;
|
| 65 | double startingZ = this.dragon.head.getZ() - viewVector.z * 1.0;
|
| 66 | double xdd = this.attackTarget.getX() - startingX;
|
| 67 | double ydd = this.attackTarget.getY(0.5) - startingY;
|
| 68 | double zdd = this.attackTarget.getZ() - startingZ;
|
| 69 | Vec3 direction = new Vec3(xdd, ydd, zdd);
|
| 70 | if (!this.dragon.isSilent()) {
|
| 71 | level.levelEvent(null, 1017, this.dragon.blockPosition(), 0);
|
| 72 | }
|
| 73 |
|
| 74 | DragonFireball entity = new DragonFireball(level, this.dragon, direction.normalize());
|
| 75 | entity.snapTo(startingX, startingY, startingZ, 0.0F, 0.0F);
|
| 76 | level.addFreshEntity(entity);
|
| 77 | this.fireballCharge = 0;
|
| 78 | if (this.currentPath != null) {
|
| 79 | while (!this.currentPath.isDone()) {
|
| 80 | this.currentPath.advance();
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | this.dragon.getPhaseManager().setPhase(EnderDragonPhase.HOLDING_PATTERN);
|
| 85 | }
|
| 86 | } else if (this.fireballCharge > 0) {
|
| 87 | this.fireballCharge--;
|
| 88 | }
|
| 89 | } else if (this.fireballCharge > 0) {
|
| 90 | this.fireballCharge--;
|
| 91 | }
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | private void findNewTarget() {
|
| 96 | if (this.currentPath == null || this.currentPath.isDone()) {
|
| 97 | int currentNodeIndex = this.dragon.findClosestNode();
|
| 98 | int targetNodeIndex = currentNodeIndex;
|
| 99 | if (this.dragon.getRandom().nextInt(8) == 0) {
|
| 100 | this.holdingPatternClockwise = !this.holdingPatternClockwise;
|
| 101 | targetNodeIndex += 6;
|
| 102 | }
|
| 103 |
|
| 104 | if (this.holdingPatternClockwise) {
|
| 105 | targetNodeIndex++;
|
| 106 | } else {
|
| 107 | targetNodeIndex--;
|
| 108 | }
|
| 109 |
|
| 110 | if (this.dragon.getDragonFight() != null && this.dragon.getDragonFight().aliveCrystals() > 0) {
|
| 111 | targetNodeIndex %= 12;
|
| 112 | if (targetNodeIndex < 0) {
|
| 113 | targetNodeIndex += 12;
|
| 114 | }
|
| 115 | } else {
|
| 116 | targetNodeIndex -= 12;
|
| 117 | targetNodeIndex &= 7;
|
| 118 | targetNodeIndex += 12;
|
| 119 | }
|
| 120 |
|
| 121 | this.currentPath = this.dragon.findPath(currentNodeIndex, targetNodeIndex, null);
|
| 122 | if (this.currentPath != null) {
|
| 123 | this.currentPath.advance();
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | this.navigateToNextPathNode();
|
| 128 | }
|
| 129 |
|
| 130 | private void navigateToNextPathNode() {
|
| 131 | if (this.currentPath != null && !this.currentPath.isDone()) {
|
| 132 | Vec3i current = this.currentPath.getNextNodePos();
|
| 133 | this.currentPath.advance();
|
| 134 | double xTarget = current.getX();
|
| 135 | double zTarget = current.getZ();
|
| 136 |
|
| 137 | double yTarget;
|
| 138 | do {
|
| 139 | yTarget = current.getY() + this.dragon.getRandom().nextFloat() * 20.0F;
|
| 140 | } while (yTarget < current.getY());
|
| 141 |
|
| 142 | this.targetLocation = new Vec3(xTarget, yTarget, zTarget);
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | public void begin() {
|
| 148 | this.fireballCharge = 0;
|
| 149 | this.targetLocation = null;
|
| 150 | this.currentPath = null;
|
| 151 | this.attackTarget = null;
|
| 152 | }
|
| 153 |
|
| 154 | public void setTarget(final LivingEntity target) {
|
| 155 | this.attackTarget = target;
|
| 156 | int currentNodeIndex = this.dragon.findClosestNode();
|
| 157 | int targetNodeIndex = this.dragon.findClosestNode(this.attackTarget.getX(), this.attackTarget.getY(), this.attackTarget.getZ());
|
| 158 | int finalXTarget = this.attackTarget.getBlockX();
|
| 159 | int finalZTarget = this.attackTarget.getBlockZ();
|
| 160 | double xd = finalXTarget - this.dragon.getX();
|
| 161 | double zd = finalZTarget - this.dragon.getZ();
|
| 162 | double sd = Math.sqrt(xd * xd + zd * zd);
|
| 163 | double ho = Math.min(0.4F + sd / 80.0 - 1.0, 10.0);
|
| 164 | int finalYTarget = Mth.floor(this.attackTarget.getY() + ho);
|
| 165 | Node finalNode = new Node(finalXTarget, finalYTarget, finalZTarget);
|
| 166 | this.currentPath = this.dragon.findPath(currentNodeIndex, targetNodeIndex, finalNode);
|
| 167 | if (this.currentPath != null) {
|
| 168 | this.currentPath.advance();
|
| 169 | this.navigateToNextPathNode();
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | @Override
|
| 174 | public @Nullable Vec3 getFlyTargetLocation() {
|
| 175 | return this.targetLocation;
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | public EnderDragonPhase<DragonStrafePlayerPhase> getPhase() {
|
| 180 | return EnderDragonPhase.STRAFE_PLAYER;
|
| 181 | }
|
| 182 | }
|
| 183 |
|