From 67b47aa01d841e16d0585b54ee4e9bd66464b35e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Hoz=C3=A1k?= <5235838+richardhozak@users.noreply.github.com> Date: Mon, 8 Jan 2024 13:06:47 +0100 Subject: [PATCH] Add world to scene hook --- src/hook.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/hook.rs b/src/hook.rs index de88f1b..77e1f55 100644 --- a/src/hook.rs +++ b/src/hook.rs @@ -46,7 +46,7 @@ pub struct SceneHooked; /// fn load_scene(mut cmds: Commands, asset_server: Res) { /// cmds.spawn(HookedSceneBundle { /// scene: SceneBundle { scene: asset_server.load("scene.glb#Scene0"), ..default() }, -/// hook: SceneHook::new(|entity, cmds| { +/// hook: SceneHook::new(|entity, cmds, _world| { /// match entity.get::().map(|t|t.as_str()) { /// Some("Pile") => cmds.insert(Pile(PileType::Drawing)), /// Some("Card") => cmds.insert(Card), @@ -58,7 +58,7 @@ pub struct SceneHooked; /// ``` #[derive(Component)] pub struct SceneHook { - hook: Box, + hook: Box, } impl SceneHook { /// Add a hook to a scene, to run for each entities when the scene is @@ -83,16 +83,18 @@ impl SceneHook { /// #[derive(Clone, Resource)] /// struct DeckAssets { player: Handle, oppo: Handle } /// - /// fn hook(decks: &DeckAssets, entity: &EntityRef, cmds: &mut EntityCommands) {} + /// fn hook(decks: &DeckAssets, entity: &EntityRef, cmds: &mut EntityCommands, world: &World) {} /// fn load_scene(mut cmds: Commands, decks: Res, assets: Res) { /// let decks = decks.clone(); /// cmds.spawn(HookedSceneBundle { /// scene: SceneBundle { scene: assets.load("scene.glb#Scene0"), ..default() }, - /// hook: SceneHook::new(move |entity, cmds| hook(&decks, entity, cmds)), + /// hook: SceneHook::new(move |entity, cmds, world| hook(&decks, entity, cmds, world)), /// }); /// } /// ``` - pub fn new(hook: F) -> Self { + pub fn new( + hook: F, + ) -> Self { Self { hook: Box::new(hook), } @@ -116,7 +118,7 @@ pub fn run_hooks( .chain(std::iter::once(entity)); for entity_ref in entities.filter_map(|e| world.get_entity(e)) { let mut cmd = cmds.entity(entity_ref.id()); - (hooked.hook)(&entity_ref, &mut cmd); + (hooked.hook)(&entity_ref, &mut cmd, world); } } }