Added power param for projectiles shooting

This commit is contained in:
Konstantin Shandurenko 2021-02-22 15:54:35 +03:00
parent 504e8cafb4
commit dbd0e472c2
4 changed files with 34 additions and 11 deletions

View File

@ -22,6 +22,7 @@ public class RangedAttackGoal extends GoalSelector {
private final int attackRangeSquared;
private final int desirableRangeSquared;
private final boolean comeClose;
private final double power;
private final double spread;
private BiFunction<Entity, Position, Projectile> projectileGenerator;
@ -35,15 +36,17 @@ public class RangedAttackGoal extends GoalSelector {
* @param desirableRange the desirable range: the entity will try to stay no further than this distance.
* @param comeClose whether entity should go as close as possible to the target whether target is not in line of sight.
* @param spread shot spread (0 for best accuracy).
* @param power shot power (1 for normal).
* @param timeUnit the unit of the delay.
*/
public RangedAttackGoal(@NotNull EntityCreature entityCreature, int delay, int attackRange, int desirableRange, boolean comeClose, double spread, @NotNull TimeUnit timeUnit) {
public RangedAttackGoal(@NotNull EntityCreature entityCreature, int delay, int attackRange, int desirableRange, boolean comeClose, double power, double spread, @NotNull TimeUnit timeUnit) {
super(entityCreature);
this.delay = delay;
this.timeUnit = timeUnit;
this.attackRangeSquared = attackRange * attackRange;
this.desirableRangeSquared = desirableRange * desirableRange;
this.comeClose = comeClose;
this.power = power;
this.spread = spread;
Check.argCondition(desirableRange > attackRange, "Desirable range can not exceed attack range!");
}
@ -84,7 +87,7 @@ public class RangedAttackGoal extends GoalSelector {
}
Projectile projectile = projectileGenerator.apply(this.entityCreature, new Position(0D, 0D, 0D));
Projectile.shoot(projectile, this.entityCreature, to, this.spread);
Projectile.shoot(projectile, this.entityCreature, to, this.power, this.spread);
this.lastShot = time;
} else {
comeClose = this.comeClose;

View File

@ -12,9 +12,9 @@ import java.util.concurrent.ThreadLocalRandom;
public interface Projectile {
static void shoot(@NotNull Projectile projectile, @NotNull Entity shooter, Position to, double spread) {
static void shoot(@NotNull Projectile projectile, @NotNull Entity shooter, Position to, double power, double spread) {
Check.argCondition(!(projectile instanceof Entity), "Projectile must be an instance of Entity!");
EntityShootEvent event = new EntityShootEvent(shooter, projectile, to, spread);
EntityShootEvent event = new EntityShootEvent(shooter, projectile, to, power, spread);
shooter.callEvent(EntityShootEvent.class, event);
if (event.isCancelled()) {
Entity proj = (Entity) projectile;
@ -22,11 +22,11 @@ public interface Projectile {
return;
}
Position from = shooter.getPosition().clone().add(0D, shooter.getEyeHeight(), 0D);
shoot(projectile, from, to, event.getSpread());
shoot(projectile, from, to, event.getPower(), event.getSpread());
}
@SuppressWarnings("ConstantConditions")
static void shoot(@NotNull Projectile projectile, @NotNull Position from, @NotNull Position to, double spread) {
static void shoot(@NotNull Projectile projectile, @NotNull Position from, @NotNull Position to, double power, double spread) {
Check.argCondition(!(projectile instanceof Entity), "Projectile must be an instance of Entity!");
Entity proj = (Entity) projectile;
double dx = to.getX() - from.getX();
@ -48,7 +48,7 @@ public interface Projectile {
velocity.setX(dx);
velocity.setY(dy);
velocity.setZ(dz);
velocity.multiply(20);
velocity.multiply(20 * power);
proj.setView(
(float) Math.toDegrees(Math.atan2(dx, dz)),
(float) Math.toDegrees(Math.atan2(dy, Math.sqrt(dx * dx + dz * dz)))

View File

@ -13,15 +13,17 @@ import org.jetbrains.annotations.NotNull;
public class EntityShootEvent extends EntityEvent implements CancellableEvent {
private final Projectile projectile;
private final Position to;
private double spread;
private final Position to;
private double power;
private double spread;
private boolean cancelled;
public EntityShootEvent(@NotNull Entity entity, @NotNull Projectile projectile, @NotNull Position to, double spread) {
public EntityShootEvent(@NotNull Entity entity, @NotNull Projectile projectile, @NotNull Position to, double power, double spread) {
super(entity);
this.projectile = projectile;
this.to = to;
this.power = power;
this.spread = spread;
}
@ -61,6 +63,24 @@ public class EntityShootEvent extends EntityEvent implements CancellableEvent {
this.spread = spread;
}
/**
* Gets shot power.
*
* @return shot power.
*/
public double getPower() {
return this.power;
}
/**
* Sets shot power.
*
* @param power shot power.
*/
public void setPower(double power) {
this.power = power;
}
@Override
public boolean isCancelled() {
return this.cancelled;

View File

@ -62,6 +62,6 @@ public class ShootCommand extends Command {
((Entity) projectile).setInstance(player.getInstance());
var dir = pos.getDirection().multiply(30D);
pos = pos.clone().add(dir.getX(), dir.getY(), dir.getZ());
Projectile.shoot(projectile, player, pos, 0D);
Projectile.shoot(projectile, player, pos, 1D, 0D);
}
}