Search in sources :

Example 36 with LionEngineException

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

the class DeviceControllerConfig method create.

/**
 * Create device controller from configuration.
 *
 * @param services The services reference (must not be <code>null</code>).
 * @param media The media configuration (must not be <code>null</code>).
 * @return The created controller.
 * @throws LionEngineException If unable to read node.
 */
public static DeviceController create(Services services, Media media) {
    final Context context = services.get(Context.class);
    final DeviceController controller = new DeviceControllerModel();
    final Collection<DeviceControllerConfig> configs = DeviceControllerConfig.imports(services, media);
    for (final DeviceControllerConfig config : configs) {
        final InputDevice device = context.getInputDevice(config.getDevice());
        if (device instanceof DevicePush) {
            final DevicePush push = (DevicePush) device;
            config.getHorizontal().forEach(h -> controller.addHorizontal(push, new DeviceActionModel(h, push)));
            config.getVertical().forEach(v -> controller.addVertical(push, new DeviceActionModel(v, push)));
            config.getFire().entrySet().forEach(e -> e.getValue().forEach(c -> controller.addFire(device, e.getKey(), new DeviceActionModel(c, push))));
        }
        if (device instanceof DevicePointer) {
            final DevicePointer pointer = (DevicePointer) device;
            controller.addHorizontal(pointer, () -> pointer.getMoveX());
            controller.addVertical(pointer, () -> -pointer.getMoveY());
        }
        if (config.isDisabled()) {
            controller.setDisabled(device.getName(), true, true);
        }
    }
    return controller;
}
Also used : Context(com.b3dgs.lionengine.Context) DevicePush(com.b3dgs.lionengine.io.DevicePush) LionEngineException(com.b3dgs.lionengine.LionEngineException) DevicePointer(com.b3dgs.lionengine.io.DevicePointer) DevicePush(com.b3dgs.lionengine.io.DevicePush) Collection(java.util.Collection) Check(com.b3dgs.lionengine.Check) Set(java.util.Set) HashMap(java.util.HashMap) DeviceControllerModel(com.b3dgs.lionengine.io.DeviceControllerModel) DeviceController(com.b3dgs.lionengine.io.DeviceController) Configurer(com.b3dgs.lionengine.game.Configurer) DeviceMapper(com.b3dgs.lionengine.io.DeviceMapper) ArrayList(java.util.ArrayList) InputDevice(com.b3dgs.lionengine.InputDevice) HashSet(java.util.HashSet) Constant(com.b3dgs.lionengine.Constant) Context(com.b3dgs.lionengine.Context) DeviceAxis(com.b3dgs.lionengine.io.DeviceAxis) List(java.util.List) XmlReader(com.b3dgs.lionengine.XmlReader) Services(com.b3dgs.lionengine.game.feature.Services) Map(java.util.Map) Media(com.b3dgs.lionengine.Media) DeviceActionModel(com.b3dgs.lionengine.io.DeviceActionModel) DeviceControllerModel(com.b3dgs.lionengine.io.DeviceControllerModel) InputDevice(com.b3dgs.lionengine.InputDevice) DeviceController(com.b3dgs.lionengine.io.DeviceController) DevicePointer(com.b3dgs.lionengine.io.DevicePointer) DeviceActionModel(com.b3dgs.lionengine.io.DeviceActionModel)

Example 37 with LionEngineException

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

the class DeviceControllerConfig method imports.

/**
 * Import the data from configurer.
 *
 * @param services The services reference (must not be <code>null</code>).
 * @param media The media configuration (must not be <code>null</code>).
 * @return The loaded input.
 * @throws LionEngineException If unable to read node.
 */
@SuppressWarnings("unchecked")
public static Collection<DeviceControllerConfig> imports(Services services, Media media) {
    Check.notNull(services);
    Check.notNull(media);
    final Collection<DeviceControllerConfig> configs = new ArrayList<>();
    final ClassLoader loader = services.getOptional(ClassLoader.class).orElse(DeviceControllerConfig.class.getClassLoader());
    final Configurer configurer = new Configurer(media);
    try {
        final Class<Enum<? extends DeviceMapper>> mapping;
        mapping = (Class<Enum<? extends DeviceMapper>>) loader.loadClass(configurer.getString(ATT_MAPPING));
        for (final XmlReader deviceNode : configurer.getChildren(NODE_DEVICE)) {
            final Class<DevicePush> device = (Class<DevicePush>) loader.loadClass(deviceNode.getString(ATT_CLASS));
            final boolean disabled = deviceNode.getBoolean(false, ATT_DISABLED);
            final List<DeviceAxis> horizontal = readAxis(deviceNode, NODE_HORIZONTAL);
            final List<DeviceAxis> vertical = readAxis(deviceNode, NODE_VERTICAL);
            final Map<Integer, Set<Integer>> fire = readFire(mapping, deviceNode);
            configs.add(new DeviceControllerConfig(device, disabled, horizontal, vertical, fire));
        }
    } catch (final ReflectiveOperationException exception) {
        throw new LionEngineException(exception);
    }
    return configs;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) DeviceMapper(com.b3dgs.lionengine.io.DeviceMapper) ArrayList(java.util.ArrayList) XmlReader(com.b3dgs.lionengine.XmlReader) DeviceAxis(com.b3dgs.lionengine.io.DeviceAxis) DevicePush(com.b3dgs.lionengine.io.DevicePush) LionEngineException(com.b3dgs.lionengine.LionEngineException) Configurer(com.b3dgs.lionengine.game.Configurer)

Example 38 with LionEngineException

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

the class PathfindableConfig method importAllowedMovements.

/**
 * Import the allowed movements.
 *
 * @param node The root node (must not be <code>null</code>).
 * @return The allowed movements.
 * @throws LionEngineException If malformed movement name.
 */
private static Collection<MovementTile> importAllowedMovements(XmlReader node) {
    if (!node.hasNode(NODE_MOVEMENT)) {
        return Collections.emptySet();
    }
    final Collection<MovementTile> movements = EnumSet.noneOf(MovementTile.class);
    final Collection<XmlReader> children = node.getChildren(NODE_MOVEMENT);
    for (final XmlReader movementNode : children) {
        try {
            movements.add(MovementTile.valueOf(movementNode.getText()));
        } catch (final IllegalArgumentException exception) {
            throw new LionEngineException(exception);
        }
    }
    children.clear();
    return movements;
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) XmlReader(com.b3dgs.lionengine.XmlReader)

Example 39 with LionEngineException

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

the class CollisionRangeConfig method imports.

/**
 * Create the collision range data from a node.
 *
 * @param node The node reference (must not be <code>null</code>).
 * @return The collision range data.
 * @throws LionEngineException If error when reading node.
 */
public static CollisionRange imports(XmlReader node) {
    Check.notNull(node);
    final Axis axis = node.getEnum(Axis.class, ATT_AXIS);
    try {
        final int minX = node.getInteger(ATT_MIN_X);
        final int maxX = node.getInteger(ATT_MAX_X);
        final int minY = node.getInteger(ATT_MIN_Y);
        final int maxY = node.getInteger(ATT_MAX_Y);
        return new CollisionRange(axis, minX, maxX, minY, maxY);
    } catch (final IllegalArgumentException exception) {
        throw new LionEngineException(exception, ERROR_TYPE + axis);
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException)

Example 40 with LionEngineException

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

the class MediaDefault method getMedias.

@Override
public Collection<Media> getMedias() {
    final File file = getFile();
    final File[] files = file.listFiles();
    if (files != null) {
        final Collection<Media> medias = new ArrayList<>(files.length);
        final String prefix = getPrefix();
        final int prefixLength = prefix.length();
        for (final File current : files) {
            final Media media = create(prefix, prefixLength, current);
            medias.add(media);
        }
        return medias;
    }
    throw new LionEngineException(this, ERROR_PATH_DIR);
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) Media(com.b3dgs.lionengine.Media) ArrayList(java.util.ArrayList) UtilFile(com.b3dgs.lionengine.util.UtilFile) File(java.io.File)

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