Search in sources :

Example 1 with LionEngineException

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

the class MapTileGame method append.

@Override
public void append(Collection<MapTile> maps, int offsetX, int offsetY, int randX, int randY) {
    int newWidth = widthInTile;
    int newHeight = heightInTile;
    final int[] randsX = new int[maps.size()];
    final int[] randsY = new int[randsX.length];
    int i = 0;
    int tw = 0;
    int th = 0;
    for (final MapTile map : maps) {
        randsX[i] = UtilRandom.getRandomInteger(randX);
        randsY[i] = UtilRandom.getRandomInteger(randY);
        newWidth += map.getInTileWidth() + randsX[i];
        newHeight += map.getInTileHeight() + randsY[i];
        if (tw == 0) {
            tw = map.getTileWidth();
            th = map.getTileHeight();
        } else if (tw != map.getTileWidth() || th != map.getTileHeight()) {
            throw new LionEngineException(ERROR_APPEND_MAP_TILE_SIZE + map.getTileWidth() + Constant.SPACE + map.getTileHeight());
        }
        i++;
    }
    if (!isCreated()) {
        create(tw, th, 1, 1);
    }
    resize(newWidth, newHeight);
    int ox = 0;
    int oy = 0;
    i = 0;
    for (final MapTile map : maps) {
        appendMap(map, ox, oy);
        ox += map.getInTileWidth() * offsetX + randsX[i];
        oy += map.getInTileHeight() * offsetY + randsY[i];
        i++;
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException)

Example 2 with LionEngineException

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

the class MapTileGame method loadSheets.

@Override
public void loadSheets(Collection<SpriteTiled> sheets) {
    int sheetId = 0;
    for (final SpriteTiled sheet : sheets) {
        this.sheets.put(Integer.valueOf(sheetId), sheet);
        if ((tileWidth != 0 || tileHeight != 0) && tileWidth != sheet.getTileWidth() && tileHeight != sheet.getTileHeight()) {
            throw new LionEngineException(ERROR_TILE_SIZE);
        }
        tileWidth = sheet.getTileWidth();
        tileHeight = sheet.getTileHeight();
        sheetId++;
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) SpriteTiled(com.b3dgs.lionengine.graphic.SpriteTiled)

Example 3 with LionEngineException

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

the class CollisionFunctionConfig method imports.

/**
 * Create the collision function from node.
 *
 * @param parent The parent reference (must not be <code>null</code>).
 * @return The collision function data.
 * @throws LionEngineException If error when reading node.
 */
public static CollisionFunction imports(Xml parent) {
    Check.notNull(parent);
    final Xml node = parent.getChild(FUNCTION);
    final String name = node.readString(TYPE);
    try {
        final CollisionFunctionType type = CollisionFunctionType.valueOf(name);
        switch(type) {
            case LINEAR:
                return new CollisionFunctionLinear(node.readDouble(A), node.readDouble(B));
            default:
                throw new LionEngineException(ERROR_TYPE + name);
        }
    } catch (final IllegalArgumentException exception) {
        throw new LionEngineException(exception, ERROR_TYPE + name);
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) Xml(com.b3dgs.lionengine.io.Xml)

Example 4 with LionEngineException

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

the class Configurer method getImplementation.

/**
 * Get the class implementation from its name by using a custom constructor.
 *
 * @param <T> The instance type.
 * @param loader The class loader to use.
 * @param type The class type.
 * @param paramsType The parameters type.
 * @param paramsValue The parameters value.
 * @param className The class name.
 * @return The typed class instance.
 * @throws LionEngineException If invalid class.
 */
public static final <T> T getImplementation(ClassLoader loader, Class<T> type, Class<?>[] paramsType, Collection<?> paramsValue, String className) {
    try {
        if (!CLASS_CACHE.containsKey(className)) {
            final Class<?> clazz = loader.loadClass(className);
            CLASS_CACHE.put(className, clazz);
        }
        final Class<?> clazz = CLASS_CACHE.get(className);
        final Constructor<?> constructor = UtilReflection.getCompatibleConstructor(clazz, paramsType);
        if (!constructor.isAccessible()) {
            UtilReflection.setAccessible(constructor, true);
        }
        return type.cast(constructor.newInstance(paramsValue.toArray()));
    } catch (final InstantiationException | IllegalArgumentException | InvocationTargetException exception) {
        throw new LionEngineException(exception, ERROR_CLASS_INSTANCE + className);
    } catch (final NoSuchMethodException exception) {
        throw new LionEngineException(exception, ERROR_CLASS_CONSTRUCTOR + className);
    } catch (final IllegalAccessException exception) {
        throw new LionEngineException(exception, ERROR_CLASS_ACCESSIBILITY + className);
    } catch (final ClassNotFoundException exception) {
        throw new LionEngineException(exception, ERROR_CLASS_PRESENCE + className);
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with LionEngineException

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

the class DocumentFactoryTest method testMissingFeature.

/**
 * Test missing feature.
 *
 * @throws Exception If error.
 */
@Test
public void testMissingFeature() throws Exception {
    final Object old = UtilReflection.getField(DocumentFactory.class, "documentBuilder");
    final Field field = DocumentFactory.class.getDeclaredField("documentBuilder");
    UtilReflection.setAccessible(field, true);
    field.set(DocumentFactory.class, null);
    final String oldFactory = System.getProperty(DocumentBuilderFactory.class.getName());
    System.setProperty(DocumentBuilderFactory.class.getName(), Factory.class.getName());
    try {
        Verbose.info("*********************************** EXPECTED VERBOSE ***********************************");
        try {
            Assert.assertNull(DocumentFactory.createDocument());
            Assert.fail();
        } catch (final LionEngineException exception) {
            Assert.assertTrue(exception.getCause() instanceof ParserConfigurationException);
        }
        Verbose.info("****************************************************************************************");
    } finally {
        if (oldFactory != null) {
            System.setProperty(DocumentBuilderFactory.class.getName(), oldFactory);
        } else {
            System.setProperty(DocumentBuilderFactory.class.getName(), "");
        }
        field.set(DocumentFactory.class, old);
        UtilReflection.setAccessible(field, false);
    }
}
Also used : Field(java.lang.reflect.Field) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) LionEngineException(com.b3dgs.lionengine.LionEngineException) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Test(org.junit.Test)

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