Search in sources :

Example 1 with IMapLoader

use of de.gurkenlabs.litiengine.environment.tilemap.IMapLoader in project litiengine by gurkenlabs.

the class MapComponent method loadMaps.

public void loadMaps(String projectPath) {
    final List<String> files = FileUtilities.findFilesByExtension(new ArrayList<>(), Paths.get(projectPath), "tmx");
    log.log(Level.INFO, "{0} maps found in folder {1}", new Object[] { files.size(), projectPath });
    final List<Map> loadedMaps = new ArrayList<>();
    for (final String mapFile : files) {
        final IMapLoader tmxLoader = new TmxMapLoader();
        Map map = (Map) tmxLoader.loadMap(mapFile);
        loadedMaps.add(map);
        log.log(Level.INFO, "map found: {0}", new Object[] { map.getFileName() });
    }
    this.loadMaps(loadedMaps);
}
Also used : TmxMapLoader(de.gurkenlabs.litiengine.environment.tilemap.TmxMapLoader) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) IMapLoader(de.gurkenlabs.litiengine.environment.tilemap.IMapLoader) Map(de.gurkenlabs.litiengine.environment.tilemap.xml.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IMap(de.gurkenlabs.litiengine.environment.tilemap.IMap)

Example 2 with IMapLoader

use of de.gurkenlabs.litiengine.environment.tilemap.IMapLoader in project litiengine by gurkenlabs.

the class MapComponent method importMap.

public void importMap() {
    if (this.getMaps() == null) {
        return;
    }
    final IMapLoader tmxLoader = new TmxMapLoader();
    XmlImportDialog.importXml("Tilemap", Map.FILE_EXTENSION, files -> {
        for (File file : files) {
            String mapPath = file.toString();
            Map map = (Map) tmxLoader.loadMap(mapPath);
            if (map == null) {
                log.log(Level.WARNING, "could not load map from file {0}", new Object[] { mapPath });
                return;
            }
            if (map.getMapObjectLayers().isEmpty()) {
                // make sure there's a map object layer on the map because we need one
                // to add any kind of entities
                MapObjectLayer layer = new MapObjectLayer();
                layer.setName(DEFAULT_MAPOBJECTLAYER_NAME);
                map.addMapObjectLayer(layer);
            }
            Optional<Map> current = this.maps.stream().filter(x -> x.getFileName().equals(map.getFileName())).findFirst();
            if (current.isPresent()) {
                int n = JOptionPane.showConfirmDialog(Game.getScreenManager().getRenderComponent(), Resources.get("input_replace_map", map.getFileName()), Resources.get("input_replace_map_title"), JOptionPane.YES_NO_OPTION);
                if (n == JOptionPane.YES_OPTION) {
                    this.getMaps().remove(current.get());
                } else {
                    return;
                }
            }
            this.getMaps().add(map);
            Collections.sort(this.getMaps());
            for (IImageLayer imageLayer : map.getImageLayers()) {
                BufferedImage img = Resources.getImage(imageLayer.getImage().getAbsoluteSourcePath(), true);
                Spritesheet sprite = Spritesheet.load(img, imageLayer.getImage().getSource(), img.getWidth(), img.getHeight());
                this.screen.getGameFile().getSpriteSheets().add(new SpriteSheetInfo(sprite));
            }
            // remove old spritesheets
            for (ITileset tileSet : map.getTilesets()) {
                this.loadTileset(tileSet, true);
            }
            // remove old tilesets
            for (ITileset tileset : map.getExternalTilesets()) {
                this.loadTileset(tileset, false);
            }
            EditorScreen.instance().updateGameFileMaps();
            ImageCache.clearAll();
            if (this.environments.containsKey(map.getFileName())) {
                this.environments.remove(map.getFileName());
            }
            EditorScreen.instance().getMapSelectionPanel().bind(this.getMaps(), true);
            this.loadEnvironment(map);
            log.log(Level.INFO, "imported map {0}", new Object[] { map.getFileName() });
        }
    });
}
Also used : Color(java.awt.Color) Arrays(java.util.Arrays) Point2D(java.awt.geom.Point2D) Rectangle2D(java.awt.geom.Rectangle2D) MapObject(de.gurkenlabs.litiengine.environment.tilemap.xml.MapObject) MathUtilities(de.gurkenlabs.litiengine.util.MathUtilities) SpriteSheetInfo(de.gurkenlabs.litiengine.SpriteSheetInfo) Map(de.gurkenlabs.litiengine.environment.tilemap.xml.Map) Ellipse2D(java.awt.geom.Ellipse2D) UndoManager(de.gurkenlabs.utiliti.UndoManager) IMapLoader(de.gurkenlabs.litiengine.environment.tilemap.IMapLoader) ImageCache(de.gurkenlabs.litiengine.graphics.ImageCache) FileUtilities(de.gurkenlabs.litiengine.util.io.FileUtilities) MapObjectProperty(de.gurkenlabs.litiengine.environment.tilemap.MapObjectProperty) 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) CollisionEntity(de.gurkenlabs.litiengine.entities.CollisionEntity) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) KeyEvent(java.awt.event.KeyEvent) Logger(java.util.logging.Logger) IEntity(de.gurkenlabs.litiengine.entities.IEntity) Program(de.gurkenlabs.utiliti.Program) ImageFormat(de.gurkenlabs.litiengine.graphics.ImageFormat) Dimension(java.awt.Dimension) List(java.util.List) Align(de.gurkenlabs.litiengine.Align) IMapObjectLayer(de.gurkenlabs.litiengine.environment.tilemap.IMapObjectLayer) XmlUtilities(de.gurkenlabs.litiengine.util.io.XmlUtilities) Entry(java.util.Map.Entry) ComponentMouseEvent(de.gurkenlabs.litiengine.gui.ComponentMouseEvent) Optional(java.util.Optional) BasicStroke(java.awt.BasicStroke) ImageSerializer(de.gurkenlabs.litiengine.util.io.ImageSerializer) Game(de.gurkenlabs.litiengine.Game) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ITileset(de.gurkenlabs.litiengine.environment.tilemap.ITileset) GeometricUtilities(de.gurkenlabs.litiengine.util.geom.GeometricUtilities) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) EditorScreen(de.gurkenlabs.utiliti.EditorScreen) IMap(de.gurkenlabs.litiengine.environment.tilemap.IMap) MapUtilities(de.gurkenlabs.litiengine.environment.tilemap.MapUtilities) IEnvironment(de.gurkenlabs.litiengine.environment.IEnvironment) IMapObject(de.gurkenlabs.litiengine.environment.tilemap.IMapObject) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Blueprint(de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint) FocusAdapter(java.awt.event.FocusAdapter) SwingUtilities(javax.swing.SwingUtilities) Graphics2D(java.awt.Graphics2D) MapObjectLayer(de.gurkenlabs.litiengine.environment.tilemap.xml.MapObjectLayer) LightSource(de.gurkenlabs.litiengine.graphics.LightSource) Stroke(java.awt.Stroke) Line2D(java.awt.geom.Line2D) Valign(de.gurkenlabs.litiengine.Valign) IOException(java.io.IOException) ComponentMouseWheelEvent(de.gurkenlabs.litiengine.gui.ComponentMouseWheelEvent) JOptionPane(javax.swing.JOptionPane) FileFilter(javax.swing.filechooser.FileFilter) File(java.io.File) TmxMapLoader(de.gurkenlabs.litiengine.environment.tilemap.TmxMapLoader) Path2D(java.awt.geom.Path2D) Input(de.gurkenlabs.litiengine.input.Input) Consumer(java.util.function.Consumer) FontMetrics(java.awt.FontMetrics) MapObjectType(de.gurkenlabs.litiengine.environment.tilemap.MapObjectType) Paths(java.nio.file.Paths) FocusEvent(java.awt.event.FocusEvent) GuiComponent(de.gurkenlabs.litiengine.gui.GuiComponent) Environment(de.gurkenlabs.litiengine.environment.Environment) RenderEngine(de.gurkenlabs.litiengine.graphics.RenderEngine) Collections(java.util.Collections) IUpdateable(de.gurkenlabs.litiengine.IUpdateable) XmlImportDialog(de.gurkenlabs.utiliti.swing.XmlImportDialog) TmxMapLoader(de.gurkenlabs.litiengine.environment.tilemap.TmxMapLoader) ITileset(de.gurkenlabs.litiengine.environment.tilemap.ITileset) IMapLoader(de.gurkenlabs.litiengine.environment.tilemap.IMapLoader) IImageLayer(de.gurkenlabs.litiengine.environment.tilemap.IImageLayer) Blueprint(de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint) BufferedImage(java.awt.image.BufferedImage) SpriteSheetInfo(de.gurkenlabs.litiengine.SpriteSheetInfo) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) File(java.io.File) Map(de.gurkenlabs.litiengine.environment.tilemap.xml.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IMap(de.gurkenlabs.litiengine.environment.tilemap.IMap) IMapObjectLayer(de.gurkenlabs.litiengine.environment.tilemap.IMapObjectLayer) MapObjectLayer(de.gurkenlabs.litiengine.environment.tilemap.xml.MapObjectLayer)

Aggregations

IMap (de.gurkenlabs.litiengine.environment.tilemap.IMap)2 IMapLoader (de.gurkenlabs.litiengine.environment.tilemap.IMapLoader)2 TmxMapLoader (de.gurkenlabs.litiengine.environment.tilemap.TmxMapLoader)2 Map (de.gurkenlabs.litiengine.environment.tilemap.xml.Map)2 Align (de.gurkenlabs.litiengine.Align)1 Game (de.gurkenlabs.litiengine.Game)1 IUpdateable (de.gurkenlabs.litiengine.IUpdateable)1 Resources (de.gurkenlabs.litiengine.Resources)1 SpriteSheetInfo (de.gurkenlabs.litiengine.SpriteSheetInfo)1 Valign (de.gurkenlabs.litiengine.Valign)1 CollisionEntity (de.gurkenlabs.litiengine.entities.CollisionEntity)1 IEntity (de.gurkenlabs.litiengine.entities.IEntity)1 Environment (de.gurkenlabs.litiengine.environment.Environment)1 IEnvironment (de.gurkenlabs.litiengine.environment.IEnvironment)1 IImageLayer (de.gurkenlabs.litiengine.environment.tilemap.IImageLayer)1 IMapObject (de.gurkenlabs.litiengine.environment.tilemap.IMapObject)1 IMapObjectLayer (de.gurkenlabs.litiengine.environment.tilemap.IMapObjectLayer)1 ITileset (de.gurkenlabs.litiengine.environment.tilemap.ITileset)1 MapObjectProperty (de.gurkenlabs.litiengine.environment.tilemap.MapObjectProperty)1 MapObjectType (de.gurkenlabs.litiengine.environment.tilemap.MapObjectType)1