|
| 1 | +use bevy::prelude::*; |
| 2 | +use bevy_mod_scripting::prelude::*; |
| 3 | + |
| 4 | +#[derive(Component, Reflect)] |
| 5 | +#[reflect(Component)] |
| 6 | +pub struct ComponentA; |
| 7 | + |
| 8 | +#[derive(Component, Reflect)] |
| 9 | +#[reflect(Component)] |
| 10 | +pub struct ComponentB; |
| 11 | + |
| 12 | +#[derive(Component, Reflect)] |
| 13 | +#[reflect(Component)] |
| 14 | +pub struct ComponentC; |
| 15 | + |
| 16 | +fn main() { |
| 17 | + let mut app = App::new(); |
| 18 | + |
| 19 | + app.add_plugins((DefaultPlugins, ScriptingPlugin)) |
| 20 | + .register_type::<ComponentA>() |
| 21 | + .register_type::<ComponentB>() |
| 22 | + .register_type::<ComponentC>() |
| 23 | + .add_script_host::<LuaScriptHost<()>>(PostUpdate) |
| 24 | + .add_script_handler::<LuaScriptHost<()>, 0, 0>(PostUpdate) |
| 25 | + .add_api_provider::<LuaScriptHost<()>>(Box::new(LuaBevyAPIProvider)) |
| 26 | + .add_api_provider::<LuaScriptHost<()>>(Box::new(LuaCoreBevyAPIProvider)) |
| 27 | + .add_systems(Startup, (setup, apply_deferred, run).chain()) |
| 28 | + .run(); |
| 29 | +} |
| 30 | + |
| 31 | +fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { |
| 32 | + commands.spawn((ComponentA,)); |
| 33 | + commands.spawn((ComponentA, ComponentB)); |
| 34 | + commands.spawn((ComponentA, ComponentC)); |
| 35 | + commands.spawn((ComponentA, ComponentB, ComponentC)); |
| 36 | + |
| 37 | + commands.spawn((ComponentB,)); |
| 38 | + commands.spawn((ComponentB, ComponentC)); |
| 39 | + commands.spawn((ComponentB, ComponentA)); |
| 40 | + commands.spawn((ComponentB, ComponentA, ComponentC)); |
| 41 | + |
| 42 | + commands.spawn((ComponentC,)); |
| 43 | + commands.spawn((ComponentC, ComponentA)); |
| 44 | + commands.spawn((ComponentC, ComponentB)); |
| 45 | + commands.spawn((ComponentC, ComponentA, ComponentB)); |
| 46 | + |
| 47 | + let path = "scripts/dynamic_queries.lua"; |
| 48 | + let handle = asset_server.load(path); |
| 49 | + |
| 50 | + commands.spawn(ScriptCollection::<LuaFile> { |
| 51 | + scripts: vec![Script::new(path.into(), handle)], |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +fn run(mut events: PriorityEventWriter<LuaEvent<()>>) { |
| 56 | + events.send( |
| 57 | + LuaEvent { |
| 58 | + hook_name: "on_event".into(), |
| 59 | + args: (), |
| 60 | + recipients: Recipients::All, |
| 61 | + }, |
| 62 | + 0, |
| 63 | + ); |
| 64 | +} |
0 commit comments