| 1 | package net.minecraft.world.entity.animal.squid;
|
| 2 |
|
| 3 | import java.util.Objects;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.particles.ParticleOptions;
|
| 6 | import net.minecraft.core.particles.ParticleTypes;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundEvent;
|
| 9 | import net.minecraft.sounds.SoundEvents;
|
| 10 | import net.minecraft.tags.FluidTags;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.world.DifficultyInstance;
|
| 13 | import net.minecraft.world.damagesource.DamageSource;
|
| 14 | import net.minecraft.world.effect.MobEffects;
|
| 15 | import net.minecraft.world.entity.AgeableMob;
|
| 16 | import net.minecraft.world.entity.Entity;
|
| 17 | import net.minecraft.world.entity.EntityDimensions;
|
| 18 | import net.minecraft.world.entity.EntityEvent;
|
| 19 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 20 | import net.minecraft.world.entity.EntityType;
|
| 21 | import net.minecraft.world.entity.EntityTypes;
|
| 22 | import net.minecraft.world.entity.LivingEntity;
|
| 23 | import net.minecraft.world.entity.Mob;
|
| 24 | import net.minecraft.world.entity.MoverType;
|
| 25 | import net.minecraft.world.entity.Pose;
|
| 26 | import net.minecraft.world.entity.SpawnGroupData;
|
| 27 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 28 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 29 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 30 | import net.minecraft.world.entity.animal.AgeableWaterCreature;
|
| 31 | import net.minecraft.world.level.Level;
|
| 32 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 33 | import net.minecraft.world.level.block.state.BlockState;
|
| 34 | import net.minecraft.world.level.material.FluidState;
|
| 35 | import net.minecraft.world.phys.Vec3;
|
| 36 | import org.jspecify.annotations.Nullable;
|
| 37 |
|
| 38 | public class Squid extends AgeableWaterCreature {
|
| 39 | public float xBodyRot;
|
| 40 | public float xBodyRotO;
|
| 41 | public float zBodyRot;
|
| 42 | public float zBodyRotO;
|
| 43 | public float tentacleMovement;
|
| 44 | public float oldTentacleMovement;
|
| 45 | public float tentacleAngle;
|
| 46 | public float oldTentacleAngle;
|
| 47 | private float speed;
|
| 48 | private float tentacleSpeed;
|
| 49 | private float rotateSpeed;
|
| 50 | private Vec3 movementVector = Vec3.ZERO;
|
| 51 | private static final EntityDimensions BABY_DIMENSIONS = EntityDimensions.scalable(0.5F, 0.5F).withEyeHeight(0.37F);
|
| 52 |
|
| 53 | public Squid(final EntityType<? extends Squid> type, final Level level) {
|
| 54 | super(type, level);
|
| 55 | this.tentacleSpeed = 1.0F / (this.random.nextFloat() + 1.0F) * 0.2F;
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | protected void registerGoals() {
|
| 60 | this.goalSelector.addGoal(0, new Squid.SquidRandomMovementGoal(this));
|
| 61 | this.goalSelector.addGoal(1, new Squid.SquidFleeGoal());
|
| 62 | }
|
| 63 |
|
| 64 | public static AttributeSupplier.Builder createAttributes() {
|
| 65 | return Mob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0);
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | protected SoundEvent getAmbientSound() {
|
| 70 | return SoundEvents.SQUID_AMBIENT;
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 75 | return SoundEvents.SQUID_HURT;
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | protected SoundEvent getDeathSound() {
|
| 80 | return SoundEvents.SQUID_DEATH;
|
| 81 | }
|
| 82 |
|
| 83 | protected SoundEvent getSquirtSound() {
|
| 84 | return SoundEvents.SQUID_SQUIRT;
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public boolean canBeLeashed() {
|
| 89 | return true;
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | protected float getSoundVolume() {
|
| 94 | return 0.4F;
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | protected Entity.MovementEmission getMovementEmission() {
|
| 99 | return Entity.MovementEmission.EVENTS;
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public @Nullable AgeableMob getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
|
| 104 | return EntityTypes.SQUID.create(level, EntitySpawnReason.BREEDING);
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | protected double getDefaultGravity() {
|
| 109 | return 0.08;
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | public void aiStep() {
|
| 114 | super.aiStep();
|
| 115 | this.xBodyRotO = this.xBodyRot;
|
| 116 | this.zBodyRotO = this.zBodyRot;
|
| 117 | this.oldTentacleMovement = this.tentacleMovement;
|
| 118 | this.oldTentacleAngle = this.tentacleAngle;
|
| 119 | this.tentacleMovement = this.tentacleMovement + this.tentacleSpeed;
|
| 120 | if (this.tentacleMovement > Math.PI * 2) {
|
| 121 | if (this.level().isClientSide()) {
|
| 122 | this.tentacleMovement = (float) (Math.PI * 2);
|
| 123 | } else {
|
| 124 | this.tentacleMovement -= (float) (Math.PI * 2);
|
| 125 | if (this.random.nextInt(10) == 0) {
|
| 126 | this.tentacleSpeed = 1.0F / (this.random.nextFloat() + 1.0F) * 0.2F;
|
| 127 | }
|
| 128 |
|
| 129 | this.level().broadcastEntityEvent(this, (byte)19);
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | if (this.isInWater()) {
|
| 134 | if (this.tentacleMovement < (float) Math.PI) {
|
| 135 | float tentacleScale = this.tentacleMovement / (float) Math.PI;
|
| 136 | this.tentacleAngle = Mth.sin(tentacleScale * tentacleScale * (float) Math.PI) * (float) Math.PI * 0.25F;
|
| 137 | if (tentacleScale > 0.75) {
|
| 138 | if (this.isLocalInstanceAuthoritative()) {
|
| 139 | this.setDeltaMovement(this.movementVector);
|
| 140 | }
|
| 141 |
|
| 142 | this.rotateSpeed = 1.0F;
|
| 143 | } else {
|
| 144 | this.rotateSpeed *= 0.8F;
|
| 145 | }
|
| 146 | } else {
|
| 147 | this.tentacleAngle = 0.0F;
|
| 148 | if (this.isLocalInstanceAuthoritative()) {
|
| 149 | this.setDeltaMovement(this.getDeltaMovement().scale(0.9));
|
| 150 | }
|
| 151 |
|
| 152 | this.rotateSpeed *= 0.99F;
|
| 153 | }
|
| 154 |
|
| 155 | Vec3 movement = this.getDeltaMovement();
|
| 156 | double horizontalMovement = movement.horizontalDistance();
|
| 157 | this.yBodyRot = this.yBodyRot + (-((float)Mth.atan2(movement.x, movement.z)) * (180.0F / (float)Math.PI) - this.yBodyRot) * 0.1F;
|
| 158 | this.setYRot(this.yBodyRot);
|
| 159 | this.zBodyRot = this.zBodyRot + (float) Math.PI * this.rotateSpeed * 1.5F;
|
| 160 | this.xBodyRot = this.xBodyRot + (-((float)Mth.atan2(horizontalMovement, movement.y)) * (180.0F / (float)Math.PI) - this.xBodyRot) * 0.1F;
|
| 161 | } else {
|
| 162 | this.tentacleAngle = Mth.abs(Mth.sin(this.tentacleMovement)) * (float) Math.PI * 0.25F;
|
| 163 | if (!this.level().isClientSide()) {
|
| 164 | double yd = this.getDeltaMovement().y;
|
| 165 | if (this.hasEffect(MobEffects.LEVITATION)) {
|
| 166 | yd = 0.05 * (this.getEffect(MobEffects.LEVITATION).getAmplifier() + 1);
|
| 167 | } else {
|
| 168 | yd -= this.getGravity();
|
| 169 | }
|
| 170 |
|
| 171 | this.setDeltaMovement(0.0, yd * this.getAirDrag(), 0.0);
|
| 172 | }
|
| 173 |
|
| 174 | this.xBodyRot = this.xBodyRot + (-90.0F - this.xBodyRot) * 0.02F;
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 180 | if (super.hurtServer(level, source, damage) && this.getLastHurtByMob() != null) {
|
| 181 | this.spawnInk();
|
| 182 | return true;
|
| 183 | } else {
|
| 184 | return false;
|
| 185 | }
|
| 186 | }
|
| 187 |
|
| 188 | private Vec3 rotateVector(final Vec3 vec) {
|
| 189 | Vec3 v = vec.xRot(this.xBodyRotO * (float) (Math.PI / 180.0));
|
| 190 | return v.yRot(-this.yBodyRotO * (float) (Math.PI / 180.0));
|
| 191 | }
|
| 192 |
|
| 193 | private void spawnInk() {
|
| 194 | this.makeSound(this.getSquirtSound());
|
| 195 | Vec3 pos = this.rotateVector(new Vec3(0.0, -1.0, 0.0)).add(this.getX(), this.getY(), this.getZ());
|
| 196 |
|
| 197 | for (int i = 0; i < 30; i++) {
|
| 198 | Vec3 dir = this.rotateVector(new Vec3(this.random.nextFloat() * 0.6 - 0.3, -1.0, this.random.nextFloat() * 0.6 - 0.3));
|
| 199 | float inkPosOffsetScale = this.isBaby() ? 0.1F : 0.3F;
|
| 200 | Vec3 dirOffset = dir.scale(inkPosOffsetScale + this.random.nextFloat() * 2.0F);
|
| 201 | ((ServerLevel)this.level()).sendParticles(this.getInkParticle(), pos.x, pos.y + 0.5, pos.z, 0, dirOffset.x, dirOffset.y, dirOffset.z, 0.1F);
|
| 202 | }
|
| 203 | }
|
| 204 |
|
| 205 | protected ParticleOptions getInkParticle() {
|
| 206 | return ParticleTypes.SQUID_INK;
|
| 207 | }
|
| 208 |
|
| 209 | @Override
|
| 210 | public void travel(final Vec3 input) {
|
| 211 | this.move(MoverType.SELF, this.getDeltaMovement());
|
| 212 | }
|
| 213 |
|
| 214 | @Override
|
| 215 | public void handleEntityEvent(final @EntityEvent.Value byte id) {
|
| 216 | if (id == 19) {
|
| 217 | this.tentacleMovement = 0.0F;
|
| 218 | } else {
|
| 219 | super.handleEntityEvent(id);
|
| 220 | }
|
| 221 | }
|
| 222 |
|
| 223 | public boolean hasMovementVector() {
|
| 224 | return this.movementVector.lengthSqr() > 1.0E-5F;
|
| 225 | }
|
| 226 |
|
| 227 | @Override
|
| 228 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 229 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
|
| 230 | ) {
|
| 231 | SpawnGroupData spawnGroupData = Objects.requireNonNullElseGet(groupData, () -> new AgeableMob.AgeableMobGroupData(0.05F));
|
| 232 | return super.finalizeSpawn(level, difficulty, spawnReason, spawnGroupData);
|
| 233 | }
|
| 234 |
|
| 235 | @Override
|
| 236 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 237 | return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
|
| 238 | }
|
| 239 |
|
| 240 | private class SquidFleeGoal extends Goal {
|
| 241 | private static final float SQUID_FLEE_SPEED = 3.0F;
|
| 242 | private static final float SQUID_FLEE_MIN_DISTANCE = 5.0F;
|
| 243 | private static final float SQUID_FLEE_MAX_DISTANCE = 10.0F;
|
| 244 | private int fleeTicks;
|
| 245 |
|
| 246 | @Override
|
| 247 | public boolean canUse() {
|
| 248 | LivingEntity entity = Squid.this.getLastHurtByMob();
|
| 249 | return Squid.this.isInWater() && entity != null ? Squid.this.distanceToSqr(entity) < 100.0 : false;
|
| 250 | }
|
| 251 |
|
| 252 | @Override
|
| 253 | public void start() {
|
| 254 | this.fleeTicks = 0;
|
| 255 | }
|
| 256 |
|
| 257 | @Override
|
| 258 | public boolean requiresUpdateEveryTick() {
|
| 259 | return true;
|
| 260 | }
|
| 261 |
|
| 262 | @Override
|
| 263 | public void tick() {
|
| 264 | this.fleeTicks++;
|
| 265 | LivingEntity lastHurtByMob = Squid.this.getLastHurtByMob();
|
| 266 | if (lastHurtByMob != null) {
|
| 267 | Vec3 fleeTo = new Vec3(Squid.this.getX() - lastHurtByMob.getX(), Squid.this.getY() - lastHurtByMob.getY(), Squid.this.getZ() - lastHurtByMob.getZ());
|
| 268 | BlockState blockState = Squid.this.level()
|
| 269 | .getBlockState(BlockPos.containing(Squid.this.getX() + fleeTo.x, Squid.this.getY() + fleeTo.y, Squid.this.getZ() + fleeTo.z));
|
| 270 | FluidState fluidState = Squid.this.level()
|
| 271 | .getFluidState(BlockPos.containing(Squid.this.getX() + fleeTo.x, Squid.this.getY() + fleeTo.y, Squid.this.getZ() + fleeTo.z));
|
| 272 | if (fluidState.is(FluidTags.WATER) || blockState.isAir()) {
|
| 273 | double length = fleeTo.length();
|
| 274 | if (length > 0.0) {
|
| 275 | fleeTo.normalize();
|
| 276 | double avoidSpeed = 3.0;
|
| 277 | if (length > 5.0) {
|
| 278 | avoidSpeed -= (length - 5.0) / 5.0;
|
| 279 | }
|
| 280 |
|
| 281 | if (avoidSpeed > 0.0) {
|
| 282 | fleeTo = fleeTo.scale(avoidSpeed);
|
| 283 | }
|
| 284 | }
|
| 285 |
|
| 286 | if (blockState.isAir()) {
|
| 287 | fleeTo = fleeTo.subtract(0.0, fleeTo.y, 0.0);
|
| 288 | }
|
| 289 |
|
| 290 | Squid.this.movementVector = new Vec3(fleeTo.x / 20.0, fleeTo.y / 20.0, fleeTo.z / 20.0);
|
| 291 | }
|
| 292 |
|
| 293 | if (this.fleeTicks % 10 == 5) {
|
| 294 | Squid.this.level().addParticle(ParticleTypes.BUBBLE, Squid.this.getX(), Squid.this.getY(), Squid.this.getZ(), 0.0, 0.0, 0.0);
|
| 295 | }
|
| 296 | }
|
| 297 | }
|
| 298 | }
|
| 299 |
|
| 300 | private static class SquidRandomMovementGoal extends Goal {
|
| 301 | private final Squid squid;
|
| 302 |
|
| 303 | public SquidRandomMovementGoal(final Squid squid) {
|
| 304 | this.squid = squid;
|
| 305 | }
|
| 306 |
|
| 307 | @Override
|
| 308 | public boolean canUse() {
|
| 309 | return true;
|
| 310 | }
|
| 311 |
|
| 312 | @Override
|
| 313 | public void tick() {
|
| 314 | int noActionTime = this.squid.getNoActionTime();
|
| 315 | if (noActionTime > 100) {
|
| 316 | this.squid.movementVector = Vec3.ZERO;
|
| 317 | } else if (this.squid.getRandom().nextInt(reducedTickDelay(50)) == 0 || !this.squid.wasTouchingWater || !this.squid.hasMovementVector()) {
|
| 318 | float angle = this.squid.getRandom().nextFloat() * (float) (Math.PI * 2);
|
| 319 | this.squid.movementVector = new Vec3(Mth.cos(angle) * 0.2F, -0.1F + this.squid.getRandom().nextFloat() * 0.2F, Mth.sin(angle) * 0.2F);
|
| 320 | }
|
| 321 | }
|
| 322 | }
|
| 323 | }
|
| 324 |
|