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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use bevy::prelude::*;
use crate::{
pico8::Clearable,
translate::{Position, Rotation},
};
use bevy_mod_scripting::{
bindings::function::{
from::Val, namespace::NamespaceBuilder, script_function::FunctionCallContext,
},
lua::mlua::{self, FromLua, Lua, UserData, Value},
};
#[cfg(feature = "scripting")]
use bevy_mod_scripting::{
bindings::{InteropError, IntoScriptRef, ReflectReference, WorldAccessGuard},
prelude::ScriptValue,
};
#[derive(Debug, Clone, Copy, Reflect)]
pub enum DropPolicy {
Nothing,
Despawn,
}
impl UserData for DropPolicy {}
impl FromLua for DropPolicy {
fn from_lua(value: Value, _: &Lua) -> mlua::Result<Self> {
match value {
Value::UserData(ud) => Ok(*ud.borrow::<Self>()?),
_ => unreachable!(),
}
}
}
impl Drop for N9Entity {
fn drop(&mut self) {
if matches!(self.drop, DropPolicy::Despawn) {
warn!("Retained entity leaked {:?}.", self.entity);
}
}
}
#[derive(Clone, Reflect)]
pub struct N9Entity {
pub entity: Entity,
pub drop: DropPolicy,
}
impl N9Entity {
#[cfg(feature = "scripting")]
pub fn into_script_ref(self, world: WorldAccessGuard) -> Result<ScriptValue, InteropError> {
let reference = {
let allocator = world.allocator();
let mut allocator = allocator.write();
ReflectReference::new_allocated(self, &mut allocator)
};
ReflectReference::into_script_ref(reference, world)
}
}
pub(crate) fn plugin(app: &mut App) {
NamespaceBuilder::<N9Entity>::new(app.world_mut())
.register(
"retain",
|ctx: FunctionCallContext, this: Val<N9Entity>, _z: Option<f32>| {
let world = ctx.world()?;
world.with_global_access(|world| {
let mut commands = world.commands();
commands.entity(this.entity).remove::<Clearable>();
// if let Some(mut transform) = world.get_mut::<Transform>(this.entity) {
// transform.translation.z = z.unwrap_or(0.0);
// }
})?;
Ok(this)
},
)
.register(
"pos",
|ctx: FunctionCallContext,
this: Val<N9Entity>,
x: Option<f32>,
y: Option<f32>,
// z: Option<f32>
| {
let world = ctx.world()?;
let pos = world.with_global_access(|world| {
if x.is_some() || y.is_some() {
world
.get_mut::<Position>(this.entity)
.map(|mut position| {
let last = position.0;
if let Some(x) = x {
position.0.x = x;
}
if let Some(y) = y {
position.0.y = y;
}
last
})
} else {
world
.get::<Position>(this.entity)
.map(|position| position.0)
}
})?;
if let Some(pos) = pos {
Ok(Some(vec![pos.x, pos.y]))
} else {
Ok(None)
}
},
)
.register(
"rot",
|ctx: FunctionCallContext,
this: Val<N9Entity>,
z: Option<f32>,
y: Option<f32>,
x: Option<f32>| {
let world = ctx.world()?;
let rot = world.with_global_access(|world| {
if x.is_some() || y.is_some() || z.is_some() {
world.get_mut::<Rotation>(this.entity).map(|mut rotation| {
let last = rotation.0;
// let last = transform.rotation.to_euler(EulerRot::ZYX);
let mut turns = last;
if let Some(z) = z {
turns.z = z;
}
if let Some(y) = y {
turns.y = y;
}
if let Some(x) = x {
turns.x = x;
}
rotation.0 = turns;
// transform.rotation =
// Quat::from_euler(EulerRot::ZYX, turns.0, turns.1, turns.2);
last
})
} else {
world
.get::<Rotation>(this.entity)
.map(|rotation| rotation.0)
}
})?;
if let Some(rot) = rot {
Ok(Some(vec![rot.z, rot.y, rot.x]))
} else {
Ok(None)
}
},
)
.register(
"name",
|ctx: FunctionCallContext, this: Val<N9Entity>, new_name: Option<String>| {
let world = ctx.world()?;
world.with_global_access(|world| {
if let Some(name) = new_name {
let mut commands = world.commands();
commands.entity(this.entity).insert(Name::new(name));
None
} else {
world
.get::<Name>(this.entity)
.map(|n| n.as_str().to_string())
}
})
},
)
.register(
"vis",
|ctx: FunctionCallContext, this: Val<N9Entity>, vis: Option<bool>| {
let world = ctx.world()?;
world.with_global_access(|world| {
if let Some(vis) = vis {
if let Some(mut visible) = world.get_mut::<Visibility>(this.entity) {
*visible = match vis {
// None => Visibility::Inherited,
true => Visibility::Visible,
false => Visibility::Hidden,
};
}
None
} else {
world
.get::<Visibility>(this.entity)
.map(|v| !matches!(v, Visibility::Hidden))
}
})
},
)
.register(
"despawn",
|ctx: FunctionCallContext, this: Val<N9Entity>| {
let world = ctx.world()?;
world.with_global_access(|world| {
let mut commands = world.commands();
commands.entity(this.entity).despawn();
})?;
Ok(())
},
);
}