AlmasB / FXGL

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

.tmx file - read, modify at runtime and save it back ? #1341

Open JavaGitHubber opened 7 months ago

JavaGitHubber commented 7 months ago

Hi Almas, hi everybody,

my son had an idea for a 2D game, a kind like RPG, where we can create a world scenario (~1600x900px) by using Tiled editor, tiled layers and tiled maps. So far so good.

Now the idea is to modify the scenario during the game, not only some pixel or small areas which could be done by objects and object layers, but in wide areas, e.g. an animated change of the basic ground floor with 10.000s of pixels on the tiled layer and, save it back to the tmx-file (or another format to load it back later again).

Game situation: Players can occupy and take over the opponent's lands; this should be indicated in the first step by changing the color of the occupied tiled elements. The development of the lands is represented by other surfaces, hurricans might destroy land and turn into ocean, etc.

How can I access directly to the Tiled Layer and its corresponding Tile Set?

Instead I first tried to create 1600 x 900 single objects to spawn each of them individually, but this is in my opinion not to handle, isn't it?

In other words,

Is this too crazy or to complicated? Or do I missed a simple way to achieve this right now?

Thanks so much. clas

AlmasB commented 7 months ago

Hi @JavaGitHubber

This sounds like more of a serialization question, rather than an asset loading one.

Once a .tmx is loaded as entities (game objects), each entity possesses a set of default properties, for example the player to which the tile belongs, the unit placed on that tile, etc. As the game progresses these properties get modified. At the end of the game, these properties can be saved to disk and reloaded as needed.

The algorithm overview might look like this:

  1. load the game level from .tmx
  2. generate entities with default properties
  3. overwrite properties from a game save file if present
  4. save properties to a game save file on exit

Hope this makes sense

JavaGitHubber commented 7 months ago

Hi Almas, hi everybody,

thank you for your answer and suggestions.

Yes I agree fully with yout aproach and I am familliar with handling and creating entities, assigning entities to objects that has been placed on the object layers in Tiled and handling and expanding their properties.

What I now want or beeter need to do is to manipulate the pixels or their tile set that is on the 'tilemap Layer'.

Do I have access directly to the Tiled Layer pixels and its corresponding Tile Set?

Thanks so much. clas

AlmasB commented 7 months ago

Hi, when a .tmx is loaded, it provides a Level object, which stores entities. Each tiled layer that is not an object layer will have an entity associated with it. These entities have their type set to "TiledMapLayer", so you should be able to grab them once loaded.

Their ViewComponent should have only 1 object, which is ImageView, whose image stores your pixels. ImagesKt class gives you the ability to manipulate pixels of any image as needed.

I don't think the tile set information is stored anywhere at runtime after parsing.