aboutsummaryrefslogtreecommitdiff
path: root/src/lua_api.rs
blob: f9a34d4f7007fc9819abaf2a3bd777a73240651c (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::str::FromStr;

use bevy::prelude::*;
use bevy_scriptum::{
    runtimes::lua::{BevyEntity, BevyVec3, LuaRuntime, LuaScriptData},
    Runtime, ScriptingRuntimeBuilder,
};
use uuid::Uuid;

use crate::{components::DirworldEntity, conditionals::Condition};

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,
        get_dirworld_id,
        condition_true,
        condition_ancestor_of,
        condition_descendant_of,
        condition_parent_of,
        condition_child_of,
        condition_in_room,
        condition_object_in_room
    )
}

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!");
        }
    }
}

fn get_dirworld_id(In((BevyEntity(entity),)): In<(BevyEntity,)>, dirworld_entity_query: Query<&DirworldEntity>) -> Option<String> {
    dirworld_entity_query.get(entity).ok().and_then(|entity| entity.payload.as_ref().map(|payload| payload.id.to_string()))
}

// Conditionals
fn condition_true(world: &mut World) -> bool {
    Condition::True.evaluate(world)
}

fn condition_ancestor_of(
    In((ancestor, descendant)): In<(String, String)>,
    world: &mut World,
) -> bool {
    let Ok(ancestor) = Uuid::from_str(&ancestor) else {
        warn!("Provided ancestor is not a valid UUID");
        return false;
    };
    let Ok(descendant) = Uuid::from_str(&descendant) else {
        warn!("Provided descendant is not a valid UUID");
        return false;
    };
    Condition::AncestorOf {
        ancestor,
        descendant,
    }
    .evaluate(world)
}

fn condition_descendant_of(
    In((descendant, ancestor)): In<(String, String)>,
    world: &mut World,
) -> bool {
    let Ok(ancestor) = Uuid::from_str(&ancestor) else {
        warn!("Provided ancestor is not a valid UUID");
        return false;
    };
    let Ok(descendant) = Uuid::from_str(&descendant) else {
        warn!("Provided descendant is not a valid UUID");
        return false;
    };
    Condition::DescendantOf {
        ancestor,
        descendant,
    }
    .evaluate(world)
}

fn condition_parent_of(In((parent, child)): In<(String, String)>, world: &mut World) -> bool {
    let Ok(parent) = Uuid::from_str(&parent) else {
        warn!("Provided parent is not a valid UUID");
        return false;
    };
    let Ok(child) = Uuid::from_str(&child) else {
        warn!("Provided child is not a valid UUID");
        return false;
    };
    Condition::ParentOf { parent, child }.evaluate(world)
}

fn condition_child_of(In((child, parent)): In<(String, String)>, world: &mut World) -> bool {
    let Ok(parent) = Uuid::from_str(&parent) else {
        warn!("Provided parent is not a valid UUID");
        return false;
    };
    let Ok(child) = Uuid::from_str(&child) else {
        warn!("Provided child is not a valid UUID");
        return false;
    };
    Condition::ChildOf { parent, child }.evaluate(world)
}

fn condition_in_room(In((room,)): In<(String,)>, world: &mut World) -> bool {
    let Ok(room) = Uuid::from_str(&room) else {
        warn!("Provided room is not a valid UUID");
        return false;
    };
    Condition::InRoom(room).evaluate(world)
}

fn condition_object_in_room(In((object,)): In<(String,)>, world: &mut World) -> bool {
    let Ok(object) = Uuid::from_str(&object) else {
        warn!("Provided object is not a valid UUID");
        return false;
    };
    Condition::ObjectInRoom(object).evaluate(world)
}

// }}}