Search in sources :

Example 51 with LionEngineException

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

the class AttackerModelTest method testEnumFail.

/**
 * Test with enum fail.
 *
 * @throws NoSuchFieldException If error.
 * @throws IllegalArgumentException If error.
 * @throws IllegalAccessException If error.
 */
@Test
public void testEnumFail() throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    final AttackerModel attacker = new AttackerModel();
    final Field field = attacker.getClass().getDeclaredField("state");
    UtilReflection.setAccessible(field, true);
    field.set(attacker, AttackState.values()[3]);
    try {
        attacker.update(1.0);
        Assert.fail();
    } catch (final LionEngineException exception) {
        Assert.assertEquals("Unknown enum: FAIL", exception.getMessage());
    }
}
Also used : Field(java.lang.reflect.Field) LionEngineException(com.b3dgs.lionengine.LionEngineException) Test(org.junit.Test)

Example 52 with LionEngineException

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

the class CollisionCategoryConfig method imports.

/**
 * Create the category data from node.
 *
 * @param root The root reference (must not be <code>null</code>).
 * @param map The map reference (must not be <code>null</code>).
 * @return The category node instance.
 * @throws LionEngineException If unable to read node.
 */
public static CollisionCategory imports(Xml root, MapTileCollision map) {
    Check.notNull(root);
    Check.notNull(map);
    final Collection<Xml> children = root.getChildren(TileGroupsConfig.NODE_GROUP);
    final Collection<CollisionGroup> groups = new ArrayList<>(children.size());
    for (final Xml groupNode : children) {
        final String groupName = groupNode.getText();
        final CollisionGroup group = map.getCollisionGroup(groupName);
        groups.add(group);
    }
    final String axisName = root.readString(ATT_AXIS);
    final Axis axis;
    try {
        axis = Axis.valueOf(axisName);
    } catch (final IllegalArgumentException exception) {
        throw new LionEngineException(exception, ERROR_AXIS + axisName);
    }
    final int x = root.readInteger(ATT_X);
    final int y = root.readInteger(ATT_Y);
    final String name = root.readString(ATT_NAME);
    return new CollisionCategory(name, axis, x, y, groups);
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) Xml(com.b3dgs.lionengine.io.Xml) ArrayList(java.util.ArrayList)

Example 53 with LionEngineException

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

the class Factory method createSetup.

/**
 * Create a setup from its media.
 *
 * @param media The media reference.
 * @return The setup instance.
 */
@SuppressWarnings("unchecked")
private Setup createSetup(Media media) {
    final Configurer configurer = new Configurer(media);
    try {
        final FeaturableConfig config = FeaturableConfig.imports(configurer);
        final String setup = config.getSetupName();
        final Class<? extends Setup> setupClass;
        if (setup.isEmpty()) {
            final Class<?> clazz = classLoader.loadClass(config.getClassName());
            final Constructor<?> constructor = UtilReflection.getCompatibleConstructorParent(clazz, new Class<?>[] { Services.class, Setup.class });
            setupClass = (Class<? extends Setup>) constructor.getParameterTypes()[SETUP_INDEX];
        } else {
            setupClass = (Class<? extends Setup>) classLoader.loadClass(config.getSetupName());
        }
        return UtilReflection.create(setupClass, new Class<?>[] { Media.class }, media);
    } catch (final ClassNotFoundException exception) {
        throw new LionEngineException(exception, ERROR_SETUP_CLASS);
    } catch (final NoSuchMethodException exception) {
        throw new LionEngineException(exception, ERROR_CONSTRUCTOR_MISSING + media.getPath());
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) Configurer(com.b3dgs.lionengine.game.Configurer)

Example 54 with LionEngineException

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

the class FeaturableModel method getClass.

/**
 * Get the class reference from its name using cache.
 *
 * @param <T> The class type.
 * @param className The class name.
 * @return The typed class instance.
 * @throws LionEngineException If invalid class.
 */
@SuppressWarnings("unchecked")
private static <T> Class<T> getClass(String className) {
    if (CLASS_CACHE.containsKey(className)) {
        return (Class<T>) CLASS_CACHE.get(className);
    }
    try {
        final Class<?> clazz = LOADER.loadClass(className);
        CLASS_CACHE.put(className, clazz);
        return (Class<T>) clazz;
    } catch (final ClassNotFoundException exception) {
        throw new LionEngineException(exception, ERROR_CLASS_PRESENCE + className);
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException)

Example 55 with LionEngineException

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

the class FeaturableModel method getFeatures.

/**
 * Get all available features.
 * Default constructor of each feature must be available or with {@link Setup} as single parameter.
 *
 * @param services The services reference.
 * @param setup The setup reference.
 * @return The available features.
 * @throws LionEngineException If invalid class.
 */
private static List<Feature> getFeatures(Services services, Setup setup) {
    final Collection<Xml> children = setup.getRoot().getChildren(FeaturableConfig.NODE_FEATURE);
    final List<Feature> features = new ArrayList<>(children.size());
    for (final Xml featureNode : children) {
        final String className = featureNode.getText();
        final Feature feature;
        try {
            final Class<? extends Feature> clazz = getClass(className);
            feature = UtilReflection.createReduce(clazz, services, setup);
        } catch (final NoSuchMethodException exception) {
            throw new LionEngineException(exception);
        }
        features.add(feature);
    }
    return features;
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) Xml(com.b3dgs.lionengine.io.Xml) ArrayList(java.util.ArrayList)

Aggregations

LionEngineException (com.b3dgs.lionengine.LionEngineException)75 IOException (java.io.IOException)13 File (java.io.File)8 OutputStream (java.io.OutputStream)7 ArrayList (java.util.ArrayList)7 XmlReader (com.b3dgs.lionengine.XmlReader)6 Field (java.lang.reflect.Field)6 Test (org.junit.Test)6 Media (com.b3dgs.lionengine.Media)5 Configurer (com.b3dgs.lionengine.game.Configurer)4 UtilFile (com.b3dgs.lionengine.UtilFile)3 Graphic (com.b3dgs.lionengine.graphic.Graphic)3 UtilFile (com.b3dgs.lionengine.util.UtilFile)3 DisplayMode (java.awt.DisplayMode)3 BufferedImage (java.awt.image.BufferedImage)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 AfterAll (org.junit.jupiter.api.AfterAll)3 Animation (com.b3dgs.lionengine.Animation)2 AnimationConfig (com.b3dgs.lionengine.game.AnimationConfig)2 Feature (com.b3dgs.lionengine.game.Feature)2