Search in sources :

Example 66 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(XmlReader root, MapTileCollision map) {
    Check.notNull(root);
    Check.notNull(map);
    final Collection<XmlReader> children = root.getChildren(TileGroupsConfig.NODE_GROUP);
    final Collection<CollisionGroup> groups = new ArrayList<>(children.size());
    for (final XmlReader groupNode : children) {
        final String groupName = groupNode.getText();
        map.getCollisionGroup(groupName).ifPresent(groups::add);
    }
    children.clear();
    final String axisName = root.getString(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.getInteger(ATT_X);
    final int y = root.getInteger(ATT_Y);
    final boolean glue = root.getBoolean(true, ATT_GLUE);
    final String name = root.getString(ATT_NAME);
    return new CollisionCategory(name, axis, x, y, glue, groups);
}
Also used : ArrayList(java.util.ArrayList) XmlReader(com.b3dgs.lionengine.XmlReader) LionEngineException(com.b3dgs.lionengine.LionEngineException)

Example 67 with LionEngineException

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

the class MapTileAppenderModel method append.

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

Example 68 with LionEngineException

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

the class FactoryGraphicAwt method generateTileset.

@Override
public void generateTileset(ImageBuffer[] images, Media media) {
    Check.notNull(images);
    Check.notNull(media);
    if (images.length == 0) {
        throw new LionEngineException("No images found !");
    }
    final int width = images[0].getWidth();
    final int height = images[0].getHeight();
    final Transparency transparency = images[0].getTransparency();
    final int multDistance = (int) Math.ceil(width * images.length / (double) height) / 4;
    final int[] mult = UtilMath.getClosestSquareMult(images.length, multDistance);
    final ImageBuffer tile = new ImageBufferAwt(ToolsAwt.createImage(width * mult[1], height * mult[0], ToolsAwt.getTransparency(transparency)));
    int x = 0;
    int y = 0;
    int line = 0;
    final Graphic g = tile.createGraphic();
    for (final ImageBuffer b : images) {
        g.drawImage(b, x, y);
        x += b.getWidth();
        line++;
        if (line == mult[1]) {
            x = 0;
            y += b.getHeight();
            line = 0;
        }
    }
    g.dispose();
    saveImage(tile, media);
}
Also used : Transparency(com.b3dgs.lionengine.graphic.Transparency) ImageBuffer(com.b3dgs.lionengine.graphic.ImageBuffer) LionEngineException(com.b3dgs.lionengine.LionEngineException) Graphic(com.b3dgs.lionengine.graphic.Graphic) FactoryGraphic(com.b3dgs.lionengine.graphic.FactoryGraphic)

Example 69 with LionEngineException

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

the class UtilSequence method create.

/**
 * Create a sequence from its class.
 *
 * @param nextSequence The next sequence class (must not be <code>null</code>).
 * @param context The context reference (must not be <code>null</code>).
 * @param arguments The arguments list (must not be <code>null</code>).
 * @return The sequence instance.
 * @throws LionEngineException If not able to create the sequence for any reason.
 */
public static Sequencable create(Class<? extends Sequencable> nextSequence, Context context, Object... arguments) {
    Check.notNull(nextSequence);
    Check.notNull(context);
    Check.notNull(arguments);
    try {
        final Class<?>[] params = getParamTypes(context, arguments);
        return UtilReflection.create(nextSequence, params, getParams(context, arguments));
    } catch (final NoSuchMethodException exception) {
        throw new LionEngineException(exception);
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException)

Example 70 with LionEngineException

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

the class AudioFactory method loadAudio.

/**
 * Load an audio file and prepare it to be played.
 *
 * @param <A> The audio type.
 * @param media The audio media (must not be <code>null</code>).
 * @param type The expected audio type (must not be <code>null</code>).
 * @return The loaded audio.
 * @throws LionEngineException If invalid arguments or invalid audio.
 */
public static synchronized <A extends Audio> A loadAudio(Media media, Class<A> type) {
    Check.notNull(media);
    Check.notNull(type);
    final String extension = UtilFile.getExtension(media.getPath());
    try {
        return type.cast(Optional.ofNullable(FACTORIES.get(extension)).orElseThrow(() -> new LionEngineException(media, ERROR_FORMAT)).loadAudio(media));
    } catch (final ClassCastException exception) {
        throw new LionEngineException(exception, media, ERROR_FORMAT);
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException)

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