|
| 1 | +use bevy::asset::ChangeWatcher; |
| 2 | +use bevy::prelude::*; |
| 3 | +use bevy_mod_scripting::prelude::*; |
| 4 | +use rand::prelude::SliceRandom; |
| 5 | +use std::sync::atomic::AtomicU32; |
| 6 | +use std::sync::atomic::Ordering::Relaxed; |
| 7 | +use std::time::Duration; |
| 8 | + |
| 9 | +#[derive(Clone)] |
| 10 | +pub struct MyRuneArg(usize); |
| 11 | + |
| 12 | +impl Args for MyRuneArg { |
| 13 | + fn into_stack(self, stack: &mut rune::runtime::Stack) -> rune::runtime::VmResult<()> { |
| 14 | + (self.0,).into_stack(stack) |
| 15 | + } |
| 16 | + |
| 17 | + fn try_into_vec(self) -> rune::runtime::VmResult<rune::alloc::Vec<rune::Value>> { |
| 18 | + (self.0,).try_into_vec() |
| 19 | + } |
| 20 | + |
| 21 | + fn count(&self) -> usize { |
| 22 | + 1 |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/// A custom Rune API. |
| 27 | +#[derive(Default)] |
| 28 | +pub struct RuneAPIProvider; |
| 29 | + |
| 30 | +impl APIProvider for RuneAPIProvider { |
| 31 | + type APITarget = Context; |
| 32 | + type DocTarget = RuneDocFragment; |
| 33 | + type ScriptContext = RuneScriptContext; |
| 34 | + |
| 35 | + fn attach_api(&mut self, ctx: &mut Self::APITarget) -> Result<(), ScriptError> { |
| 36 | + let mut module = rune::Module::new(); |
| 37 | + |
| 38 | + module |
| 39 | + .function("info", |msg: String| info!("{msg}")) |
| 40 | + .build() |
| 41 | + .map_err(ScriptError::new_other)?; |
| 42 | + |
| 43 | + ctx.install(module).map_err(ScriptError::new_other)?; |
| 44 | + |
| 45 | + Ok(()) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +static COUNTER: AtomicU32 = AtomicU32::new(0); |
| 50 | + |
| 51 | +/// Utility for generating random events from a list. |
| 52 | +fn fire_random_event( |
| 53 | + w: &mut PriorityEventWriter<RuneEvent<MyRuneArg>>, |
| 54 | + events: &[ScriptEventData], |
| 55 | +) { |
| 56 | + let mut rng = rand::thread_rng(); |
| 57 | + let id = COUNTER.fetch_add(1, Relaxed); |
| 58 | + let arg = MyRuneArg(id as usize); |
| 59 | + let event = events |
| 60 | + .choose(&mut rng) |
| 61 | + .map(|v| RuneEvent { |
| 62 | + hook_name: v.0.to_string(), |
| 63 | + args: arg, |
| 64 | + recipients: v.1.clone(), |
| 65 | + }) |
| 66 | + .unwrap(); |
| 67 | + |
| 68 | + info!( |
| 69 | + "\t - event: {},\t recipients: {:?},\t id: {}", |
| 70 | + event.hook_name, event.recipients, id |
| 71 | + ); |
| 72 | + w.send(event, 0); |
| 73 | +} |
| 74 | + |
| 75 | +fn do_update(mut w: PriorityEventWriter<RuneEvent<MyRuneArg>>) { |
| 76 | + info!("Update, firing:"); |
| 77 | + |
| 78 | + let all_events = [ |
| 79 | + ScriptEventData("on_event", Recipients::All), |
| 80 | + ScriptEventData("on_event", Recipients::ScriptID(0)), |
| 81 | + ScriptEventData("on_event", Recipients::ScriptID(1)), |
| 82 | + ScriptEventData( |
| 83 | + "on_event", |
| 84 | + Recipients::ScriptName("scripts/event_recipients.rune".to_owned()), |
| 85 | + ), |
| 86 | + ]; |
| 87 | + |
| 88 | + // fire random event, for any of the system sets |
| 89 | + fire_random_event(&mut w, &all_events); |
| 90 | +} |
| 91 | + |
| 92 | +#[derive(Clone)] |
| 93 | +pub struct ScriptEventData(&'static str, Recipients); |
| 94 | + |
| 95 | +fn load_scripts(server: Res<AssetServer>, mut commands: Commands) { |
| 96 | + // Spawn two identical scripts. |
| 97 | + // Their id's will be 0 and 1. |
| 98 | + let path = "scripts/event_recipients.rune"; |
| 99 | + let handle = server.load::<RuneFile, &str>(path); |
| 100 | + let scripts = (0..2) |
| 101 | + .map(|_| Script::<RuneFile>::new(path.to_string(), handle.clone())) |
| 102 | + .collect(); |
| 103 | + |
| 104 | + commands |
| 105 | + .spawn(()) |
| 106 | + .insert(ScriptCollection::<RuneFile> { scripts }); |
| 107 | +} |
| 108 | + |
| 109 | +fn main() { |
| 110 | + App::new() |
| 111 | + .add_plugins(DefaultPlugins.set(AssetPlugin { |
| 112 | + watch_for_changes: ChangeWatcher::with_delay(Duration::from_secs(0)), |
| 113 | + ..Default::default() |
| 114 | + })) |
| 115 | + .add_plugins(ScriptingPlugin) |
| 116 | + .add_systems(Startup, load_scripts) |
| 117 | + // Randomly fire events for either all scripts, the script with an id of `0`, |
| 118 | + // or the script with an id of `1`. |
| 119 | + .add_systems(Update, do_update) |
| 120 | + .add_script_handler::<RuneScriptHost<MyRuneArg>, 0, 0>(PostUpdate) |
| 121 | + .add_script_host::<RuneScriptHost<MyRuneArg>>(PostUpdate) |
| 122 | + .add_api_provider::<RuneScriptHost<MyRuneArg>>(Box::new(RuneAPIProvider)) |
| 123 | + .run() |
| 124 | +} |
0 commit comments