Search in sources :

Example 1 with Spritesheet

use of de.gurkenlabs.litiengine.graphics.Spritesheet in project litiengine by gurkenlabs.

the class EditorScreen method loadSpriteSheets.

private void loadSpriteSheets(Map map) {
    List<SpriteSheetInfo> infos = new ArrayList<>();
    int cnt = 0;
    for (ITileset tileSet : map.getTilesets()) {
        if (tileSet.getImage() == null || Spritesheet.find(tileSet.getName()) != null) {
            continue;
        }
        Spritesheet sprite = Spritesheet.find(tileSet.getImage().getSource());
        if (sprite == null) {
            sprite = Spritesheet.load(tileSet);
            if (sprite == null) {
                continue;
            }
        }
        infos.add(new SpriteSheetInfo(sprite));
        cnt++;
    }
    for (IImageLayer imageLayer : map.getImageLayers()) {
        Spritesheet sprite = Spritesheet.find(imageLayer.getImage().getSource());
        if (sprite == null) {
            BufferedImage img = Resources.getImage(imageLayer.getImage().getAbsoluteSourcePath(), true);
            sprite = Spritesheet.load(img, imageLayer.getImage().getSource(), img.getWidth(), img.getHeight());
            if (sprite == null) {
                continue;
            }
        }
        SpriteSheetInfo info = new SpriteSheetInfo(sprite);
        infos.add(info);
        this.getGameFile().getSpriteSheets().removeIf(x -> x.getName().equals(info.getName()));
        this.getGameFile().getSpriteSheets().add(info);
        cnt++;
    }
    this.loadSpriteSheets(infos, false);
    for (SpriteSheetInfo info : infos) {
        if (!this.getGameFile().getSpriteSheets().stream().anyMatch(x -> x.getName().equals(info.getName()))) {
            this.getGameFile().getSpriteSheets().add(info);
        }
    }
    if (cnt > 0) {
        log.log(Level.INFO, "{0} tilesets loaded from {1}", new Object[] { cnt, map.getFileName() });
    }
}
Also used : Color(java.awt.Color) ITileset(de.gurkenlabs.litiengine.environment.tilemap.ITileset) EditorComponent(de.gurkenlabs.utiliti.components.EditorComponent) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) Point(java.awt.Point) ComponentType(de.gurkenlabs.utiliti.components.EditorComponent.ComponentType) MathUtilities(de.gurkenlabs.litiengine.util.MathUtilities) SpriteSheetInfo(de.gurkenlabs.litiengine.SpriteSheetInfo) Map(de.gurkenlabs.litiengine.environment.tilemap.xml.Map) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Blueprint(de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint) MapComponent(de.gurkenlabs.utiliti.components.MapComponent) Graphics2D(java.awt.Graphics2D) ImageCache(de.gurkenlabs.litiengine.graphics.ImageCache) FileUtilities(de.gurkenlabs.litiengine.util.io.FileUtilities) Tileset(de.gurkenlabs.litiengine.environment.tilemap.xml.Tileset) JFileChooser(javax.swing.JFileChooser) Resources(de.gurkenlabs.litiengine.Resources) IImageLayer(de.gurkenlabs.litiengine.environment.tilemap.IImageLayer) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) EmitterData(de.gurkenlabs.litiengine.graphics.particles.xml.EmitterData) Collection(java.util.Collection) IOException(java.io.IOException) Logger(java.util.logging.Logger) JOptionPane(javax.swing.JOptionPane) FileFilter(javax.swing.filechooser.FileFilter) Collectors(java.util.stream.Collectors) MapObjectPanel(de.gurkenlabs.utiliti.swing.panels.MapObjectPanel) File(java.io.File) GameData(de.gurkenlabs.litiengine.GameData) Input(de.gurkenlabs.litiengine.input.Input) ImageFormat(de.gurkenlabs.litiengine.graphics.ImageFormat) List(java.util.List) CustomEmitter(de.gurkenlabs.litiengine.graphics.particles.xml.CustomEmitter) XmlUtilities(de.gurkenlabs.litiengine.util.io.XmlUtilities) Paths(java.nio.file.Paths) Screen(de.gurkenlabs.litiengine.gui.screens.Screen) RenderEngine(de.gurkenlabs.litiengine.graphics.RenderEngine) SpritesheetImportPanel(de.gurkenlabs.utiliti.swing.dialogs.SpritesheetImportPanel) Game(de.gurkenlabs.litiengine.Game) ScreenInfo(de.gurkenlabs.litiengine.annotation.ScreenInfo) XmlImportDialog(de.gurkenlabs.utiliti.swing.XmlImportDialog) SpriteSheetInfo(de.gurkenlabs.litiengine.SpriteSheetInfo) ITileset(de.gurkenlabs.litiengine.environment.tilemap.ITileset) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) ArrayList(java.util.ArrayList) IImageLayer(de.gurkenlabs.litiengine.environment.tilemap.IImageLayer) Point(java.awt.Point) Blueprint(de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint) BufferedImage(java.awt.image.BufferedImage)

Example 2 with Spritesheet

use of de.gurkenlabs.litiengine.graphics.Spritesheet in project litiengine by gurkenlabs.

the class IconTreeListRenderer method getIcon.

private static Icon getIcon(Creature creature) {
    String cacheKey = Game.getEnvironment().getMap().getName() + "-" + creature.getSpritePrefix() + "-" + creature.getMapId() + "-tree";
    BufferedImage propImag;
    if (ImageCache.IMAGES.containsKey(cacheKey)) {
        propImag = ImageCache.IMAGES.get(cacheKey);
    } else {
        Collection<Spritesheet> sprites = Spritesheet.find(s -> s.getName().equals(creature.getSpritePrefix() + CreatureAnimationController.IDLE) || s.getName().equals(creature.getSpritePrefix() + CreatureAnimationController.WALK) || s.getName().equals(creature.getSpritePrefix() + CreatureAnimationController.DEAD) || s.getName().startsWith(creature.getSpritePrefix() + "-"));
        if (sprites.isEmpty()) {
            return null;
        }
        propImag = ImageProcessing.scaleImage(sprites.iterator().next().getSprite(0), 16, 16, true);
        ImageCache.IMAGES.put(cacheKey, propImag);
    }
    return new ImageIcon(propImag);
}
Also used : ImageIcon(javax.swing.ImageIcon) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) BufferedImage(java.awt.image.BufferedImage)

Example 3 with Spritesheet

use of de.gurkenlabs.litiengine.graphics.Spritesheet in project litiengine by gurkenlabs.

the class OrthogonalMapRenderer method getTileImage.

private static Image getTileImage(final IMap map, final ITile tile) {
    if (tile == null) {
        return null;
    }
    final ITileset tileset = MapUtilities.findTileSet(map, tile);
    if (tileset == null || tileset.getFirstGridId() > tile.getGridId()) {
        return null;
    }
    // get the grid id relative to the sprite sheet since we use a 0 based
    // approach to calculate the position
    int index = tile.getGridId() - tileset.getFirstGridId();
    // support for animated tiles
    final ITileAnimation animation = MapUtilities.getAnimation(map, index);
    if (animation != null && !animation.getFrames().isEmpty()) {
        final long playedMs = Game.getTime().sinceGameStart();
        final int totalDuration = animation.getTotalDuration();
        final long animationsPlayed = playedMs / totalDuration;
        final long deltaTicks = playedMs - animationsPlayed * totalDuration;
        int currentPlayTime = 0;
        for (final ITileAnimationFrame frame : animation.getFrames()) {
            currentPlayTime += frame.getDuration();
            if (deltaTicks < currentPlayTime) {
                // found the current animation frame
                index = frame.getTileId();
                break;
            }
        }
    }
    Spritesheet sprite = tileset.getSpritesheet();
    if (sprite == null) {
        return null;
    }
    BufferedImage tileImage = sprite.getSprite(index);
    if (tile.isFlipped()) {
        if (tile.isFlippedDiagonally()) {
            tileImage = ImageProcessing.rotate(tileImage, Math.toRadians(90));
            tileImage = ImageProcessing.verticalFlip(tileImage);
        }
        if (tile.isFlippedHorizontally()) {
            tileImage = ImageProcessing.horizontalFlip(tileImage);
        }
        if (tile.isFlippedVertically()) {
            tileImage = ImageProcessing.verticalFlip(tileImage);
        }
    }
    return tileImage;
}
Also used : Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage)

Example 4 with Spritesheet

use of de.gurkenlabs.litiengine.graphics.Spritesheet in project litiengine by gurkenlabs.

the class CustomEmitter method createNewParticle.

@Override
protected Particle createNewParticle() {
    float x;
    float y;
    float deltaX;
    float deltaY;
    float gravityX;
    float gravityY;
    float width;
    float height;
    float deltaWidth;
    float deltaHeight;
    x = this.getEmitterData().getParticleX().get();
    y = this.getEmitterData().getParticleY().get();
    deltaX = this.getEmitterData().getDeltaX().get();
    deltaY = this.getEmitterData().getDeltaY().get();
    gravityX = this.getEmitterData().getGravityX().get();
    gravityY = this.getEmitterData().getGravityY().get();
    width = this.getEmitterData().getParticleWidth().get();
    height = this.getEmitterData().getParticleHeight().get();
    deltaWidth = this.getEmitterData().getDeltaWidth().get();
    deltaHeight = this.getEmitterData().getDeltaHeight().get();
    Particle particle;
    switch(this.getEmitterData().getParticleType()) {
        case LEFTLINE:
            particle = new LeftLineParticle(width, height, this.getRandomParticleColor(), this.getRandomParticleTTL()).setX(x).setY(y).setDeltaIncX(gravityX).setDeltaIncY(gravityY).setDeltaX(deltaX).setDeltaY(deltaY).setDeltaWidth(deltaWidth).setDeltaHeight(deltaHeight);
            break;
        case DISC:
            particle = new OvalParticle(width, height, this.getRandomParticleColor(), this.getRandomParticleTTL()).setX(x).setY(y).setDeltaIncX(gravityX).setDeltaIncY(gravityY).setDeltaX(deltaX).setDeltaY(deltaY).setDeltaWidth(deltaWidth).setDeltaHeight(deltaHeight);
            break;
        case RECTANGLE:
            particle = new RectangleFillParticle(width, height, this.getRandomParticleColor(), this.getRandomParticleTTL()).setX(x).setY(y).setDeltaIncX(gravityX).setDeltaIncY(gravityY).setDeltaX(deltaX).setDeltaY(deltaY).setDeltaWidth(deltaWidth).setDeltaHeight(deltaHeight);
            break;
        case RECTANGLE_OUTLINE:
            particle = new RectangleOutlineParticle(width, height, this.getRandomParticleColor(), this.getRandomParticleTTL()).setX(x).setY(y).setDeltaIncX(gravityX).setDeltaIncY(gravityY).setDeltaX(deltaX).setDeltaY(deltaY).setDeltaWidth(deltaWidth).setDeltaHeight(deltaHeight);
            break;
        case RIGHTLINE:
            particle = new RightLineParticle(width, height, this.getRandomParticleColor(), this.getRandomParticleTTL()).setX(x).setY(y).setDeltaIncX(gravityX).setDeltaIncY(gravityY).setDeltaX(deltaX).setDeltaY(deltaY).setDeltaWidth(deltaWidth).setDeltaHeight(deltaHeight);
            break;
        case SHIMMER:
            particle = new ShimmerParticle(new Rectangle2D.Float(x, y, this.getWidth(), this.getHeight()), width, height, this.getRandomParticleColor()).setX(x).setY(y).setDeltaIncX(gravityX).setDeltaIncY(gravityY).setDeltaX(deltaX).setDeltaY(deltaY).setDeltaWidth(deltaWidth).setDeltaHeight(deltaHeight);
            break;
        case TEXT:
            particle = new TextParticle(this.getEmitterData().getParticleText(), this.getRandomParticleColor(), this.getRandomParticleTTL()).setX(x).setY(y).setDeltaIncX(gravityX).setDeltaIncY(gravityY).setDeltaX(deltaX).setDeltaY(deltaY).setDeltaWidth(deltaWidth).setDeltaHeight(deltaHeight);
            break;
        case SPRITE:
            Spritesheet sprite = Spritesheet.find(this.getEmitterData().getSpritesheet());
            if (sprite == null) {
                return null;
            }
            particle = new SpriteParticle(sprite.getSprite(MathUtilities.randomInRange(0, sprite.getTotalNumberOfSprites() - 1)), this.getRandomParticleTTL()).setX(x).setY(y).setDeltaIncX(gravityX).setDeltaIncY(gravityY).setDeltaX(deltaX).setDeltaY(deltaY).setDeltaWidth(deltaWidth).setDeltaHeight(deltaHeight);
            break;
        default:
            particle = new RectangleFillParticle(width, height, this.getRandomParticleColor(), this.getRandomParticleTTL()).setX(x).setY(y).setDeltaIncX(gravityX).setDeltaIncY(gravityY).setDeltaX(deltaX).setDeltaY(deltaY).setDeltaWidth(deltaWidth).setDeltaHeight(deltaHeight);
            break;
    }
    particle.setDeltaWidth(deltaWidth);
    particle.setDeltaHeight(deltaHeight);
    particle.setCollisionType(this.getEmitterData().getCollisionType());
    return particle;
}
Also used : Particle(de.gurkenlabs.litiengine.graphics.particles.Particle) TextParticle(de.gurkenlabs.litiengine.graphics.particles.TextParticle) RectangleFillParticle(de.gurkenlabs.litiengine.graphics.particles.RectangleFillParticle) OvalParticle(de.gurkenlabs.litiengine.graphics.particles.OvalParticle) RightLineParticle(de.gurkenlabs.litiengine.graphics.particles.RightLineParticle) ShimmerParticle(de.gurkenlabs.litiengine.graphics.particles.ShimmerParticle) RectangleOutlineParticle(de.gurkenlabs.litiengine.graphics.particles.RectangleOutlineParticle) LeftLineParticle(de.gurkenlabs.litiengine.graphics.particles.LeftLineParticle) SpriteParticle(de.gurkenlabs.litiengine.graphics.particles.SpriteParticle) RectangleFillParticle(de.gurkenlabs.litiengine.graphics.particles.RectangleFillParticle) RectangleOutlineParticle(de.gurkenlabs.litiengine.graphics.particles.RectangleOutlineParticle) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) TextParticle(de.gurkenlabs.litiengine.graphics.particles.TextParticle) SpriteParticle(de.gurkenlabs.litiengine.graphics.particles.SpriteParticle) LeftLineParticle(de.gurkenlabs.litiengine.graphics.particles.LeftLineParticle) RightLineParticle(de.gurkenlabs.litiengine.graphics.particles.RightLineParticle) OvalParticle(de.gurkenlabs.litiengine.graphics.particles.OvalParticle) ShimmerParticle(de.gurkenlabs.litiengine.graphics.particles.ShimmerParticle)

Example 5 with Spritesheet

use of de.gurkenlabs.litiengine.graphics.Spritesheet in project litiengine by gurkenlabs.

the class PropAnimationController method findSpriteSheet.

private Spritesheet findSpriteSheet(final Prop prop, final PropState state) {
    if (prop == null || prop.getSpritesheetName() == null || prop.getSpritesheetName().isEmpty()) {
        return null;
    }
    final String propState = state.name().toLowerCase();
    final String name = "prop-" + prop.getSpritesheetName().toLowerCase() + "-" + propState;
    Spritesheet sprite = Spritesheet.find(name);
    if (sprite != null) {
        return sprite;
    }
    final String fallbackName = "prop-" + prop.getSpritesheetName().toLowerCase();
    return Spritesheet.find(fallbackName);
}
Also used : Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet)

Aggregations

Spritesheet (de.gurkenlabs.litiengine.graphics.Spritesheet)17 BufferedImage (java.awt.image.BufferedImage)9 SpriteSheetInfo (de.gurkenlabs.litiengine.SpriteSheetInfo)6 ImageIcon (javax.swing.ImageIcon)4 ITileset (de.gurkenlabs.litiengine.environment.tilemap.ITileset)3 Blueprint (de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint)3 ImageFormat (de.gurkenlabs.litiengine.graphics.ImageFormat)3 File (java.io.File)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 JFileChooser (javax.swing.JFileChooser)3 FileFilter (javax.swing.filechooser.FileFilter)3 FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)3 Game (de.gurkenlabs.litiengine.Game)2 Resources (de.gurkenlabs.litiengine.Resources)2 IImageLayer (de.gurkenlabs.litiengine.environment.tilemap.IImageLayer)2 IMap (de.gurkenlabs.litiengine.environment.tilemap.IMap)2 Map (de.gurkenlabs.litiengine.environment.tilemap.xml.Map)2 MapObject (de.gurkenlabs.litiengine.environment.tilemap.xml.MapObject)2 Tileset (de.gurkenlabs.litiengine.environment.tilemap.xml.Tileset)2