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