Search in sources :

Example 1 with MapObject

use of org.apollo.cache.map.MapObject in project apollo by apollo-rsps.

the class WorldObjectsDecoder method run.

/**
 * Decode the {@code MapObject}s from the cache and register them with the world.
 */
@Override
public void run() {
    Map<Integer, MapIndex> mapIndices = MapIndex.getIndices();
    try {
        for (MapIndex index : mapIndices.values()) {
            MapObjectsDecoder decoder = MapObjectsDecoder.create(fs, index);
            List<MapObject> objects = decoder.decode();
            int mapX = index.getX(), mapY = index.getY();
            for (MapObject object : objects) {
                Position position = new Position(mapX + object.getLocalX(), mapY + object.getLocalY(), object.getHeight());
                StaticGameObject gameObject = new StaticGameObject(world, object.getId(), position, object.getType(), object.getOrientation());
                regionRepository.fromPosition(position).addEntity(gameObject, false);
            }
        }
    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
}
Also used : Position(org.apollo.game.model.Position) MapObjectsDecoder(org.apollo.cache.map.MapObjectsDecoder) UncheckedIOException(java.io.UncheckedIOException) MapIndex(org.apollo.cache.map.MapIndex) StaticGameObject(org.apollo.game.model.entity.obj.StaticGameObject) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) MapObject(org.apollo.cache.map.MapObject)

Example 2 with MapObject

use of org.apollo.cache.map.MapObject in project apollo by apollo-rsps.

the class MapObjectsDecoder method decode.

/**
 * Decodes the data in the {@code buffer} to a list of {@link MapObject}s.
 *
 * @return A list of decoded {@link MapObject}s.
 */
public List<MapObject> decode() {
    List<MapObject> objects = new ArrayList<>();
    int id = -1;
    int idOffset = BufferUtil.readSmart(buffer);
    while (idOffset != 0) {
        id += idOffset;
        int packed = 0;
        int positionOffset = BufferUtil.readSmart(buffer);
        while (positionOffset != 0) {
            packed += positionOffset - 1;
            int attributes = buffer.get() & 0xFF;
            int type = attributes >> 2;
            int orientation = attributes & 0x3;
            objects.add(new MapObject(id, packed, type, orientation));
            positionOffset = BufferUtil.readSmart(buffer);
        }
        idOffset = BufferUtil.readSmart(buffer);
    }
    return objects;
}
Also used : ArrayList(java.util.ArrayList) MapObject(org.apollo.cache.map.MapObject)

Example 3 with MapObject

use of org.apollo.cache.map.MapObject 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;
}
Also used : Position(org.apollo.game.model.Position) RegionRepository(org.apollo.game.model.area.RegionRepository) Region(org.apollo.game.model.area.Region) StaticGameObject(org.apollo.game.model.entity.obj.StaticGameObject) World(org.apollo.game.model.World) MapObject(org.apollo.cache.map.MapObject)

Aggregations

MapObject (org.apollo.cache.map.MapObject)3 Position (org.apollo.game.model.Position)2 StaticGameObject (org.apollo.game.model.entity.obj.StaticGameObject)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1 MapIndex (org.apollo.cache.map.MapIndex)1 MapObjectsDecoder (org.apollo.cache.map.MapObjectsDecoder)1 World (org.apollo.game.model.World)1 Region (org.apollo.game.model.area.Region)1 RegionRepository (org.apollo.game.model.area.RegionRepository)1