use of org.apollo.game.model.area.RegionRepository in project apollo by apollo-rsps.
the class CollisionManagerTests method createCollisionManager.
/**
* Sets up dependencies for and creates a stub {@link CollisionManager}, then builds the collision matrices
* using the {@code objects} given.
*
* @param objects The objects to build collision matrices from.
* @return A new {@link CollisionManager} with a valid {@link RegionRepository} and every {@link CollisionMatrix}
* built.
*/
private static CollisionManager createCollisionManager(MapObject... objects) {
World world = new World();
RegionRepository regions = world.getRegionRepository();
CollisionManager collisionManager = world.getCollisionManager();
for (MapObject object : objects) {
// treat local coordinates as absolute for simplicity
int x = object.getLocalX(), y = object.getLocalY();
int height = object.getHeight();
Position position = new Position(x, y, height);
Region region = regions.fromPosition(position);
region.addEntity(new StaticGameObject(world, object.getId(), position, object.getType(), object.getOrientation()), false);
}
collisionManager.build(false);
return collisionManager;
}
use of org.apollo.game.model.area.RegionRepository in project apollo by apollo-rsps.
the class NpcSynchronizationTask method run.
@Override
public void run() {
List<Npc> locals = player.getLocalNpcList();
List<SynchronizationSegment> segments = new ArrayList<>();
int originalCount = locals.size();
final Position playerPosition = player.getPosition();
int distance = player.getViewingDistance();
for (Iterator<Npc> iterator = locals.iterator(); iterator.hasNext(); ) {
Npc npc = iterator.next();
Position position = npc.getPosition();
if (!npc.isActive() || npc.isTeleporting() || position.getLongestDelta(playerPosition) > distance || !position.isWithinDistance(playerPosition, distance)) {
iterator.remove();
segments.add(new RemoveMobSegment());
} else {
segments.add(new MovementSegment(npc.getBlockSet(), npc.getDirections()));
}
}
int added = 0, count = locals.size();
RegionRepository repository = player.getWorld().getRegionRepository();
Region current = repository.fromPosition(playerPosition);
Set<RegionCoordinates> regions = current.getSurrounding();
regions.add(current.getCoordinates());
Stream<Npc> npcs = regions.stream().map(repository::get).flatMap(region -> region.getEntities(EntityType.NPC));
Iterator<Npc> iterator = npcs.iterator();
while (iterator.hasNext()) {
if (count >= MAXIMUM_LOCAL_NPCS) {
player.flagExcessiveNpcs();
break;
} else if (added >= NEW_NPCS_PER_CYCLE) {
break;
}
Npc npc = iterator.next();
Position position = npc.getPosition();
if (position.isWithinDistance(playerPosition, distance) && !locals.contains(npc)) {
locals.add(npc);
count++;
added++;
npc.turnTo(npc.getFacingPosition());
segments.add(new AddNpcSegment(npc.getBlockSet(), npc.getIndex(), position, npc.getId()));
}
}
player.send(new NpcSynchronizationMessage(playerPosition, segments, originalCount));
}
Aggregations