bevy_ecs_ldtk
bevy_ecs_ldtk copied to clipboard
[Feature request] Level respawn API
I miss a way to restart the level (or all levels in the set). What I looking for is a convenient way to:
- Request a respawn
- Receive
LevelEvent::Despawned
so I could run my cleanup logic - Receive
LevelEvent::Spawned
so I could run my initialization logic
Proposed solution
I would imagine an Event consumer of the library can send, which is handled by a couple of systems in an order:
-
despawn_system
, removing current or requested level fromLevelSet
. Run beforeLdtkSystemLabel::PreSpawn
. -
respawn_system
, inserting current or requested level toLevelSet
. Run afterLdtkSystemLabel::PreSpawn
but beforeCoreStage::Update
.
This way apply_level_set
system can react to the removal of the level and trigger Despawned
event; there will be 1 frame gap until respawn though.
I implemented roughly this idea in this branch
Thanks for the issue! Will give this some thought for sure
It turns out that the plugin needs a new respawning mechanism internally due to hot reloading being broken (#108). I'm thinking of making a new Respawn
component. This component can be inserted on a Handle<LdtkAsset>
entity, a Handle<LdtkLevel>
entity, or an EntityInstance
entity, and when it is, the plugin will respawn those entities/their children.
When inserted on the world/level, the appropriate systems will respawn them by:
- despawning their descendants if they have children
- spawning them normally if they don't have children, and removing the
Respawn
component.
The plugin can insert this on the world on AssetEvent::Modified
for hot reloading.
The approach for EntityInstance
will have to be slightly different, and I haven't completely thought through the implementation yet. This use case may be worth putting off until a future release, but I think it would be a reasonable feature eventually.