Search in sources :

Example 6 with Region

use of org.apollo.game.model.area.Region in project apollo by apollo-rsps.

the class Mob method setPosition.

/**
 * Sets the {@link Position} of this mob.
 * <p>
 * This method may be intercepted using a {@link MobPositionUpdateEvent}, which can be terminated like any
 * other. Plugins that intercept this Event <strong>must</strong> be cautious, because movement will not be
 * possible (even through mechanisms such as teleporting) if the Event is terminated.
 *
 * @param position The Position.
 */
public final void setPosition(Position position) {
    if (!position.equals(this.position) && world.submit(new MobPositionUpdateEvent(this, position))) {
        Position old = this.position;
        RegionRepository repository = world.getRegionRepository();
        Region current = repository.fromPosition(old), next = repository.fromPosition(position);
        current.removeEntity(this);
        // addEntity relies on the position being updated, so do that first.
        this.position = position;
        next.addEntity(this);
    }
}
Also used : MobPositionUpdateEvent(org.apollo.game.model.event.impl.MobPositionUpdateEvent) Position(org.apollo.game.model.Position) RegionRepository(org.apollo.game.model.area.RegionRepository) Region(org.apollo.game.model.area.Region)

Example 7 with Region

use of org.apollo.game.model.area.Region 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)

Example 8 with Region

use of org.apollo.game.model.area.Region in project apollo by apollo-rsps.

the class CollisionManager method build.

/**
 * Applies the initial {@link CollisionUpdate} to the {@link CollisionMatrix}es for all objects and tiles loaded
 * from the cache.
 *
 * @param rebuilding A flag indicating whether or not {@link CollisionMatrix}es are being rebuilt.
 */
public void build(boolean rebuilding) {
    if (rebuilding) {
        for (Region region : regions.getRegions()) {
            for (CollisionMatrix matrix : region.getMatrices()) {
                matrix.reset();
            }
        }
    }
    CollisionUpdate.Builder builder = new CollisionUpdate.Builder();
    builder.type(CollisionUpdateType.ADDING);
    for (Position tile : blocked) {
        int x = tile.getX(), y = tile.getY();
        int height = tile.getHeight();
        if (bridges.contains(new Position(x, y, 1))) {
            height--;
        }
        if (height >= 0) {
            builder.tile(new Position(x, y, height), false, Direction.NESW);
        }
    }
    apply(builder.build());
    for (Region region : regions.getRegions()) {
        CollisionUpdate.Builder objects = new CollisionUpdate.Builder();
        objects.type(CollisionUpdateType.ADDING);
        region.getEntities(STATIC_OBJECT, DYNAMIC_OBJECT).forEach(entity -> objects.object((GameObject) entity));
        apply(objects.build());
    }
}
Also used : Position(org.apollo.game.model.Position) GameObject(org.apollo.game.model.entity.obj.GameObject) Region(org.apollo.game.model.area.Region)

Example 9 with Region

use of org.apollo.game.model.area.Region in project apollo by apollo-rsps.

the class ItemOnObjectVerificationHandlerTests method terminateIfObjectOutOfRange.

@Test
public void terminateIfObjectOutOfRange() throws Exception {
    Position playerPosition = new Position(3200, 3200);
    Position objectPosition = new Position(3200, 3200);
    World world = mock(World.class);
    Region region = mock(Region.class);
    RegionRepository regionRepository = mock(RegionRepository.class);
    Player player = mock(Player.class);
    Set<Entity> entitySet = new HashSet<>();
    entitySet.add(new StaticGameObject(world, 4151, objectPosition, 0, 0));
    Inventory inventory = new Inventory(28);
    inventory.set(1, new Item(4151, 1));
    when(player.getInventory()).thenReturn(inventory);
    when(world.getRegionRepository()).thenReturn(regionRepository);
    when(regionRepository.fromPosition(objectPosition)).thenReturn(region);
    when(player.getPosition()).thenReturn(playerPosition);
    when(region.getEntities(objectPosition, EntityType.STATIC_OBJECT, EntityType.DYNAMIC_OBJECT)).thenReturn(entitySet);
    ItemOnObjectMessage itemOnObjectMessage = new ItemOnObjectMessage(SynchronizationInventoryListener.INVENTORY_ID, 4151, 1, 1, objectPosition.getX(), objectPosition.getY());
    ItemOnObjectVerificationHandler itemOnObjectVerificationHandler = new ItemOnObjectVerificationHandler(world);
    itemOnObjectVerificationHandler.handle(player, itemOnObjectMessage);
    assertTrue("ObjectVerificationHandler: message not terminated when object out of range!", itemOnObjectMessage.terminated());
}
Also used : Entity(org.apollo.game.model.entity.Entity) Player(org.apollo.game.model.entity.Player) Position(org.apollo.game.model.Position) ItemOnObjectMessage(org.apollo.game.message.impl.ItemOnObjectMessage) StaticGameObject(org.apollo.game.model.entity.obj.StaticGameObject) World(org.apollo.game.model.World) Item(org.apollo.game.model.Item) RegionRepository(org.apollo.game.model.area.RegionRepository) Region(org.apollo.game.model.area.Region) Inventory(org.apollo.game.model.inv.Inventory) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 10 with Region

use of org.apollo.game.model.area.Region in project apollo by apollo-rsps.

the class ItemOnObjectVerificationHandlerTests method terminateIfInvalidItem.

@Test
public void terminateIfInvalidItem() throws Exception {
    Position playerPosition = new Position(3200, 3200);
    Position objectPosition = new Position(3200, 3216);
    World world = mock(World.class);
    Region region = mock(Region.class);
    RegionRepository regionRepository = mock(RegionRepository.class);
    Player player = mock(Player.class);
    Set<Entity> entitySet = new HashSet<>();
    entitySet.add(new StaticGameObject(world, 4151, objectPosition, 0, 0));
    Inventory inventory = new Inventory(28);
    when(player.getInventory()).thenReturn(inventory);
    when(world.getRegionRepository()).thenReturn(regionRepository);
    when(regionRepository.fromPosition(objectPosition)).thenReturn(region);
    when(player.getPosition()).thenReturn(playerPosition);
    when(region.getEntities(objectPosition, EntityType.STATIC_OBJECT, EntityType.DYNAMIC_OBJECT)).thenReturn(entitySet);
    ItemOnObjectMessage itemOnObjectMessage = new ItemOnObjectMessage(SynchronizationInventoryListener.INVENTORY_ID, 4151, 1, 1, objectPosition.getX(), objectPosition.getY());
    ItemOnObjectVerificationHandler itemOnObjectVerificationHandler = new ItemOnObjectVerificationHandler(world);
    itemOnObjectVerificationHandler.handle(player, itemOnObjectMessage);
    assertTrue("ObjectVerificationHandler: message not terminated valid item given!", itemOnObjectMessage.terminated());
}
Also used : Entity(org.apollo.game.model.entity.Entity) Player(org.apollo.game.model.entity.Player) Position(org.apollo.game.model.Position) RegionRepository(org.apollo.game.model.area.RegionRepository) ItemOnObjectMessage(org.apollo.game.message.impl.ItemOnObjectMessage) Region(org.apollo.game.model.area.Region) StaticGameObject(org.apollo.game.model.entity.obj.StaticGameObject) World(org.apollo.game.model.World) Inventory(org.apollo.game.model.inv.Inventory) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Region (org.apollo.game.model.area.Region)19 Position (org.apollo.game.model.Position)14 RegionRepository (org.apollo.game.model.area.RegionRepository)9 World (org.apollo.game.model.World)6 Player (org.apollo.game.model.entity.Player)6 HashSet (java.util.HashSet)5 Entity (org.apollo.game.model.entity.Entity)5 StaticGameObject (org.apollo.game.model.entity.obj.StaticGameObject)5 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Inventory (org.apollo.game.model.inv.Inventory)4 ItemOnObjectMessage (org.apollo.game.message.impl.ItemOnObjectMessage)3 Npc (org.apollo.game.model.entity.Npc)3 GameObject (org.apollo.game.model.entity.obj.GameObject)3 ArrayList (java.util.ArrayList)2 ObjectActionMessage (org.apollo.game.message.impl.ObjectActionMessage)2 Direction (org.apollo.game.model.Direction)2 Item (org.apollo.game.model.Item)2 RegionCoordinates (org.apollo.game.model.area.RegionCoordinates)2 MovementSegment (org.apollo.game.sync.seg.MovementSegment)2