p a t h f i n d i n g

This commit is contained in:
FatherToast 2022-07-12 14:23:25 -05:00
parent 69f183b707
commit 447da324d0
3 changed files with 39 additions and 26 deletions

View file

@ -89,7 +89,6 @@ public class SpecialMobs {
* ? piglins
* ? hoglins
* ? zoglins
* ? endermites
* ? guardians
* ? vortex
* ? shulkers

View file

@ -1,18 +1,19 @@
package fathertoast.specialmobs.common.entity.ai.goal;
import fathertoast.specialmobs.common.entity.MobHelper;
import fathertoast.specialmobs.common.entity.zombie.MadScientistZombieEntity;
import net.minecraft.entity.MobEntity;
import net.minecraft.entity.ai.goal.Goal;
import net.minecraft.entity.monster.CreeperEntity;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World;
import java.util.EnumSet;
import java.util.List;
import java.util.function.BiPredicate;
public class ChargeCreeperGoal<T extends MadScientistZombieEntity> extends Goal {
public class ChargeCreeperGoal<T extends MobEntity> extends Goal {
private final BiPredicate<T, ? super CreeperEntity> targetPredicate;
@ -23,9 +24,10 @@ public class ChargeCreeperGoal<T extends MadScientistZombieEntity> extends Goal
/** The creeper to target for power-up injection **/
private CreeperEntity creeper;
private int pathUpdateCooldown;
private Vector3d pathTarget = Vector3d.ZERO;
private boolean canUseWhileMounted = false;
public ChargeCreeperGoal( T madman, double movementSpeed, double targetRange, BiPredicate<T, ? super CreeperEntity> targetPredicate ) {
this.madman = madman;
this.movementSpeed = movementSpeed;
@ -44,8 +46,14 @@ public class ChargeCreeperGoal<T extends MadScientistZombieEntity> extends Goal
@Override
public boolean canUse() {
if( madman.isPassenger() || !canUseWhileMounted && madman.isVehicle() ) return false;
findCreeper();
return creeper != null;
if( creeper == null ) return false;
madman.getNavigation().moveTo( creeper, movementSpeed );
pathTarget = creeper.position();
pathUpdateCooldown = 4 + madman.getRandom().nextInt( 7 );
return madman.getNavigation().getPath() != null;
}
private void findCreeper() {
@ -62,33 +70,36 @@ public class ChargeCreeperGoal<T extends MadScientistZombieEntity> extends Goal
}
}
/** Called when this AI is activated. */
@Override
public void start() {
madman.getNavigation().moveTo( creeper, movementSpeed );
}
/** @return Called each update while active and returns true if this AI can remain active. */
@Override
public boolean canContinueToUse() {
return !madman.isOnGround() && !madman.isPassenger() && !madman.isInWaterOrBubble() && !madman.isInLava();
return !madman.isPassenger() && (canUseWhileMounted || madman.isVehicle()) && creeper != null && targetPredicate.test( madman, creeper );
}
/** Called each tick while this AI is active. */
@Override
public void tick() {
if( creeper == null || !targetPredicate.test( madman, creeper ) ) {
findCreeper();
}
else {
madman.getNavigation().moveTo( creeper, movementSpeed );
madman.getLookControl().setLookAt( this.creeper.getX(), this.creeper.getEyeY(), this.creeper.getZ() );
if( creeper == null ) return;
final double distanceSq = madman.distanceToSqr( creeper );
pathUpdateCooldown--;
if( pathUpdateCooldown <= 0 && (creeper.distanceToSqr( pathTarget ) >= 1.0 || madman.getRandom().nextFloat() < 0.05F) ) {
pathUpdateCooldown = 4 + madman.getRandom().nextInt( 7 );
if( distanceSq > 1024.0 ) pathUpdateCooldown += 10;
else if( distanceSq > 256.0 ) pathUpdateCooldown += 5;
if( madman.distanceTo( creeper ) < 1.5D ) {
MobHelper.charge( creeper );
madman.level.playSound( null, creeper.getX() + 0.5D, creeper.getY(), creeper.getZ() + 0.5D, SoundEvents.BEE_STING, SoundCategory.HOSTILE, 0.9F, 1.0F );
creeper = null;
}
if( !madman.getNavigation().moveTo( creeper, movementSpeed ) ) pathUpdateCooldown += 15;
pathTarget = creeper.position();
}
madman.getLookControl().setLookAt( creeper, 30.0F, 30.0F );
if( distanceSq < 2.5 ) {
MobHelper.charge( creeper );
madman.level.playSound( null, creeper.getX(), creeper.getY(), creeper.getZ(),
SoundEvents.BEE_STING, SoundCategory.HOSTILE, 0.9F, 1.0F );
creeper = null;
}
}
}

View file

@ -12,6 +12,7 @@ import fathertoast.specialmobs.datagen.loot.LootTableBuilder;
import mcp.MethodsReturnNonnullByDefault;
import net.minecraft.entity.*;
import net.minecraft.entity.ai.attributes.Attributes;
import net.minecraft.entity.monster.CreeperEntity;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.EffectInstance;
@ -22,6 +23,7 @@ import net.minecraft.world.World;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.function.BiPredicate;
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
@ -63,14 +65,15 @@ public class MadScientistZombieEntity extends _SpecialZombieEntity {
//--------------- Variant-Specific Implementations ----------------
private final BiPredicate<MadScientistZombieEntity, ? super CreeperEntity> CHARGE_CREEPER_TARGET = ( madman, creeper ) ->
creeper.isAlive() && !creeper.isPowered() && madman.getSensing().canSee( creeper );
public MadScientistZombieEntity( EntityType<? extends _SpecialZombieEntity> entityType, World world ) { super( entityType, world ); }
/** Override to change this entity's AI goals. */
@Override
protected void registerVariantGoals() {
AIHelper.insertGoal( goalSelector, 2, new ChargeCreeperGoal<>(
this, getAttributeValue( Attributes.MOVEMENT_SPEED ) * 1.25, 20.0,
( madman, creeper ) -> creeper.isAlive() && !creeper.isPowered() && madman.getSensing().canSee( creeper ) ) );
AIHelper.insertGoal( goalSelector, 2, new ChargeCreeperGoal<>( this, 1.25, 20.0, CHARGE_CREEPER_TARGET ) );
}
/** Override to change this entity's attack goal priority. */