aboutsummaryrefslogtreecommitdiff
path: root/src/lua_api.rs
blob: cda24863faa790741b3219d1c8bf74ce5cb48cd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use bevy::prelude::*;
use bevy_scriptum::{runtimes::lua::{BevyEntity, BevyVec3, LuaRuntime, LuaScriptData}, ScriptingRuntimeBuilder, Runtime};

pub fn trigger_update(
    mut scripted_entities: Query<(Entity, &mut LuaScriptData)>,
    scripting_runtime: Res<LuaRuntime>,
    time: Res<Time>,
) {
    let delta = time.delta_seconds();
    for (entity, mut script_data) in scripted_entities.iter_mut() {
        if let Err(e) = scripting_runtime.call_fn("on_update", &mut script_data, entity, (delta, )) {
            error!("Encountered lua scripting error: {:?}", e);
        }
    }
}

// ACTUAL API STUFF BELOW THIS POINT {{{

macro_rules! register_fns {
    ($runtime:expr, $($function:expr),+) => {
        { 
            $runtime$(.add_function(stringify!($function).to_string(), $function))+
        }
    };
}

pub fn register(runtime: ScriptingRuntimeBuilder<LuaRuntime>) -> ScriptingRuntimeBuilder<LuaRuntime> {
    register_fns!(runtime, translate, rotate)
}

fn translate(
    In((BevyEntity(entity), BevyVec3(translation))): In<(BevyEntity, BevyVec3)>,
    mut transform_query: Query<&mut Transform>,
) {
    if let Ok(mut transform) = transform_query.get_mut(entity) {
        transform.translation += translation;
    }
}

fn rotate(
    In((BevyEntity(entity), BevyVec3(axis), angle)): In<(BevyEntity, BevyVec3, f32)>,
    mut transform_query: Query<&mut Transform>,
) {
    if let Ok(mut transform) = transform_query.get_mut(entity) {
        if let Ok(direction) = Dir3::new(axis) {
            transform.rotate_axis(direction, angle);
        } else {
            warn!("Provided axis was not a valid direction!");
        }
    }
}

// }}}