Minestom/src/main/java/net/minestom/server/entity/ExperienceOrb.java

109 lines
3.0 KiB
Java
Raw Normal View History

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
import java.util.Comparator;
2019-08-31 07:54:53 +02:00
public class ExperienceOrb extends Entity {
private short experienceCount;
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);
//todo vanilla sets random velocity here?
2019-08-31 07:54:53 +02:00
this.experienceCount = experienceCount;
}
@Override
public void update(long time) {
2019-08-31 07:54:53 +02:00
// TODO slide toward nearest player
//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) {
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());
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)));
}
}
// 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-10-15 21:16:31 +02:00
* Gets the experience count.
2020-08-03 06:36:42 +02:00
*
* @return the experience count
*/
2019-08-31 07:54:53 +02:00
public short getExperienceCount() {
return experienceCount;
}
/**
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
*/
2019-08-31 07:54:53 +02:00
public void setExperienceCount(short experienceCount) {
// 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);
2019-08-31 07:54:53 +02:00
this.experienceCount = experienceCount;
2020-09-24 01:50:25 +02:00
getViewers().forEach(this::addViewer);
2019-08-31 07:54:53 +02:00
}
private Player getClosestPlayer(Entity entity, float maxDistance) {
Player closest = entity.getInstance()
.getPlayers()
.stream()
.min(Comparator.comparingDouble(a -> a.getDistance(entity)))
.orElse(null);
if (closest == null) return null;
if (closest.getDistance(entity) > maxDistance) return null;
return closest;
}
2019-08-31 07:54:53 +02:00
}