use of org.apollo.game.model.Position in project apollo by apollo-rsps.
the class WalkingQueue method addFirstStep.
/**
* Adds a first step into this WalkingQueue.
*
* @param next The {@link Position} of the step.
*/
public void addFirstStep(Position next) {
points.clear();
running = false;
/*
* We need to connect 'current' and 'next' whilst accounting for the
* fact that the client and server might be out of sync (i.e. what the
* client thinks is 'current' is different to what the server thinks is
* 'current').
*
* First try to connect them via points from the previous queue.
*/
Queue<Position> backtrack = new ArrayDeque<>();
while (!previousPoints.isEmpty()) {
Position position = previousPoints.pollLast();
backtrack.add(position);
if (position.equals(next)) {
backtrack.forEach(this::addStep);
previousPoints.clear();
return;
}
}
/* If that doesn't work, connect the points directly. */
previousPoints.clear();
addStep(next);
}
use of org.apollo.game.model.Position in project apollo by apollo-rsps.
the class WalkingQueue method addStep.
/**
* Adds a step to this WalkingQueue.
*
* @param next The {@link Position} of the step.
*/
public void addStep(Position next) {
Position current = points.peekLast();
/*
* If current equals next, addFirstStep doesn't end up adding anything points queue. This makes peekLast()
* return null. If it does, the correct behaviour is to fill it in with mob.getPosition().
*/
if (current == null) {
current = mob.getPosition();
}
addStep(current, next);
}
use of org.apollo.game.model.Position in project apollo by apollo-rsps.
the class Region method addEntity.
/**
* Adds a {@link Entity} to the Region. Note that this does not spawn the Entity, or do any other action other than
* register it to this Region.
*
* @param entity The Entity.
* @param notify Whether or not the {@link RegionListener}s for this Region should be notified.
* @throws IllegalArgumentException If the Entity does not belong in this Region.
*/
public void addEntity(Entity entity, boolean notify) {
EntityType type = entity.getEntityType();
Position position = entity.getPosition();
checkPosition(position);
if (!type.isTransient()) {
Set<Entity> local = entities.computeIfAbsent(position, key -> new HashSet<>(DEFAULT_LIST_SIZE));
local.add(entity);
}
if (notify) {
notifyListeners(entity, EntityUpdateType.ADD);
}
}
use of org.apollo.game.model.Position in project apollo by apollo-rsps.
the class Region method removeEntity.
/**
* Removes an {@link Entity} from this Region.
*
* @param entity The Entity.
* @throws IllegalArgumentException If the Entity does not belong in this Region, or if it was never added.
*/
public void removeEntity(Entity entity) {
EntityType type = entity.getEntityType();
if (type.isTransient()) {
throw new IllegalArgumentException("Tried to remove a transient Entity (" + entity + ") from " + "(" + this + ").");
}
Position position = entity.getPosition();
checkPosition(position);
Set<Entity> local = entities.get(position);
if (local == null || !local.remove(entity)) {
throw new IllegalArgumentException("Entity (" + entity + ") belongs in (" + this + ") but does not exist.");
}
notifyListeners(entity, EntityUpdateType.REMOVE);
}
use of org.apollo.game.model.Position in project apollo by apollo-rsps.
the class CollisionManager method traversable.
/**
* Checks if the given {@link EntityType} can traverse to the next tile from {@code position} in the given
* {@code direction}.
*
* @param position The current position of the entity.
* @param type The type of the entity.
* @param direction The direction the entity is travelling.
* @return {@code true} if next tile is traversable, {@code false} otherwise.
*/
public boolean traversable(Position position, EntityType type, Direction direction) {
Position next = position.step(1, direction);
Region region = regions.fromPosition(next);
if (!region.traversable(next, type, direction)) {
return false;
}
if (direction.isDiagonal()) {
for (Direction component : Direction.diagonalComponents(direction)) {
next = position.step(1, component);
if (!region.contains(next)) {
region = regions.fromPosition(next);
}
if (!region.traversable(next, type, component)) {
return false;
}
}
}
return true;
}
Aggregations