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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
use std::{str::FromStr, sync::Mutex};
use bevy::prelude::*;
use bevy_mod_scripting::api::providers::bevy_reflect::LuaVec3;
use bevy_mod_scripting::{api::providers::bevy_ecs::LuaEntity, lua::tealr::mlu::mlua::Error as LuaError};
use bevy_mod_scripting::lua::LuaEvent;
use bevy_mod_scripting::prelude::*;
use uuid::Uuid;
use crate::{components::DirworldEntity, conditionals::Condition};
pub fn trigger_update(mut w: PriorityEventWriter<LuaEvent<()>>) {
let event = LuaEvent::<()> {
args: (),
hook_name: "on_update".into(),
recipients: Recipients::All,
};
w.send(event, 0);
}
// ACTUAL API STUFF BELOW THIS POINT {{{
macro_rules! register_fns {
($runtime:expr, $($function:expr),+) => {
{
let ctx = $runtime.get_mut().unwrap();
$(ctx.globals().set(stringify!($function).to_string(), ctx.create_function($function).unwrap()).unwrap();)+
}
};
}
pub fn register(api: &mut Mutex<Lua>) {
register_fns!(
api,
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(ctx: &Lua, (entity, translation): (LuaEntity, LuaVec3)) -> Result<(), LuaError> {
let world = ctx.get_world()?;
let mut world = world.write();
if let Some(mut transform) = world.entity_mut(entity.inner().unwrap()).get_mut::<Transform>() {
transform.translation += translation.inner().unwrap();
}
Ok(())
}
fn rotate(ctx: &Lua, (entity, axis, angle): (LuaEntity, LuaVec3, f32)) -> Result<(), LuaError> {
let world = ctx.get_world()?;
let mut world = world.write();
if let Some(mut transform) = world.entity_mut(entity.inner().unwrap()).get_mut::<Transform>() {
transform.rotation *= Quat::from_axis_angle(axis.inner().unwrap(), angle);
}
Ok(())
}
fn get_dirworld_id(ctx: &Lua, (entity,): (LuaEntity,)) -> Result<String, LuaError> {
let world = ctx.get_world()?;
let world = world.read();
if let Some(dirworld_entity) = world.entity(entity.inner().unwrap()).get::<DirworldEntity>() {
dirworld_entity.payload.as_ref().map(|p| p.id.to_string()).ok_or(LuaError::runtime("Failed to get entity id from payload"))
} else {
Err(LuaError::runtime("Entity missing DirworldEntity component"))
}
}
// Conditionals
pub struct ConditionalAPI;
impl APIProvider for ConditionalAPI {
type APITarget = Mutex<Lua>;
type ScriptContext = Mutex<Lua>;
type DocTarget = LuaDocFragment;
fn attach_api(
&mut self,
api: &mut Self::APITarget,
) -> Result<(), bevy_mod_scripting::prelude::ScriptError> {
register(api);
Ok(())
}
}
fn condition_true(ctx: &Lua, _: ()) -> Result<bool, LuaError> {
let world = ctx.get_world()?;
let mut world = world.write();
Ok(Condition::True.evaluate(&mut world))
}
fn condition_ancestor_of(ctx: &Lua, (ancestor, descendant): (String, String)) -> Result<bool, LuaError> {
let world = ctx.get_world()?;
let mut world = world.write();
let Ok(ancestor) = Uuid::from_str(&ancestor) else {
warn!("Provided ancestor is not a valid UUID");
return Ok(false);
};
let Ok(descendant) = Uuid::from_str(&descendant) else {
warn!("Provided descendant is not a valid UUID");
return Ok(false);
};
Ok(Condition::AncestorOf {
ancestor,
descendant,
}.evaluate(&mut world))
}
fn condition_descendant_of(ctx: &Lua, (descendant, ancestor): (String, String)) -> Result<bool, LuaError> {
let world = ctx.get_world()?;
let mut world = world.write();
let Ok(ancestor) = Uuid::from_str(&ancestor) else {
warn!("Provided ancestor is not a valid UUID");
return Ok(false);
};
let Ok(descendant) = Uuid::from_str(&descendant) else {
warn!("Provided descendant is not a valid UUID");
return Ok(false);
};
Ok(Condition::DescendantOf {
ancestor,
descendant,
}.evaluate(&mut world))
}
fn condition_parent_of(ctx: &Lua, (parent, child): (String, String)) -> Result<bool, LuaError> {
let world = ctx.get_world()?;
let mut world = world.write();
let Ok(parent) = Uuid::from_str(&parent) else {
warn!("Provided ancestor is not a valid UUID");
return Ok(false);
};
let Ok(child) = Uuid::from_str(&child) else {
warn!("Provided descendant is not a valid UUID");
return Ok(false);
};
Ok(Condition::ParentOf {
parent,
child,
}.evaluate(&mut world))
}
fn condition_child_of(ctx: &Lua, (child, parent): (String, String)) -> Result<bool, LuaError> {
let world = ctx.get_world()?;
let mut world = world.write();
let Ok(parent) = Uuid::from_str(&parent) else {
warn!("Provided ancestor is not a valid UUID");
return Ok(false);
};
let Ok(child) = Uuid::from_str(&child) else {
warn!("Provided descendant is not a valid UUID");
return Ok(false);
};
Ok(Condition::ChildOf {
parent,
child,
}.evaluate(&mut world))
}
fn condition_in_room(ctx: &Lua, (room,): (String,)) -> Result<bool, LuaError> {
let world = ctx.get_world()?;
let mut world = world.write();
let Ok(room) = Uuid::from_str(&room) else {
warn!("Provided room is not a valid UUID");
return Ok(false);
};
Ok(Condition::InRoom(room).evaluate(&mut world))
}
fn condition_object_in_room(ctx: &Lua, (object,): (String,)) -> Result<bool, LuaError> {
let world = ctx.get_world()?;
let mut world = world.write();
let Ok(object) = Uuid::from_str(&object) else {
warn!("Provided object is not a valid UUID");
return Ok(false);
};
Ok(Condition::ObjectInRoom(object).evaluate(&mut world))
}
// }}}
|