Search in sources :

Example 21 with Position

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);
}
Also used : Position(org.apollo.game.model.Position) ArrayDeque(java.util.ArrayDeque)

Example 22 with Position

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);
}
Also used : Position(org.apollo.game.model.Position)

Example 23 with Position

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);
    }
}
Also used : EntityType(org.apollo.game.model.entity.EntityType) Entity(org.apollo.game.model.entity.Entity) GroupableEntity(org.apollo.game.model.area.update.GroupableEntity) Position(org.apollo.game.model.Position)

Example 24 with Position

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);
}
Also used : EntityType(org.apollo.game.model.entity.EntityType) Entity(org.apollo.game.model.entity.Entity) GroupableEntity(org.apollo.game.model.area.update.GroupableEntity) Position(org.apollo.game.model.Position)

Example 25 with Position

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;
}
Also used : Position(org.apollo.game.model.Position) Region(org.apollo.game.model.area.Region) Direction(org.apollo.game.model.Direction)

Aggregations

Position (org.apollo.game.model.Position)66 Region (org.apollo.game.model.area.Region)14 RegionRepository (org.apollo.game.model.area.RegionRepository)10 GamePacketReader (org.apollo.net.codec.game.GamePacketReader)10 GamePacketBuilder (org.apollo.net.codec.game.GamePacketBuilder)9 ObjectActionMessage (org.apollo.game.message.impl.ObjectActionMessage)8 Direction (org.apollo.game.model.Direction)8 Entity (org.apollo.game.model.entity.Entity)8 HashSet (java.util.HashSet)7 World (org.apollo.game.model.World)7 Player (org.apollo.game.model.entity.Player)7 Test (org.junit.Test)7 StaticGameObject (org.apollo.game.model.entity.obj.StaticGameObject)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 ArrayList (java.util.ArrayList)3 ItemOnObjectMessage (org.apollo.game.message.impl.ItemOnObjectMessage)3 RegionCoordinates (org.apollo.game.model.area.RegionCoordinates)3 GroupableEntity (org.apollo.game.model.area.update.GroupableEntity)3 Inventory (org.apollo.game.model.inv.Inventory)3 MovementSegment (org.apollo.game.sync.seg.MovementSegment)3