2016-08-15 17:09:01 -04:00
|
|
|
package WayofTime.bloodmagic.entity.ai;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
import WayofTime.bloodmagic.entity.mob.EntityDemonBase;
|
2016-08-15 17:09:01 -04:00
|
|
|
import net.minecraft.entity.EntityLivingBase;
|
|
|
|
import net.minecraft.entity.ai.EntityAIBase;
|
|
|
|
import net.minecraft.item.ItemBow;
|
|
|
|
import net.minecraft.util.EnumHand;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public class EntityAIAttackRangedBow extends EntityAIBase {
|
2016-08-29 18:56:21 -04:00
|
|
|
private final EntityDemonBase entity;
|
2016-08-15 17:09:01 -04:00
|
|
|
private final double moveSpeedAmp;
|
|
|
|
private final float maxAttackDistance;
|
2017-08-15 21:30:48 -07:00
|
|
|
private int attackCooldown;
|
2016-08-15 17:09:01 -04:00
|
|
|
private int attackTime = -1;
|
|
|
|
private int seeTime;
|
|
|
|
private boolean strafingClockwise;
|
|
|
|
private boolean strafingBackwards;
|
|
|
|
private int strafingTime = -1;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public EntityAIAttackRangedBow(EntityDemonBase entityDemonBase, double speedAmplifier, int delay, float maxDistance) {
|
2016-08-29 18:56:21 -04:00
|
|
|
this.entity = entityDemonBase;
|
2016-08-15 17:09:01 -04:00
|
|
|
this.moveSpeedAmp = speedAmplifier;
|
|
|
|
this.attackCooldown = delay;
|
|
|
|
this.maxAttackDistance = maxDistance * maxDistance;
|
|
|
|
this.setMutexBits(3);
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public void setAttackCooldown(int p_189428_1_) {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.attackCooldown = p_189428_1_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the EntityAIBase should begin execution.
|
|
|
|
*/
|
2017-08-15 21:30:48 -07:00
|
|
|
public boolean shouldExecute() {
|
2016-12-12 19:56:36 -08:00
|
|
|
return this.entity.getAttackTarget() != null && this.isBowInMainhand();
|
2016-08-15 17:09:01 -04:00
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
protected boolean isBowInMainhand() {
|
2016-12-12 19:56:36 -08:00
|
|
|
return this.entity.getHeldItemMainhand().getItem() instanceof ItemBow;
|
2016-08-15 17:09:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether an in-progress EntityAIBase should continue executing
|
|
|
|
*/
|
2017-08-15 21:30:48 -07:00
|
|
|
public boolean continueExecuting() {
|
2016-08-15 17:09:01 -04:00
|
|
|
return (this.shouldExecute() || !this.entity.getNavigator().noPath()) && this.isBowInMainhand();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a one shot task or start executing a continuous task
|
|
|
|
*/
|
2017-08-15 21:30:48 -07:00
|
|
|
public void startExecuting() {
|
2016-08-15 17:09:01 -04:00
|
|
|
super.startExecuting();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the task
|
|
|
|
*/
|
2017-08-15 21:30:48 -07:00
|
|
|
public void resetTask() {
|
2016-08-15 17:09:01 -04:00
|
|
|
super.startExecuting();
|
|
|
|
this.seeTime = 0;
|
|
|
|
this.attackTime = -1;
|
|
|
|
this.entity.resetActiveHand();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the task
|
|
|
|
*/
|
2017-08-15 21:30:48 -07:00
|
|
|
public void updateTask() {
|
2016-08-15 17:09:01 -04:00
|
|
|
EntityLivingBase entitylivingbase = this.entity.getAttackTarget();
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (entitylivingbase != null) {
|
2016-08-15 17:09:01 -04:00
|
|
|
double d0 = this.entity.getDistanceSq(entitylivingbase.posX, entitylivingbase.getEntityBoundingBox().minY, entitylivingbase.posZ);
|
|
|
|
boolean flag = this.entity.getEntitySenses().canSee(entitylivingbase);
|
|
|
|
boolean flag1 = this.seeTime > 0;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (flag != flag1) {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.seeTime = 0;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (flag) {
|
2016-08-15 17:09:01 -04:00
|
|
|
++this.seeTime;
|
2017-08-15 21:30:48 -07:00
|
|
|
} else {
|
2016-08-15 17:09:01 -04:00
|
|
|
--this.seeTime;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (d0 <= (double) this.maxAttackDistance && this.seeTime >= 20) {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.entity.getNavigator().clearPathEntity();
|
|
|
|
++this.strafingTime;
|
2017-08-15 21:30:48 -07:00
|
|
|
} else {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.entity.getNavigator().tryMoveToEntityLiving(entitylivingbase, this.moveSpeedAmp);
|
|
|
|
this.strafingTime = -1;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (this.strafingTime >= 20) {
|
|
|
|
if ((double) this.entity.getRNG().nextFloat() < 0.3D) {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.strafingClockwise = !this.strafingClockwise;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if ((double) this.entity.getRNG().nextFloat() < 0.3D) {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.strafingBackwards = !this.strafingBackwards;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.strafingTime = 0;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (this.strafingTime > -1) {
|
|
|
|
if (d0 > (double) (this.maxAttackDistance * 0.75F)) {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.strafingBackwards = false;
|
2017-08-15 21:30:48 -07:00
|
|
|
} else if (d0 < (double) (this.maxAttackDistance * 0.25F)) {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.strafingBackwards = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.entity.getMoveHelper().strafe(this.strafingBackwards ? -0.5F : 0.5F, this.strafingClockwise ? 0.5F : -0.5F);
|
|
|
|
this.entity.faceEntity(entitylivingbase, 30.0F, 30.0F);
|
2017-08-15 21:30:48 -07:00
|
|
|
} else {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.entity.getLookHelper().setLookPositionWithEntity(entitylivingbase, 30.0F, 30.0F);
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (this.entity.isHandActive()) {
|
|
|
|
if (!flag && this.seeTime < -60) {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.entity.resetActiveHand();
|
2017-08-15 21:30:48 -07:00
|
|
|
} else if (flag) {
|
2016-08-15 17:09:01 -04:00
|
|
|
int i = this.entity.getItemInUseMaxCount();
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (i >= 20) {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.entity.resetActiveHand();
|
|
|
|
this.entity.attackEntityWithRangedAttack(entitylivingbase, ItemBow.getArrowVelocity(i));
|
|
|
|
this.attackTime = this.attackCooldown;
|
|
|
|
}
|
|
|
|
}
|
2017-08-15 21:30:48 -07:00
|
|
|
} else if (--this.attackTime <= 0 && this.seeTime >= -60) {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.entity.setActiveHand(EnumHand.MAIN_HAND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|