Added MeleeAttackGoal + fix velocity for players

This commit is contained in:
Felix Cravic 2020-08-09 05:13:36 +02:00
parent bcca8a67d1
commit 08b4b8576e
10 changed files with 90 additions and 12 deletions

View File

@ -671,7 +671,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
EntityVelocityEvent entityVelocityEvent = new EntityVelocityEvent(this, velocity);
callCancellableEvent(EntityVelocityEvent.class, entityVelocityEvent, () -> {
this.velocity.copy(entityVelocityEvent.getVelocity());
sendPacketToViewers(getVelocityPacket());
sendPacketToViewersAndSelf(getVelocityPacket());
});
}

View File

@ -109,7 +109,7 @@ public abstract class EntityCreature extends LivingEntity {
// Execute tick for the goal selector
if (currentGoalSelector != null) {
currentGoalSelector.tick();
currentGoalSelector.tick(time);
}
}

View File

@ -25,8 +25,10 @@ public abstract class GoalSelector {
/**
* Called every tick when this {@link GoalSelector} is running
*
* @param time the time of the update in milliseconds
*/
public abstract void tick();
public abstract void tick(long time);
/**
* Whether or not this {@link GoalSelector} should end.

View File

@ -48,7 +48,7 @@ public class DoNothingGoal extends GoalSelector {
}
@Override
public void tick() {
public void tick(long time) {
}
}

View File

@ -55,7 +55,7 @@ public class EatBlockGoal extends GoalSelector {
}
@Override
public void tick() {
public void tick(long time) {
this.eatAnimationTick = Math.max(0, this.eatAnimationTick - 1);
if (this.eatAnimationTick != 4) {
return;

View File

@ -7,8 +7,6 @@ import net.minestom.server.utils.Position;
public class FollowTargetGoal extends GoalSelector {
private Position lastPath;
public FollowTargetGoal(EntityCreature entityCreature) {
super(entityCreature);
}
@ -29,15 +27,15 @@ public class FollowTargetGoal extends GoalSelector {
return;
}
if (lastPath == null || lastPath.equals(targetPosition)) {
if (entityCreature.getPathPosition() == null ||
(!entityCreature.getPathPosition().isSimilar(targetPosition))) {
entityCreature.setPathTo(targetPosition);
lastPath = targetPosition;
}
}
}
@Override
public void tick() {
public void tick(long time) {
}

View File

@ -0,0 +1,68 @@
package net.minestom.server.entity.ai.goal;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityCreature;
import net.minestom.server.entity.ai.GoalSelector;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.time.CooldownUtils;
import net.minestom.server.utils.time.TimeUnit;
public class MeleeAttackGoal extends GoalSelector {
private long lastHit;
private int delay;
private TimeUnit timeUnit;
/**
* @param entityCreature the entity to add the goal to
* @param delay the delay between each attacks
* @param timeUnit the unit of the delay
*/
public MeleeAttackGoal(EntityCreature entityCreature, int delay, TimeUnit timeUnit) {
super(entityCreature);
this.delay = delay;
this.timeUnit = timeUnit;
}
@Override
public boolean shouldStart() {
return entityCreature.getTarget() != null;
}
@Override
public void start() {
final Position targetPosition = entityCreature.getTarget().getPosition();
entityCreature.setPathTo(targetPosition);
}
@Override
public void tick(long time) {
final Entity target = entityCreature.getTarget();
if (target != null) {
if (entityCreature.getBoundingBox().intersect(target)) {
if (!CooldownUtils.hasCooldown(time, lastHit, timeUnit, delay)) {
entityCreature.attack(target, true);
this.lastHit = time;
}
return;
}
final Position pathPosition = entityCreature.getPathPosition();
final Position targetPosition = target.getPosition();
if (pathPosition == null || !pathPosition.isSimilar(targetPosition)) {
entityCreature.setPathTo(targetPosition);
}
}
}
@Override
public boolean shouldEnd() {
return entityCreature.getTarget() == null;
}
@Override
public void end() {
}
}

View File

@ -67,7 +67,7 @@ public class RandomLookAroundGoal extends GoalSelector {
}
@Override
public void tick() {
public void tick(long time) {
--lookTime;
entityCreature.setView(entityCreature.getPosition().clone().setDirection(lookDirection));
}

View File

@ -44,7 +44,7 @@ public class RandomStrollGoal extends GoalSelector {
}
@Override
public void tick() {
public void tick(long time) {
}

View File

@ -189,6 +189,16 @@ public class Position {
Float.compare(position.pitch, pitch) == 0;
}
/**
* Check it two positions are similar (x/y/z)
*
* @param position the position to compare
* @return true if the two positions are similar
*/
public boolean isSimilar(Position position) {
return position.x == x && position.y == y && position.z == z;
}
@Override
public int hashCode() {
return Objects.hash(x, y, z, yaw, pitch);