AlmasB / FXGL

Java / JavaFX / Kotlin Game Library (Engine)
http://almasb.github.io/FXGL/
MIT License
4.45k stars 556 forks source link

Support of infinite map from Tiled #1388

Closed almost-wizard closed 2 months ago

almost-wizard commented 2 months ago

Hi! I have a question. Can the library support infinite-size maps from Tiled? I'm trying to create an infinite map in Tiled, export it in tmx format, save it in the resources/assets/levels/map.tmx directory, and then write the following in the code:

public class Runner extends GameApplication {
    @Override
    protected void initGame() {
        FXGL.getGameWorld().addEntityFactory(new GameFactory());
        FXGL.setLevelFromMap("map.tmx");
    }

    public static void main(String[] args) {
        launch(args);
    }
}

When the program starts, it returns the following error:

Message:  Initialization failed
Type:  RuntimeException
Method:  FXGLApplication$InitAppTask.failed()
Line:  FXGLApplication.kt:343

java.lang.RuntimeException: Initialization failed
    at com.almasb.fxgl.all@17.3/com.almasb.fxgl.app.FXGLApplication$InitAppTask.failed(FXGLApplication.kt:343)
    at javafx.graphics@22-ea/javafx.concurrent.Task.setState(Task.java:710)
    at javafx.graphics@22-ea/javafx.concurrent.Task$TaskCallable.lambda$call$2(Task.java:1429)
    at javafx.graphics@22-ea/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
    at javafx.graphics@22-ea/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
    at javafx.graphics@22-ea/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at javafx.graphics@22-ea/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics@22-ea/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:185)
    at java.base/java.lang.Thread.run(Thread.java:1570)
Caused by: com.almasb.fxgl.entity.level.LevelLoadingException: ParseError at [row,col]:[8,46]
Message: elementGetText() function expects text only elment but START_ELEMENT was encountered.
    at com.almasb.fxgl.entity@17.3/com.almasb.fxgl.entity.level.tiled.TMXLevelLoader.load(TMXLevelLoader.kt:78)
    at com.almasb.fxgl.all@17.3/com.almasb.fxgl.app.services.FXGLAssetLoaderService.loadLevel(FXGLAssetLoaderService.kt:430)
    at com.almasb.fxgl.all@17.3/com.almasb.fxgl.dsl.FXGL$Companion.setLevelFromMap(FXGL.kt:634)
    at com.almasb.fxgl.all@17.3/com.almasb.fxgl.dsl.FXGL.setLevelFromMap(FXGL.kt)
    at ru.rsreu.gorobchenko.project/ru.rsreu.gorobchenko.project.Runner.initGame(Runner.java:76)
    at com.almasb.fxgl.all@17.3/com.almasb.fxgl.app.FXGLApplication$InitAppTask.initGame(FXGLApplication.kt:339)
    at com.almasb.fxgl.all@17.3/com.almasb.fxgl.app.FXGLApplication$InitAppTask.call(FXGLApplication.kt:319)
    at com.almasb.fxgl.all@17.3/com.almasb.fxgl.app.FXGLApplication$InitAppTask.call(FXGLApplication.kt:311)
    at javafx.graphics@22-ea/javafx.concurrent.Task$TaskCallable.call(Task.java:1399)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
    ... 1 more
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[8,46]
Message: elementGetText() function expects text only elment but START_ELEMENT was encountered.
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.getElementText(XMLStreamReaderImpl.java:918)
    at java.xml/com.sun.xml.internal.stream.XMLEventReaderImpl.getElementText(XMLEventReaderImpl.java:187)
    at com.almasb.fxgl.entity@17.3/com.almasb.fxgl.entity.level.tiled.TMXLevelLoader.parse(TMXLevelLoader.kt:225)
    at com.almasb.fxgl.entity@17.3/com.almasb.fxgl.entity.level.tiled.TMXLevelLoader.load(TMXLevelLoader.kt:52)
    ... 14 more

If the library does not support "infinite" maps, how can I optimize the loading of large maps? Do I need to write my own map renderer by chunks?

AlmasB commented 2 months ago

I'm afraid they are not implemented yet, see #1265 . However, in the meantime you can try something like this

almost-wizard commented 2 months ago

I understand you, it's a pity that the library does not directly support infinite maps yet. I will wait for updates, but in the meantime I will close the issue. Thanks for the advice