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);
}
}
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;
}
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());
}
}
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());
}
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());
}
Aggregations