Skip to content

Implement dynamic autotiling - #338

Open
haath wants to merge 6 commits into
Trouv:mainfrom
haath:main
Open

Implement dynamic autotiling#338
haath wants to merge 6 commits into
Trouv:mainfrom
haath:main

Conversation

@haath

@haath haath commented Nov 7, 2024

Copy link
Copy Markdown

Hello!

With this PR I'd like to add support for dynamic auto-tiling.
And by that I mean that layers who are auto-tiled depending on another int grid layer, will now have their tiles automatically updated whenever the values of the corresponding IntGridCell are changed.

You can see this in action in the screencap below, which happens to be captured from the new autotile example that I've also added to this repo.

demo

This is for now a draft PR and a request for feedback.

In particular, I would ideally like some maintainer to help me with the following questions.

  1. Should this functionality always be available, or enabled via a cargo feature?
  2. Should it be always on for all auto layers? Or perhaps controlled by some marker component (e.g EnableAutoLayer)? In the case of the latter, should the marker be added to all newly spawned auto layers by default, or should the user opt-in to this functionality?
  3. The system I've added currently starts by looking up which layers are affected for each int grid layer, and this is done by querying up to find the project and get the information from the json metadata. Since this inter-layer dependency will not change since loading the project though, I propose to cache this information as a new field in LayerMetadata. Do you agree? Or even better in a new component, since this only concerns int grid layers.

The implementation itself is also a work in progress, after we settle on the above I'd like to do some refactoring and maybe also add some tests if it makes sense.

@haath haath changed the title Implement autotiling Implement dynamic autotiling Nov 7, 2024
@haath

haath commented Dec 30, 2024

Copy link
Copy Markdown
Author

Since the PR went a bit under the radar, I took some liberties with my previous questions to make it ready for the review.

The current design goes like this:

  1. The dynamic autotiling behavior is enabled with a new component called EnableDynamicAutotiling which is added by default to all layers.
  2. A new component on int grid layers called IntGridLayerAffectedLayers keeps the list of the other layer Entity ids which have some dependency on it for autotiling. It gets initialized for newly spawned layers by the new init_int_grid_affected_layers system.
  3. A new component on int grid layers called IntGridLayerCellValues keeps a copy of the 2D array of current int grid cell values on the layer. This is used both to speed up lookups, and to avoid triggering changes unnecessary. This is updated by the new update_int_grid_layer_values system which watches for Changed<IntGridCell>.
  4. The new system apply_int_grid_autotiling watches for Changed<IntGridLayerCellValues> and performs the autotiling on all affected layers accordingly.

I guess some further optimization can be achieved by storing the autotiling rules (which is a Vec<AutoLayerRuleGroup) on each autotiled layer. What do you think?

Also some cases are currently not handled, and they error-out here:

debug_assert!(!rule.perlin_active);
debug_assert!(!rule.flip_x);
debug_assert!(!rule.flip_y);
debug_assert_eq!(rule.checker, Checker::None);
debug_assert!([1, 3, 5, 7, 9].contains(&rule.size));
debug_assert!(!rule.tile_rects_ids.is_empty());
debug_assert!(rule.tile_rects_ids.iter().all(|rect| rect.len() == 1));

@haath
haath marked this pull request as ready for review December 30, 2024 18:27
@Trouv Trouv added A-Spawning-LDtk Area: regarding the systems around spawning LDtk worlds/levels/layers, etc. A-ECS-Api Area: regarding the ECS items exposed to interact/configure this plugin D-Complex Difficulty: rewrites and unusually complex features. C-Feature Category: a new feature, making something new possible. S-Needs-Review Status: this PR needs reviewer attention to move forward. labels Nov 27, 2025
@Trouv

Trouv commented Jan 6, 2026

Copy link
Copy Markdown
Owner

Sorry I never got to this, it's been a crazy year or two. This is some impressive work, I appreciate the effort.

The main issue for me to decide here is whether or not this is "in scope" for this plugin. Typically I've defined the scope as: assets for LDtk projects + spawning of LDtk levels w/ some hooks. This is more like a re-implementation of LDtk autotile rules, which I think is definitely within the bounds of "LDtk integration" but not really in the bounds of "spawning LDtk levels". So, this is scope creep, but it may be acceptable scope creep. I'd like to hear your thoughts on this too.

Should this functionality always be available, or enabled via a cargo feature?

Absolutely, I think this is heavy enough to warrant a separate feature flag.

My question to you is - how difficult would it be to make this a 3rd party crate? And more specifically, if there's some inelegance or annoyance in making it a 3rd party crate, what changes could we make to this plugin to support it better?

I guess some further optimization can be achieved by storing the autotiling rules (which is a Vec<AutoLayerRuleGroup) on each autotiled layer. What do you think?

I'd prefer to keep the code simpler and suboptimal for an initial implementation.

@Trouv Trouv added S-Waiting-On-Author Status: the author needs to make changes to this PR before it can be approved. and removed S-Needs-Review Status: this PR needs reviewer attention to move forward. labels Jan 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ECS-Api Area: regarding the ECS items exposed to interact/configure this plugin A-Spawning-LDtk Area: regarding the systems around spawning LDtk worlds/levels/layers, etc. C-Feature Category: a new feature, making something new possible. D-Complex Difficulty: rewrites and unusually complex features. S-Waiting-On-Author Status: the author needs to make changes to this PR before it can be approved.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants