2020-04-24 03:25:58 +02:00
|
|
|
package net.minestom.server.entity;
|
2019-08-31 07:54:53 +02:00
|
|
|
|
2021-07-08 13:48:28 +02:00
|
|
|
import net.minestom.server.coordinate.Vec;
|
2019-08-31 07:54:53 +02:00
|
|
|
|
2021-01-25 13:47:13 +01:00
|
|
|
import java.util.Comparator;
|
|
|
|
|
2019-08-31 07:54:53 +02:00
|
|
|
public class ExperienceOrb extends Entity {
|
|
|
|
|
|
|
|
private short experienceCount;
|
2020-12-31 19:50:26 +01:00
|
|
|
private Player target;
|
|
|
|
private long lastTargetUpdateTick;
|
|
|
|
|
2021-07-06 20:44:24 +02:00
|
|
|
public ExperienceOrb(short experienceCount) {
|
|
|
|
super(EntityType.EXPERIENCE_ORB);
|
2019-08-31 07:54:53 +02:00
|
|
|
setBoundingBox(0.5f, 0.5f, 0.5f);
|
2020-12-31 19:50:26 +01:00
|
|
|
//todo vanilla sets random velocity here?
|
2019-08-31 07:54:53 +02:00
|
|
|
this.experienceCount = experienceCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-06-01 00:51:31 +02:00
|
|
|
public void update(long time) {
|
2020-12-31 19:50:26 +01:00
|
|
|
|
2019-08-31 07:54:53 +02:00
|
|
|
// TODO slide toward nearest player
|
2020-12-31 19:50:26 +01:00
|
|
|
|
|
|
|
//todo water movement
|
|
|
|
if (hasNoGravity()) {
|
|
|
|
setVelocity(getVelocity().add(0, -0.3f, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
//todo lava
|
|
|
|
|
|
|
|
double d = 8.0;
|
|
|
|
if (lastTargetUpdateTick < time - 20 + getEntityId() % 100) {
|
2021-07-06 20:44:24 +02:00
|
|
|
if (target == null || target.getPosition().distanceSquared(getPosition()) > 64) {
|
2020-12-31 19:50:26 +01:00
|
|
|
this.target = getClosestPlayer(this, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
lastTargetUpdateTick = time;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target != null && target.getGameMode() == GameMode.SPECTATOR) {
|
|
|
|
target = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target != null) {
|
2021-07-06 20:44:24 +02:00
|
|
|
final var pos = getPosition();
|
|
|
|
final var targetPos = target.getPosition();
|
|
|
|
final Vec toTarget = new Vec(targetPos.x() - pos.x(), targetPos.y() + (target.getEyeHeight() / 2) - pos.y(), targetPos.z() - pos.z());
|
2020-12-31 19:50:26 +01:00
|
|
|
double e = toTarget.length(); //could really be lengthSquared
|
|
|
|
if (e < 8) {
|
|
|
|
double f = 1 - (e / 8);
|
2021-07-06 20:44:24 +02:00
|
|
|
setVelocity(getVelocity().add(toTarget.normalize().mul(f * f * 0.1)));
|
2020-12-31 19:50:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move should be called here
|
|
|
|
float g = 0.98f;
|
|
|
|
if (this.onGround) {
|
|
|
|
// g = 2f;
|
|
|
|
g = 0.6f * 0.98f;
|
|
|
|
}
|
|
|
|
// apply slipperiness
|
|
|
|
|
2021-07-06 20:44:24 +02:00
|
|
|
setVelocity(getVelocity().mul(new Vec(g, 0.98f, g)));
|
|
|
|
if (isOnGround()) {
|
|
|
|
setVelocity(getVelocity().mul(new Vec(1, -0.9f, 1)));
|
|
|
|
}
|
2019-08-31 07:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void spawn() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-24 20:21:17 +02:00
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Gets the experience count.
|
2020-08-03 06:36:42 +02:00
|
|
|
*
|
|
|
|
* @return the experience count
|
2020-05-24 20:21:17 +02:00
|
|
|
*/
|
2019-08-31 07:54:53 +02:00
|
|
|
public short getExperienceCount() {
|
|
|
|
return experienceCount;
|
|
|
|
}
|
|
|
|
|
2020-05-24 20:21:17 +02:00
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Changes the experience count.
|
2020-08-03 06:36:42 +02:00
|
|
|
*
|
|
|
|
* @param experienceCount the new experience count
|
2020-05-24 20:21:17 +02:00
|
|
|
*/
|
2019-08-31 07:54:53 +02:00
|
|
|
public void setExperienceCount(short experienceCount) {
|
2020-05-24 20:21:17 +02:00
|
|
|
// Remove the entity in order to respawn it with the correct experience count
|
2020-09-24 01:50:25 +02:00
|
|
|
getViewers().forEach(this::removeViewer);
|
2020-05-24 20:21:17 +02:00
|
|
|
|
2019-08-31 07:54:53 +02:00
|
|
|
this.experienceCount = experienceCount;
|
2020-05-24 20:21:17 +02:00
|
|
|
|
2020-09-24 01:50:25 +02:00
|
|
|
getViewers().forEach(this::addViewer);
|
2019-08-31 07:54:53 +02:00
|
|
|
}
|
2020-12-31 19:50:26 +01:00
|
|
|
|
|
|
|
private Player getClosestPlayer(Entity entity, float maxDistance) {
|
|
|
|
Player closest = entity.getInstance()
|
|
|
|
.getPlayers()
|
|
|
|
.stream()
|
2021-01-25 13:47:13 +01:00
|
|
|
.min(Comparator.comparingDouble(a -> a.getDistance(entity)))
|
2020-12-31 19:50:26 +01:00
|
|
|
.orElse(null);
|
|
|
|
if (closest == null) return null;
|
|
|
|
if (closest.getDistance(entity) > maxDistance) return null;
|
|
|
|
return closest;
|
|
|
|
}
|
2019-08-31 07:54:53 +02:00
|
|
|
}
|