Search in sources :

Example 6 with World

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

the class MessageHandlerChainSetParser method parse.

/**
 * Parses the XML and produces a group of {@link MessageHandlerChain}s.
 *
 * @param world The {@link World} this MessageHandlerChainGroup is for.
 * @return A {@link MessageHandlerChainSet}.
 * @throws IOException If an I/O error occurs.
 * @throws SAXException If a SAX error occurs.
 * @throws ReflectiveOperationException If a reflection error occurs.
 */
@SuppressWarnings("unchecked")
public MessageHandlerChainSet parse(World world) throws IOException, SAXException, ReflectiveOperationException {
    XmlNode messages = parser.parse(is);
    if (!messages.getName().equals("messages")) {
        throw new IOException("Root node name is not 'messages'.");
    }
    MessageHandlerChainSet chainSet = new MessageHandlerChainSet();
    for (XmlNode message : messages) {
        if (!message.getName().equals("message")) {
            throw new IOException("Only expected nodes named 'message' beneath the root node.");
        }
        XmlNode typeNode = message.getChild("type");
        if (typeNode == null) {
            throw new IOException("No node named 'type' beneath current message node.");
        }
        XmlNode chainNode = message.getChild("chain");
        if (chainNode == null) {
            throw new IOException("No node named 'chain' beneath current message node.");
        }
        String messageClassName = typeNode.getValue();
        if (messageClassName == null) {
            throw new IOException("Type node must have a value.");
        }
        Class<? extends Message> messageClass = (Class<? extends Message>) Class.forName(messageClassName);
        for (XmlNode handlerNode : chainNode) {
            if (!handlerNode.getName().equals("handler")) {
                throw new IOException("Only expected nodes named 'handler' beneath the root node.");
            }
            String handlerClassName = handlerNode.getValue();
            if (handlerClassName == null) {
                throw new IOException("Handler node must have a value.");
            }
            Class<? extends MessageHandler<? extends Message>> handlerClass = (Class<? extends MessageHandler<? extends Message>>) Class.forName(handlerClassName);
            MessageHandler<? extends Message> handler = handlerClass.getConstructor(World.class).newInstance(world);
            chainSet.putHandler(messageClass, handler);
        }
    }
    return chainSet;
}
Also used : XmlNode(org.apollo.util.xml.XmlNode) Message(org.apollo.net.message.Message) MessageHandler(org.apollo.game.message.handler.MessageHandler) MessageHandlerChainSet(org.apollo.game.message.handler.MessageHandlerChainSet) IOException(java.io.IOException) World(org.apollo.game.model.World)

Example 7 with World

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

the class WalkingQueue method pulse.

/**
 * Pulses this WalkingQueue.
 */
public void pulse() {
    Position position = mob.getPosition();
    int height = position.getHeight();
    Direction firstDirection = Direction.NONE;
    Direction secondDirection = Direction.NONE;
    World world = mob.getWorld();
    CollisionManager collisionManager = world.getCollisionManager();
    Position next = points.poll();
    if (next != null) {
        firstDirection = Direction.between(position, next);
        if (!collisionManager.traversable(position, EntityType.NPC, firstDirection)) {
            clear();
            firstDirection = Direction.NONE;
        } else {
            previousPoints.add(next);
            position = new Position(next.getX(), next.getY(), height);
            mob.setLastDirection(firstDirection);
            if (running) {
                next = points.poll();
                if (next != null) {
                    secondDirection = Direction.between(position, next);
                    if (!collisionManager.traversable(position, EntityType.NPC, secondDirection)) {
                        clear();
                        secondDirection = Direction.NONE;
                    } else {
                        previousPoints.add(next);
                        position = new Position(next.getX(), next.getY(), height);
                        mob.setLastDirection(secondDirection);
                    }
                }
            }
        }
    }
    mob.setDirections(firstDirection, secondDirection);
    mob.setPosition(position);
}
Also used : CollisionManager(org.apollo.game.model.area.collision.CollisionManager) Position(org.apollo.game.model.Position) World(org.apollo.game.model.World) Direction(org.apollo.game.model.Direction)

Example 8 with World

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

the class ObjectActionVerificationHandlerTests method terminateIfOutOfRange.

@Test
public void terminateIfOutOfRange() 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));
    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);
    ObjectActionMessage objectActionMessage = new ObjectActionMessage(1, 4151, objectPosition);
    ObjectActionVerificationHandler objectActionVerificationHandler = new ObjectActionVerificationHandler(world);
    objectActionVerificationHandler.handle(player, objectActionMessage);
    assertTrue("ObjectVerificationHandler: message not terminated when out of range!", objectActionMessage.terminated());
}
Also used : Entity(org.apollo.game.model.entity.Entity) Player(org.apollo.game.model.entity.Player) ObjectActionMessage(org.apollo.game.message.impl.ObjectActionMessage) 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) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 9 with World

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

Example 10 with World

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

the class LoginService method init.

/**
 * Initialises the login service.
 *
 * @throws SAXException If there is an error parsing the XML file.
 * @throws IOException If there is an error accessing the file.
 * @throws ReflectiveOperationException If the {@link PlayerSerializer} implementation could not be created.
 */
private void init() throws SAXException, IOException, ReflectiveOperationException {
    XmlParser parser = new XmlParser();
    XmlNode rootNode;
    try (InputStream is = new FileInputStream("data/login.xml")) {
        rootNode = parser.parse(is);
    }
    if (!rootNode.getName().equals("login")) {
        throw new IOException("Unexpected root node name, expected 'login'.");
    }
    XmlNode serializer = rootNode.getChild("serializer");
    if (serializer == null || !serializer.hasValue()) {
        throw new IOException("No serializer child node or value.");
    }
    Class<?> clazz = Class.forName(serializer.getValue());
    this.serializer = (PlayerSerializer) clazz.getConstructor(World.class).newInstance(world);
}
Also used : XmlParser(org.apollo.util.xml.XmlParser) XmlNode(org.apollo.util.xml.XmlNode) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) World(org.apollo.game.model.World) FileInputStream(java.io.FileInputStream)

Aggregations

World (org.apollo.game.model.World)10 Position (org.apollo.game.model.Position)7 Region (org.apollo.game.model.area.Region)6 RegionRepository (org.apollo.game.model.area.RegionRepository)6 HashSet (java.util.HashSet)5 Entity (org.apollo.game.model.entity.Entity)5 Player (org.apollo.game.model.entity.Player)5 StaticGameObject (org.apollo.game.model.entity.obj.StaticGameObject)5 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 ItemOnObjectMessage (org.apollo.game.message.impl.ItemOnObjectMessage)3 Inventory (org.apollo.game.model.inv.Inventory)3 IOException (java.io.IOException)2 ObjectActionMessage (org.apollo.game.message.impl.ObjectActionMessage)2 XmlNode (org.apollo.util.xml.XmlNode)2 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 IndexedFileSystem (org.apollo.cache.IndexedFileSystem)1