Commit ee52fb31 by Jonathan Thomas

- Renamed lookAtEntity to lookAtPlayer

- Added custom SquidEntity support for lookcontrols (fixing squid issues with following and looking at player)
- Refactored look controls to be simpler and easier to modify
parent 79077bf9
Pipeline #11973 passed with stage
in 20 seconds
...@@ -2,33 +2,62 @@ package com.owlmaddie.controls; ...@@ -2,33 +2,62 @@ package com.owlmaddie.controls;
import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.mob.SlimeEntity; import net.minecraft.entity.mob.SlimeEntity;
import net.minecraft.entity.passive.SquidEntity;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* The {@code LookControls} class allows an entity to look at the player, with some custom fixes * The {@code LookControls} class enables entities to look at the player,
* for certain mobs that refuse to use the normal LookControls (i.e. Slime). * with specific adjustments for certain mobs like Slimes and Squids.
*/ */
public class LookControls { public class LookControls {
public static void LookAtEntity(ServerPlayerEntity player, MobEntity entity) { public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt");
// Fix Slimes (who handle looking differently) public static void lookAtPlayer(ServerPlayerEntity player, MobEntity entity) {
if (entity instanceof SlimeEntity) { if (entity instanceof SlimeEntity) {
// Calculate direction which entity needs to face handleSlimeLook((SlimeEntity) entity, player);
double deltaX = player.getX() - entity.getX(); } else if (entity instanceof SquidEntity) {
double deltaZ = player.getZ() - entity.getZ(); handleSquidLook((SquidEntity) entity, player);
float currentYaw = entity.getYaw();
float targetYaw = (float) Math.toDegrees(Math.atan2(deltaZ, deltaX)) - 90.0F;
float yawDifference = MathHelper.wrapDegrees(targetYaw - currentYaw); // Ensures the difference is within -180 to 180
float yawChange = MathHelper.clamp(yawDifference, -10.0F, 10.0F); // Limits the change to 10 degrees
((SlimeEntity.SlimeMoveControl) entity.getMoveControl()).look(currentYaw + yawChange, false);
} else { } else {
// Make the entity look at the player // Make the entity look at the player
entity.getLookControl().lookAt(player, 10.0F, (float)entity.getMaxLookPitchChange()); entity.getLookControl().lookAt(player, 10.0F, (float)entity.getMaxLookPitchChange());
} }
}
private static void handleSlimeLook(SlimeEntity slime, ServerPlayerEntity player) {
float yawChange = calculateYawChangeToPlayer(slime, player);
((SlimeEntity.SlimeMoveControl) slime.getMoveControl()).look(slime.getYaw() + yawChange, false);
}
private static void handleSquidLook(SquidEntity squid, ServerPlayerEntity player) {
Vec3d toPlayer = calculateNormalizedDirection(squid, player);
float initialSwimStrength = 0.15f;
squid.setSwimmingVector(
(float) toPlayer.x * initialSwimStrength,
(float) toPlayer.y * initialSwimStrength,
(float) toPlayer.z * initialSwimStrength
);
double distanceToPlayer = squid.getPos().distanceTo(player.getPos());
if (distanceToPlayer < 3.5F) {
// Stop motion when close
squid.setVelocity(0,0,0);
}
}
public static float calculateYawChangeToPlayer(MobEntity entity, ServerPlayerEntity player) {
Vec3d toPlayer = calculateNormalizedDirection(entity, player);
float targetYaw = (float) Math.toDegrees(Math.atan2(toPlayer.z, toPlayer.x)) - 90.0F;
float yawDifference = MathHelper.wrapDegrees(targetYaw - entity.getYaw());
return MathHelper.clamp(yawDifference, -10.0F, 10.0F);
}
public static Vec3d calculateNormalizedDirection(MobEntity entity, ServerPlayerEntity player) {
Vec3d playerPos = player.getPos();
Vec3d entityPos = entity.getPos();
return playerPos.subtract(entityPos).normalize();
} }
} }
\ No newline at end of file
...@@ -46,7 +46,7 @@ public class FollowPlayerGoal extends Goal { ...@@ -46,7 +46,7 @@ public class FollowPlayerGoal extends Goal {
@Override @Override
public void tick() { public void tick() {
// Look at the player and start moving towards them // Look at the player and start moving towards them
LookControls.LookAtEntity(this.targetPlayer, this.entity); LookControls.lookAtPlayer(this.targetPlayer, this.entity);
this.navigation.startMovingTo(this.targetPlayer, this.speed); this.navigation.startMovingTo(this.targetPlayer, this.speed);
} }
} }
...@@ -51,7 +51,7 @@ public class TalkPlayerGoal extends Goal { ...@@ -51,7 +51,7 @@ public class TalkPlayerGoal extends Goal {
@Override @Override
public void tick() { public void tick() {
// Make the entity look at the player without moving towards them // Make the entity look at the player without moving towards them
LookControls.LookAtEntity(this.targetPlayer, this.entity); LookControls.lookAtPlayer(this.targetPlayer, this.entity);
// Continuously stop the entity's navigation to ensure it remains stationary // Continuously stop the entity's navigation to ensure it remains stationary
this.navigation.stop(); this.navigation.stop();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment