Search in sources :

Example 11 with XmlReader

use of com.b3dgs.lionengine.XmlReader in project lionengine by b3dgs.

the class CollidableFramedConfig method imports.

/**
 * Create the collision data from node.
 *
 * @param root The node reference (must not be <code>null</code>).
 * @return The collisions data.
 * @throws LionEngineException If unable to read node.
 */
public static CollidableFramedConfig imports(XmlReader root) {
    Check.notNull(root);
    final Map<Integer, Collection<Collision>> collisions = new HashMap<>(0);
    if (root.hasNode(AnimationConfig.NODE_ANIMATIONS)) {
        final Collection<XmlReader> children = root.getChild(AnimationConfig.NODE_ANIMATIONS).getChildren(AnimationConfig.NODE_ANIMATION);
        for (final XmlReader node : children) {
            final int start = node.getInteger(AnimationConfig.ANIMATION_START);
            for (final XmlReader framed : node.getChildren(NODE_COLLISION_FRAMED)) {
                importFrame(node, framed, start, collisions);
            }
        }
        children.clear();
    }
    return new CollidableFramedConfig(collisions);
}
Also used : HashMap(java.util.HashMap) Collection(java.util.Collection) XmlReader(com.b3dgs.lionengine.XmlReader)

Example 12 with XmlReader

use of com.b3dgs.lionengine.XmlReader in project lionengine by b3dgs.

the class CollidableFramedConfig method importFrame.

/**
 * Import frame collision data.
 *
 * @param node The node root reference.
 * @param framed The framed node reference.
 * @param start The collision start number.
 * @param collisions The imported collisions.
 */
private static void importFrame(XmlReader node, XmlReader framed, int start, Map<Integer, Collection<Collision>> collisions) {
    final String name = getFrameName(node, framed);
    if (framed.hasAttribute(ATT_NUMBER)) {
        final int number = start + framed.getInteger(ATT_NUMBER);
        final Collision collision = createCollision(name, framed, number - start);
        final Integer key = Integer.valueOf(number - 1);
        collisions.computeIfAbsent(key, k -> new ArrayList<>()).add(collision);
    } else {
        final int end = node.getInteger(AnimationConfig.ANIMATION_END);
        for (int number = start; number <= end; number++) {
            final Collision collision = createCollision(name, framed, number - start + 1);
            final Integer key = Integer.valueOf(number);
            collisions.computeIfAbsent(key, k -> new ArrayList<>()).add(collision);
        }
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) Collection(java.util.Collection) Check(com.b3dgs.lionengine.Check) HashMap(java.util.HashMap) Configurer(com.b3dgs.lionengine.game.Configurer) ArrayList(java.util.ArrayList) Constant(com.b3dgs.lionengine.Constant) Xml(com.b3dgs.lionengine.Xml) XmlReader(com.b3dgs.lionengine.XmlReader) Collision(com.b3dgs.lionengine.game.feature.collidable.Collision) Map(java.util.Map) Entry(java.util.Map.Entry) Collections(java.util.Collections) AnimationConfig(com.b3dgs.lionengine.game.AnimationConfig) Collision(com.b3dgs.lionengine.game.feature.collidable.Collision) ArrayList(java.util.ArrayList)

Example 13 with XmlReader

use of com.b3dgs.lionengine.XmlReader in project lionengine by b3dgs.

the class FeaturableConfig method getFeatures.

/**
 * Get all available features.
 * Default constructor of each feature must be available or with {@link Setup} as single parameter.
 *
 * @param loader The class loader reference.
 * @param services The services reference.
 * @param setup The setup reference.
 * @param filter The type filter (can be <code>null</code> if none).
 * @return The available features.
 * @throws LionEngineException If invalid class.
 */
public static List<Feature> getFeatures(ClassLoader loader, Services services, Setup setup, Class<?> filter) {
    final Collection<XmlReader> children;
    final XmlReader root = setup.getRoot();
    if (root.hasNode(NODE_FEATURES)) {
        children = setup.getRoot().getChild(FeaturableConfig.NODE_FEATURES).getChildren(FeaturableConfig.NODE_FEATURE);
    } else {
        children = Collections.emptyList();
    }
    final List<Feature> features = new ArrayList<>(children.size());
    for (final XmlReader featureNode : children) {
        final String className = featureNode.getText();
        try {
            final Class<? extends Feature> clazz = getClass(loader, className);
            if (filter == null || filter.isAssignableFrom(clazz)) {
                final Feature feature = UtilReflection.createReduce(clazz, services, setup);
                features.add(feature);
            }
        } catch (final NoSuchMethodException | LionEngineException exception) {
            throw new LionEngineException(exception, setup.getMedia());
        }
    }
    children.clear();
    return features;
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) ArrayList(java.util.ArrayList) XmlReader(com.b3dgs.lionengine.XmlReader) Feature(com.b3dgs.lionengine.game.Feature)

Example 14 with XmlReader

use of com.b3dgs.lionengine.XmlReader in project lionengine by b3dgs.

the class ActionConfig method imports.

/**
 * Import the action data from node.
 *
 * @param root The root node reference (must not be <code>null</code>).
 * @return The action data.
 * @throws LionEngineException If unable to read node.
 */
public static ActionConfig imports(XmlReader root) {
    Check.notNull(root);
    final XmlReader nodeAction = root.getChild(NODE_ACTION);
    final String name = nodeAction.getString(ATT_NAME);
    final String description = nodeAction.getString(ATT_DESCRIPTION);
    final int x = nodeAction.getInteger(ATT_X);
    final int y = nodeAction.getInteger(ATT_Y);
    final int width = nodeAction.getInteger(ATT_WIDTH);
    final int height = nodeAction.getInteger(ATT_HEIGHT);
    return new ActionConfig(name, description, x, y, width, height);
}
Also used : XmlReader(com.b3dgs.lionengine.XmlReader)

Example 15 with XmlReader

use of com.b3dgs.lionengine.XmlReader in project lionengine by b3dgs.

the class ForceConfig method imports.

/**
 * Create the force data from node.
 *
 * @param root The root reference (must not be <code>null</code>).
 * @return The force data.
 * @throws LionEngineException If unable to read node.
 */
public static Force imports(XmlReader root) {
    Check.notNull(root);
    final XmlReader node = root.getChild(NODE_FORCE);
    final Force force = new Force(node.getDouble(ATT_VX), node.getDouble(ATT_VY));
    force.setVelocity(node.getDouble(0.0, ATT_VELOCITY));
    force.setSensibility(node.getDouble(0.0, ATT_SENSIBILITY));
    return force;
}
Also used : XmlReader(com.b3dgs.lionengine.XmlReader)

Aggregations

XmlReader (com.b3dgs.lionengine.XmlReader)45 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)12 LionEngineException (com.b3dgs.lionengine.LionEngineException)6 HashSet (java.util.HashSet)6 Collection (java.util.Collection)4 Xml (com.b3dgs.lionengine.Xml)2 Configurer (com.b3dgs.lionengine.game.Configurer)2 DeviceAxis (com.b3dgs.lionengine.io.DeviceAxis)2 DeviceMapper (com.b3dgs.lionengine.io.DeviceMapper)2 Set (java.util.Set)2 Animation (com.b3dgs.lionengine.Animation)1 Check (com.b3dgs.lionengine.Check)1 Constant (com.b3dgs.lionengine.Constant)1 AnimationConfig (com.b3dgs.lionengine.game.AnimationConfig)1 Feature (com.b3dgs.lionengine.game.Feature)1 Orientation (com.b3dgs.lionengine.game.Orientation)1 SizeConfig (com.b3dgs.lionengine.game.SizeConfig)1 Collision (com.b3dgs.lionengine.game.feature.collidable.Collision)1 ColorRgba (com.b3dgs.lionengine.graphic.ColorRgba)1