bevy 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Cargo.lock +4279 -0
- data/Cargo.toml +36 -0
- data/README.md +226 -0
- data/crates/bevy/Cargo.toml +52 -0
- data/crates/bevy/src/app.rs +43 -0
- data/crates/bevy/src/component.rs +111 -0
- data/crates/bevy/src/entity.rs +30 -0
- data/crates/bevy/src/error.rs +32 -0
- data/crates/bevy/src/event.rs +190 -0
- data/crates/bevy/src/input_bridge.rs +300 -0
- data/crates/bevy/src/lib.rs +42 -0
- data/crates/bevy/src/mesh_renderer.rs +328 -0
- data/crates/bevy/src/query.rs +53 -0
- data/crates/bevy/src/render_app.rs +689 -0
- data/crates/bevy/src/resource.rs +28 -0
- data/crates/bevy/src/schedule.rs +186 -0
- data/crates/bevy/src/sprite_renderer.rs +355 -0
- data/crates/bevy/src/system.rs +44 -0
- data/crates/bevy/src/text_renderer.rs +258 -0
- data/crates/bevy/src/types/color.rs +114 -0
- data/crates/bevy/src/types/dynamic.rs +131 -0
- data/crates/bevy/src/types/math.rs +260 -0
- data/crates/bevy/src/types/mod.rs +9 -0
- data/crates/bevy/src/types/transform.rs +166 -0
- data/crates/bevy/src/world.rs +163 -0
- data/crates/bevy_ruby_render/Cargo.toml +22 -0
- data/crates/bevy_ruby_render/src/asset.rs +360 -0
- data/crates/bevy_ruby_render/src/audio.rs +511 -0
- data/crates/bevy_ruby_render/src/camera.rs +365 -0
- data/crates/bevy_ruby_render/src/gamepad.rs +398 -0
- data/crates/bevy_ruby_render/src/lib.rs +26 -0
- data/crates/bevy_ruby_render/src/material.rs +310 -0
- data/crates/bevy_ruby_render/src/mesh.rs +491 -0
- data/crates/bevy_ruby_render/src/sprite.rs +289 -0
- data/ext/bevy/Cargo.toml +20 -0
- data/ext/bevy/extconf.rb +6 -0
- data/ext/bevy/src/conversions.rs +137 -0
- data/ext/bevy/src/lib.rs +29 -0
- data/ext/bevy/src/ruby_app.rs +65 -0
- data/ext/bevy/src/ruby_color.rs +149 -0
- data/ext/bevy/src/ruby_component.rs +189 -0
- data/ext/bevy/src/ruby_entity.rs +33 -0
- data/ext/bevy/src/ruby_math.rs +384 -0
- data/ext/bevy/src/ruby_query.rs +64 -0
- data/ext/bevy/src/ruby_render_app.rs +779 -0
- data/ext/bevy/src/ruby_system.rs +122 -0
- data/ext/bevy/src/ruby_world.rs +107 -0
- data/lib/bevy/animation.rb +597 -0
- data/lib/bevy/app.rb +675 -0
- data/lib/bevy/asset.rb +613 -0
- data/lib/bevy/audio.rb +545 -0
- data/lib/bevy/audio_effects.rb +224 -0
- data/lib/bevy/camera.rb +412 -0
- data/lib/bevy/component.rb +91 -0
- data/lib/bevy/diagnostics.rb +227 -0
- data/lib/bevy/ecs_advanced.rb +296 -0
- data/lib/bevy/event.rb +199 -0
- data/lib/bevy/gizmos.rb +158 -0
- data/lib/bevy/gltf.rb +227 -0
- data/lib/bevy/hierarchy.rb +444 -0
- data/lib/bevy/input.rb +514 -0
- data/lib/bevy/lighting.rb +369 -0
- data/lib/bevy/material.rb +248 -0
- data/lib/bevy/mesh.rb +257 -0
- data/lib/bevy/navigation.rb +344 -0
- data/lib/bevy/networking.rb +335 -0
- data/lib/bevy/particle.rb +337 -0
- data/lib/bevy/physics.rb +396 -0
- data/lib/bevy/plugins/default_plugins.rb +34 -0
- data/lib/bevy/plugins/input_plugin.rb +49 -0
- data/lib/bevy/reflect.rb +361 -0
- data/lib/bevy/render_graph.rb +210 -0
- data/lib/bevy/resource.rb +185 -0
- data/lib/bevy/scene.rb +254 -0
- data/lib/bevy/shader.rb +319 -0
- data/lib/bevy/shape.rb +195 -0
- data/lib/bevy/skeletal.rb +248 -0
- data/lib/bevy/sprite.rb +152 -0
- data/lib/bevy/sprite_sheet.rb +444 -0
- data/lib/bevy/state.rb +277 -0
- data/lib/bevy/system.rb +206 -0
- data/lib/bevy/text.rb +99 -0
- data/lib/bevy/text_advanced.rb +455 -0
- data/lib/bevy/timer.rb +147 -0
- data/lib/bevy/transform.rb +158 -0
- data/lib/bevy/ui.rb +454 -0
- data/lib/bevy/ui_advanced.rb +568 -0
- data/lib/bevy/version.rb +5 -0
- data/lib/bevy/visibility.rb +250 -0
- data/lib/bevy/window.rb +302 -0
- data/lib/bevy.rb +390 -0
- metadata +150 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
use bevy_ruby::{DynamicComponent, DynamicValue};
|
|
2
|
+
use magnus::{function, method, prelude::*, Error, RHash, RModule, Ruby, Symbol, Value};
|
|
3
|
+
use std::cell::RefCell;
|
|
4
|
+
use std::collections::HashMap;
|
|
5
|
+
|
|
6
|
+
#[magnus::wrap(class = "Bevy::Component", free_immediately, size)]
|
|
7
|
+
pub struct RubyComponent {
|
|
8
|
+
inner: RefCell<DynamicComponent>,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
impl RubyComponent {
|
|
12
|
+
pub fn new(type_name: String) -> Self {
|
|
13
|
+
Self {
|
|
14
|
+
inner: RefCell::new(DynamicComponent::new(&type_name)),
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
pub fn from_dynamic(component: DynamicComponent) -> Self {
|
|
19
|
+
Self {
|
|
20
|
+
inner: RefCell::new(component),
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
pub fn inner(&self) -> DynamicComponent {
|
|
25
|
+
self.inner.borrow().clone()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
fn type_name(&self) -> String {
|
|
29
|
+
self.inner.borrow().type_name().to_string()
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
fn get(&self, name: String) -> Result<Value, Error> {
|
|
33
|
+
let ruby = Ruby::get().unwrap();
|
|
34
|
+
let inner = self.inner.borrow();
|
|
35
|
+
match inner.get(&name) {
|
|
36
|
+
Some(value) => dynamic_value_to_ruby(&ruby, value),
|
|
37
|
+
None => Ok(ruby.qnil().as_value()),
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
fn set(&self, name: String, value: Value) -> Result<Value, Error> {
|
|
42
|
+
let dynamic_value = ruby_to_dynamic_value(value)?;
|
|
43
|
+
self.inner.borrow_mut().set(&name, dynamic_value);
|
|
44
|
+
Ok(value)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
fn to_h(&self) -> Result<RHash, Error> {
|
|
48
|
+
let ruby = Ruby::get().unwrap();
|
|
49
|
+
let hash = ruby.hash_new();
|
|
50
|
+
let inner = self.inner.borrow();
|
|
51
|
+
|
|
52
|
+
for (key, value) in &inner.data {
|
|
53
|
+
let ruby_value = dynamic_value_to_ruby(&ruby, value)?;
|
|
54
|
+
hash.aset(ruby.to_symbol(key), ruby_value)?;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
Ok(hash)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
fn from_hash(type_name: String, hash: RHash) -> Result<Self, Error> {
|
|
61
|
+
let mut component = DynamicComponent::new(&type_name);
|
|
62
|
+
|
|
63
|
+
hash.foreach(|key: Value, value: Value| {
|
|
64
|
+
let key_str = if let Ok(sym) = Symbol::try_convert(key) {
|
|
65
|
+
sym.name().map(|s| s.to_string()).unwrap_or_default()
|
|
66
|
+
} else {
|
|
67
|
+
key.to_string()
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
if let Ok(dynamic_value) = ruby_to_dynamic_value(value) {
|
|
71
|
+
component.set(&key_str, dynamic_value);
|
|
72
|
+
}
|
|
73
|
+
Ok(magnus::r_hash::ForEach::Continue)
|
|
74
|
+
})?;
|
|
75
|
+
|
|
76
|
+
Ok(Self {
|
|
77
|
+
inner: RefCell::new(component),
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
unsafe impl Send for RubyComponent {}
|
|
83
|
+
|
|
84
|
+
fn dynamic_value_to_ruby(ruby: &Ruby, value: &DynamicValue) -> Result<Value, Error> {
|
|
85
|
+
match value {
|
|
86
|
+
DynamicValue::Nil => Ok(ruby.qnil().as_value()),
|
|
87
|
+
DynamicValue::Boolean(b) => Ok(if *b {
|
|
88
|
+
ruby.qtrue().as_value()
|
|
89
|
+
} else {
|
|
90
|
+
ruby.qfalse().as_value()
|
|
91
|
+
}),
|
|
92
|
+
DynamicValue::Integer(i) => Ok(ruby.integer_from_i64(*i).as_value()),
|
|
93
|
+
DynamicValue::Float(f) => Ok(ruby.float_from_f64(*f).as_value()),
|
|
94
|
+
DynamicValue::String(s) => Ok(ruby.str_new(s).as_value()),
|
|
95
|
+
DynamicValue::Symbol(s) => Ok(ruby.to_symbol(s).as_value()),
|
|
96
|
+
DynamicValue::Array(arr) => {
|
|
97
|
+
let ruby_arr = ruby.ary_new();
|
|
98
|
+
for item in arr {
|
|
99
|
+
ruby_arr.push(dynamic_value_to_ruby(ruby, item)?)?;
|
|
100
|
+
}
|
|
101
|
+
Ok(ruby_arr.as_value())
|
|
102
|
+
}
|
|
103
|
+
DynamicValue::Hash(h) => {
|
|
104
|
+
let ruby_hash = ruby.hash_new();
|
|
105
|
+
for (k, v) in h {
|
|
106
|
+
ruby_hash.aset(ruby.to_symbol(k), dynamic_value_to_ruby(ruby, v)?)?;
|
|
107
|
+
}
|
|
108
|
+
Ok(ruby_hash.as_value())
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
fn ruby_to_dynamic_value(value: Value) -> Result<DynamicValue, Error> {
|
|
114
|
+
let ruby = Ruby::get().unwrap();
|
|
115
|
+
|
|
116
|
+
if value.is_nil() {
|
|
117
|
+
return Ok(DynamicValue::Nil);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if value.is_kind_of(ruby.class_true_class()) || value.is_kind_of(ruby.class_false_class()) {
|
|
121
|
+
if let Ok(b) = bool::try_convert(value) {
|
|
122
|
+
return Ok(DynamicValue::Boolean(b));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if value.is_kind_of(ruby.class_integer()) {
|
|
127
|
+
if let Ok(i) = i64::try_convert(value) {
|
|
128
|
+
return Ok(DynamicValue::Integer(i));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if value.is_kind_of(ruby.class_float()) {
|
|
133
|
+
if let Ok(f) = f64::try_convert(value) {
|
|
134
|
+
return Ok(DynamicValue::Float(f));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if let Ok(sym) = Symbol::try_convert(value) {
|
|
139
|
+
return Ok(DynamicValue::Symbol(
|
|
140
|
+
sym.name().map(|s| s.to_string()).unwrap_or_default(),
|
|
141
|
+
));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if value.is_kind_of(ruby.class_string()) {
|
|
145
|
+
if let Ok(s) = String::try_convert(value) {
|
|
146
|
+
return Ok(DynamicValue::String(s));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if let Ok(arr) = magnus::RArray::try_convert(value) {
|
|
151
|
+
let mut result = Vec::new();
|
|
152
|
+
for item in arr.into_iter() {
|
|
153
|
+
result.push(ruby_to_dynamic_value(item)?);
|
|
154
|
+
}
|
|
155
|
+
return Ok(DynamicValue::Array(result));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if let Ok(hash) = RHash::try_convert(value) {
|
|
159
|
+
let mut result = HashMap::new();
|
|
160
|
+
hash.foreach(|k: Value, v: Value| {
|
|
161
|
+
let key = if let Ok(sym) = Symbol::try_convert(k) {
|
|
162
|
+
sym.name().map(|s| s.to_string()).unwrap_or_default()
|
|
163
|
+
} else {
|
|
164
|
+
k.to_string()
|
|
165
|
+
};
|
|
166
|
+
if let Ok(dv) = ruby_to_dynamic_value(v) {
|
|
167
|
+
result.insert(key, dv);
|
|
168
|
+
}
|
|
169
|
+
Ok(magnus::r_hash::ForEach::Continue)
|
|
170
|
+
})?;
|
|
171
|
+
return Ok(DynamicValue::Hash(result));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
Err(Error::new(
|
|
175
|
+
ruby.exception_type_error(),
|
|
176
|
+
format!("Cannot convert {:?} to DynamicValue", value),
|
|
177
|
+
))
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
pub fn define(ruby: &Ruby, module: &RModule) -> Result<(), Error> {
|
|
181
|
+
let class = module.define_class("Component", ruby.class_object())?;
|
|
182
|
+
class.define_singleton_method("new", function!(RubyComponent::new, 1))?;
|
|
183
|
+
class.define_singleton_method("from_hash", function!(RubyComponent::from_hash, 2))?;
|
|
184
|
+
class.define_method("type_name", method!(RubyComponent::type_name, 0))?;
|
|
185
|
+
class.define_method("[]", method!(RubyComponent::get, 1))?;
|
|
186
|
+
class.define_method("[]=", method!(RubyComponent::set, 2))?;
|
|
187
|
+
class.define_method("to_h", method!(RubyComponent::to_h, 0))?;
|
|
188
|
+
Ok(())
|
|
189
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
use bevy_ruby::EntityWrapper;
|
|
2
|
+
use magnus::{method, prelude::*, Error, RModule, Ruby};
|
|
3
|
+
|
|
4
|
+
#[magnus::wrap(class = "Bevy::Entity", free_immediately, size)]
|
|
5
|
+
pub struct RubyEntity {
|
|
6
|
+
inner: EntityWrapper,
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
impl RubyEntity {
|
|
10
|
+
pub fn new(entity: EntityWrapper) -> Self {
|
|
11
|
+
Self { inner: entity }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
pub fn inner(&self) -> EntityWrapper {
|
|
15
|
+
self.inner
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
fn id(&self) -> u64 {
|
|
19
|
+
self.inner.id()
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
impl PartialEq for RubyEntity {
|
|
24
|
+
fn eq(&self, other: &Self) -> bool {
|
|
25
|
+
self.inner == other.inner
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
pub fn define(ruby: &Ruby, module: &RModule) -> Result<(), Error> {
|
|
30
|
+
let class = module.define_class("Entity", ruby.class_object())?;
|
|
31
|
+
class.define_method("id", method!(RubyEntity::id, 0))?;
|
|
32
|
+
Ok(())
|
|
33
|
+
}
|
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
use bevy_ruby::{RubyQuat, RubyVec2, RubyVec3};
|
|
2
|
+
use magnus::{function, method, prelude::*, Error, RArray, RModule, Ruby};
|
|
3
|
+
use std::cell::RefCell;
|
|
4
|
+
|
|
5
|
+
#[magnus::wrap(class = "Bevy::Vec2", free_immediately, size)]
|
|
6
|
+
pub struct MagnusVec2 {
|
|
7
|
+
inner: RefCell<RubyVec2>,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
impl MagnusVec2 {
|
|
11
|
+
fn new(x: f64, y: f64) -> Self {
|
|
12
|
+
Self {
|
|
13
|
+
inner: RefCell::new(RubyVec2::new(x as f32, y as f32)),
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
fn zero() -> Self {
|
|
18
|
+
Self {
|
|
19
|
+
inner: RefCell::new(RubyVec2::zero()),
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
fn one() -> Self {
|
|
24
|
+
Self {
|
|
25
|
+
inner: RefCell::new(RubyVec2::one()),
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
fn x(&self) -> f64 {
|
|
30
|
+
self.inner.borrow().x() as f64
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
fn y(&self) -> f64 {
|
|
34
|
+
self.inner.borrow().y() as f64
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
fn set_x(&self, x: f64) {
|
|
38
|
+
self.inner.borrow_mut().set_x(x as f32);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
fn set_y(&self, y: f64) {
|
|
42
|
+
self.inner.borrow_mut().set_y(y as f32);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
fn length(&self) -> f64 {
|
|
46
|
+
self.inner.borrow().length() as f64
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
fn length_squared(&self) -> f64 {
|
|
50
|
+
self.inner.borrow().length_squared() as f64
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
fn normalize(&self) -> Self {
|
|
54
|
+
Self {
|
|
55
|
+
inner: RefCell::new(self.inner.borrow().normalize()),
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
fn dot(&self, other: &MagnusVec2) -> f64 {
|
|
60
|
+
self.inner.borrow().dot(&other.inner.borrow()) as f64
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
fn add(&self, other: &MagnusVec2) -> Self {
|
|
64
|
+
Self {
|
|
65
|
+
inner: RefCell::new(self.inner.borrow().add(&other.inner.borrow())),
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
fn sub(&self, other: &MagnusVec2) -> Self {
|
|
70
|
+
Self {
|
|
71
|
+
inner: RefCell::new(self.inner.borrow().sub(&other.inner.borrow())),
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
fn mul(&self, scalar: f64) -> Self {
|
|
76
|
+
Self {
|
|
77
|
+
inner: RefCell::new(self.inner.borrow().mul(scalar as f32)),
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
fn div(&self, scalar: f64) -> Self {
|
|
82
|
+
Self {
|
|
83
|
+
inner: RefCell::new(self.inner.borrow().div(scalar as f32)),
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
fn distance(&self, other: &MagnusVec2) -> f64 {
|
|
88
|
+
self.inner.borrow().distance(&other.inner.borrow()) as f64
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fn to_a(&self) -> Result<RArray, Error> {
|
|
92
|
+
let ruby = Ruby::get().unwrap();
|
|
93
|
+
let arr = ruby.ary_new();
|
|
94
|
+
let v = self.inner.borrow();
|
|
95
|
+
arr.push(v.x() as f64)?;
|
|
96
|
+
arr.push(v.y() as f64)?;
|
|
97
|
+
Ok(arr)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
pub fn inner(&self) -> RubyVec2 {
|
|
101
|
+
*self.inner.borrow()
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
unsafe impl Send for MagnusVec2 {}
|
|
106
|
+
|
|
107
|
+
#[magnus::wrap(class = "Bevy::Vec3", free_immediately, size)]
|
|
108
|
+
pub struct MagnusVec3 {
|
|
109
|
+
inner: RefCell<RubyVec3>,
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
impl MagnusVec3 {
|
|
113
|
+
fn new(x: f64, y: f64, z: f64) -> Self {
|
|
114
|
+
Self {
|
|
115
|
+
inner: RefCell::new(RubyVec3::new(x as f32, y as f32, z as f32)),
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
fn zero() -> Self {
|
|
120
|
+
Self {
|
|
121
|
+
inner: RefCell::new(RubyVec3::zero()),
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
fn one() -> Self {
|
|
126
|
+
Self {
|
|
127
|
+
inner: RefCell::new(RubyVec3::one()),
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
fn x(&self) -> f64 {
|
|
132
|
+
self.inner.borrow().x() as f64
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
fn y(&self) -> f64 {
|
|
136
|
+
self.inner.borrow().y() as f64
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
fn z(&self) -> f64 {
|
|
140
|
+
self.inner.borrow().z() as f64
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
fn set_x(&self, x: f64) {
|
|
144
|
+
self.inner.borrow_mut().set_x(x as f32);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
fn set_y(&self, y: f64) {
|
|
148
|
+
self.inner.borrow_mut().set_y(y as f32);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
fn set_z(&self, z: f64) {
|
|
152
|
+
self.inner.borrow_mut().set_z(z as f32);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
fn length(&self) -> f64 {
|
|
156
|
+
self.inner.borrow().length() as f64
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
fn length_squared(&self) -> f64 {
|
|
160
|
+
self.inner.borrow().length_squared() as f64
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
fn normalize(&self) -> Self {
|
|
164
|
+
Self {
|
|
165
|
+
inner: RefCell::new(self.inner.borrow().normalize()),
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
fn dot(&self, other: &MagnusVec3) -> f64 {
|
|
170
|
+
self.inner.borrow().dot(&other.inner.borrow()) as f64
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
fn cross(&self, other: &MagnusVec3) -> Self {
|
|
174
|
+
Self {
|
|
175
|
+
inner: RefCell::new(self.inner.borrow().cross(&other.inner.borrow())),
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
fn add(&self, other: &MagnusVec3) -> Self {
|
|
180
|
+
Self {
|
|
181
|
+
inner: RefCell::new(self.inner.borrow().add(&other.inner.borrow())),
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
fn sub(&self, other: &MagnusVec3) -> Self {
|
|
186
|
+
Self {
|
|
187
|
+
inner: RefCell::new(self.inner.borrow().sub(&other.inner.borrow())),
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
fn mul(&self, scalar: f64) -> Self {
|
|
192
|
+
Self {
|
|
193
|
+
inner: RefCell::new(self.inner.borrow().mul(scalar as f32)),
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
fn div(&self, scalar: f64) -> Self {
|
|
198
|
+
Self {
|
|
199
|
+
inner: RefCell::new(self.inner.borrow().div(scalar as f32)),
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
fn distance(&self, other: &MagnusVec3) -> f64 {
|
|
204
|
+
self.inner.borrow().distance(&other.inner.borrow()) as f64
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
fn to_a(&self) -> Result<RArray, Error> {
|
|
208
|
+
let ruby = Ruby::get().unwrap();
|
|
209
|
+
let arr = ruby.ary_new();
|
|
210
|
+
let v = self.inner.borrow();
|
|
211
|
+
arr.push(v.x() as f64)?;
|
|
212
|
+
arr.push(v.y() as f64)?;
|
|
213
|
+
arr.push(v.z() as f64)?;
|
|
214
|
+
Ok(arr)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
pub fn inner(&self) -> RubyVec3 {
|
|
218
|
+
*self.inner.borrow()
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
unsafe impl Send for MagnusVec3 {}
|
|
223
|
+
|
|
224
|
+
#[magnus::wrap(class = "Bevy::Quat", free_immediately, size)]
|
|
225
|
+
pub struct MagnusQuat {
|
|
226
|
+
inner: RefCell<RubyQuat>,
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
impl MagnusQuat {
|
|
230
|
+
fn identity() -> Self {
|
|
231
|
+
Self {
|
|
232
|
+
inner: RefCell::new(RubyQuat::identity()),
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
fn from_axis_angle(axis: &MagnusVec3, angle: f64) -> Self {
|
|
237
|
+
Self {
|
|
238
|
+
inner: RefCell::new(RubyQuat::from_axis_angle(&axis.inner(), angle as f32)),
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
fn from_euler(x: f64, y: f64, z: f64) -> Self {
|
|
243
|
+
Self {
|
|
244
|
+
inner: RefCell::new(RubyQuat::from_euler(x as f32, y as f32, z as f32)),
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
fn from_rotation_x(angle: f64) -> Self {
|
|
249
|
+
Self {
|
|
250
|
+
inner: RefCell::new(RubyQuat::from_rotation_x(angle as f32)),
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
fn from_rotation_y(angle: f64) -> Self {
|
|
255
|
+
Self {
|
|
256
|
+
inner: RefCell::new(RubyQuat::from_rotation_y(angle as f32)),
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
fn from_rotation_z(angle: f64) -> Self {
|
|
261
|
+
Self {
|
|
262
|
+
inner: RefCell::new(RubyQuat::from_rotation_z(angle as f32)),
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
fn x(&self) -> f64 {
|
|
267
|
+
self.inner.borrow().x() as f64
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
fn y(&self) -> f64 {
|
|
271
|
+
self.inner.borrow().y() as f64
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
fn z(&self) -> f64 {
|
|
275
|
+
self.inner.borrow().z() as f64
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
fn w(&self) -> f64 {
|
|
279
|
+
self.inner.borrow().w() as f64
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
fn normalize(&self) -> Self {
|
|
283
|
+
Self {
|
|
284
|
+
inner: RefCell::new(self.inner.borrow().normalize()),
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
fn inverse(&self) -> Self {
|
|
289
|
+
Self {
|
|
290
|
+
inner: RefCell::new(self.inner.borrow().inverse()),
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
fn mul_quat(&self, other: &MagnusQuat) -> Self {
|
|
295
|
+
Self {
|
|
296
|
+
inner: RefCell::new(self.inner.borrow().mul(&other.inner.borrow())),
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
fn mul_vec3(&self, v: &MagnusVec3) -> MagnusVec3 {
|
|
301
|
+
MagnusVec3 {
|
|
302
|
+
inner: RefCell::new(self.inner.borrow().mul_vec3(&v.inner())),
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
fn to_a(&self) -> Result<RArray, Error> {
|
|
307
|
+
let ruby = Ruby::get().unwrap();
|
|
308
|
+
let arr = ruby.ary_new();
|
|
309
|
+
let q = self.inner.borrow();
|
|
310
|
+
arr.push(q.x() as f64)?;
|
|
311
|
+
arr.push(q.y() as f64)?;
|
|
312
|
+
arr.push(q.z() as f64)?;
|
|
313
|
+
arr.push(q.w() as f64)?;
|
|
314
|
+
Ok(arr)
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
pub fn inner(&self) -> RubyQuat {
|
|
318
|
+
*self.inner.borrow()
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
unsafe impl Send for MagnusQuat {}
|
|
323
|
+
|
|
324
|
+
pub fn define(ruby: &Ruby, module: &RModule) -> Result<(), Error> {
|
|
325
|
+
let vec2_class = module.define_class("Vec2", ruby.class_object())?;
|
|
326
|
+
vec2_class.define_singleton_method("new", function!(MagnusVec2::new, 2))?;
|
|
327
|
+
vec2_class.define_singleton_method("zero", function!(MagnusVec2::zero, 0))?;
|
|
328
|
+
vec2_class.define_singleton_method("one", function!(MagnusVec2::one, 0))?;
|
|
329
|
+
vec2_class.define_method("x", method!(MagnusVec2::x, 0))?;
|
|
330
|
+
vec2_class.define_method("y", method!(MagnusVec2::y, 0))?;
|
|
331
|
+
vec2_class.define_method("x=", method!(MagnusVec2::set_x, 1))?;
|
|
332
|
+
vec2_class.define_method("y=", method!(MagnusVec2::set_y, 1))?;
|
|
333
|
+
vec2_class.define_method("length", method!(MagnusVec2::length, 0))?;
|
|
334
|
+
vec2_class.define_method("length_squared", method!(MagnusVec2::length_squared, 0))?;
|
|
335
|
+
vec2_class.define_method("normalize", method!(MagnusVec2::normalize, 0))?;
|
|
336
|
+
vec2_class.define_method("dot", method!(MagnusVec2::dot, 1))?;
|
|
337
|
+
vec2_class.define_method("+", method!(MagnusVec2::add, 1))?;
|
|
338
|
+
vec2_class.define_method("-", method!(MagnusVec2::sub, 1))?;
|
|
339
|
+
vec2_class.define_method("*", method!(MagnusVec2::mul, 1))?;
|
|
340
|
+
vec2_class.define_method("/", method!(MagnusVec2::div, 1))?;
|
|
341
|
+
vec2_class.define_method("distance", method!(MagnusVec2::distance, 1))?;
|
|
342
|
+
vec2_class.define_method("to_a", method!(MagnusVec2::to_a, 0))?;
|
|
343
|
+
|
|
344
|
+
let vec3_class = module.define_class("Vec3", ruby.class_object())?;
|
|
345
|
+
vec3_class.define_singleton_method("new", function!(MagnusVec3::new, 3))?;
|
|
346
|
+
vec3_class.define_singleton_method("zero", function!(MagnusVec3::zero, 0))?;
|
|
347
|
+
vec3_class.define_singleton_method("one", function!(MagnusVec3::one, 0))?;
|
|
348
|
+
vec3_class.define_method("x", method!(MagnusVec3::x, 0))?;
|
|
349
|
+
vec3_class.define_method("y", method!(MagnusVec3::y, 0))?;
|
|
350
|
+
vec3_class.define_method("z", method!(MagnusVec3::z, 0))?;
|
|
351
|
+
vec3_class.define_method("x=", method!(MagnusVec3::set_x, 1))?;
|
|
352
|
+
vec3_class.define_method("y=", method!(MagnusVec3::set_y, 1))?;
|
|
353
|
+
vec3_class.define_method("z=", method!(MagnusVec3::set_z, 1))?;
|
|
354
|
+
vec3_class.define_method("length", method!(MagnusVec3::length, 0))?;
|
|
355
|
+
vec3_class.define_method("length_squared", method!(MagnusVec3::length_squared, 0))?;
|
|
356
|
+
vec3_class.define_method("normalize", method!(MagnusVec3::normalize, 0))?;
|
|
357
|
+
vec3_class.define_method("dot", method!(MagnusVec3::dot, 1))?;
|
|
358
|
+
vec3_class.define_method("cross", method!(MagnusVec3::cross, 1))?;
|
|
359
|
+
vec3_class.define_method("+", method!(MagnusVec3::add, 1))?;
|
|
360
|
+
vec3_class.define_method("-", method!(MagnusVec3::sub, 1))?;
|
|
361
|
+
vec3_class.define_method("*", method!(MagnusVec3::mul, 1))?;
|
|
362
|
+
vec3_class.define_method("/", method!(MagnusVec3::div, 1))?;
|
|
363
|
+
vec3_class.define_method("distance", method!(MagnusVec3::distance, 1))?;
|
|
364
|
+
vec3_class.define_method("to_a", method!(MagnusVec3::to_a, 0))?;
|
|
365
|
+
|
|
366
|
+
let quat_class = module.define_class("Quat", ruby.class_object())?;
|
|
367
|
+
quat_class.define_singleton_method("identity", function!(MagnusQuat::identity, 0))?;
|
|
368
|
+
quat_class.define_singleton_method("from_axis_angle", function!(MagnusQuat::from_axis_angle, 2))?;
|
|
369
|
+
quat_class.define_singleton_method("from_euler", function!(MagnusQuat::from_euler, 3))?;
|
|
370
|
+
quat_class.define_singleton_method("from_rotation_x", function!(MagnusQuat::from_rotation_x, 1))?;
|
|
371
|
+
quat_class.define_singleton_method("from_rotation_y", function!(MagnusQuat::from_rotation_y, 1))?;
|
|
372
|
+
quat_class.define_singleton_method("from_rotation_z", function!(MagnusQuat::from_rotation_z, 1))?;
|
|
373
|
+
quat_class.define_method("x", method!(MagnusQuat::x, 0))?;
|
|
374
|
+
quat_class.define_method("y", method!(MagnusQuat::y, 0))?;
|
|
375
|
+
quat_class.define_method("z", method!(MagnusQuat::z, 0))?;
|
|
376
|
+
quat_class.define_method("w", method!(MagnusQuat::w, 0))?;
|
|
377
|
+
quat_class.define_method("normalize", method!(MagnusQuat::normalize, 0))?;
|
|
378
|
+
quat_class.define_method("inverse", method!(MagnusQuat::inverse, 0))?;
|
|
379
|
+
quat_class.define_method("*", method!(MagnusQuat::mul_quat, 1))?;
|
|
380
|
+
quat_class.define_method("mul_vec3", method!(MagnusQuat::mul_vec3, 1))?;
|
|
381
|
+
quat_class.define_method("to_a", method!(MagnusQuat::to_a, 0))?;
|
|
382
|
+
|
|
383
|
+
Ok(())
|
|
384
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
use bevy_ruby::QueryBuilder;
|
|
2
|
+
use magnus::{function, method, prelude::*, Error, RArray, RModule, Ruby};
|
|
3
|
+
use std::cell::RefCell;
|
|
4
|
+
|
|
5
|
+
#[magnus::wrap(class = "Bevy::QueryBuilder", free_immediately, size)]
|
|
6
|
+
pub struct RubyQueryBuilder {
|
|
7
|
+
inner: RefCell<QueryBuilder>,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
impl RubyQueryBuilder {
|
|
11
|
+
fn new() -> Self {
|
|
12
|
+
Self {
|
|
13
|
+
inner: RefCell::new(QueryBuilder::new()),
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
fn fetch(&self, component_name: String) -> Self {
|
|
18
|
+
let builder = self.inner.borrow().clone().fetch(&component_name);
|
|
19
|
+
Self {
|
|
20
|
+
inner: RefCell::new(builder),
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
fn fetch_components(&self) -> RArray {
|
|
25
|
+
let ruby = Ruby::get().unwrap();
|
|
26
|
+
let components = self.inner.borrow();
|
|
27
|
+
let fetch = components.fetch_components();
|
|
28
|
+
let array = ruby.ary_new_capa(fetch.len());
|
|
29
|
+
for name in fetch {
|
|
30
|
+
let _ = array.push(name.clone());
|
|
31
|
+
}
|
|
32
|
+
array
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
fn with_filter(&self, component_name: String) -> Self {
|
|
36
|
+
Self {
|
|
37
|
+
inner: RefCell::new(self.inner.borrow().clone().fetch(&component_name)),
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
fn without_filter(&self, _component_name: String) -> Self {
|
|
42
|
+
Self {
|
|
43
|
+
inner: RefCell::new(self.inner.borrow().clone()),
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
#[allow(dead_code)]
|
|
48
|
+
pub fn inner(&self) -> QueryBuilder {
|
|
49
|
+
self.inner.borrow().clone()
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
unsafe impl Send for RubyQueryBuilder {}
|
|
54
|
+
|
|
55
|
+
pub fn define(ruby: &Ruby, module: &RModule) -> Result<(), Error> {
|
|
56
|
+
let class = module.define_class("QueryBuilder", ruby.class_object())?;
|
|
57
|
+
class.define_singleton_method("new", function!(RubyQueryBuilder::new, 0))?;
|
|
58
|
+
class.define_method("fetch", method!(RubyQueryBuilder::fetch, 1))?;
|
|
59
|
+
class.define_method("fetch_components", method!(RubyQueryBuilder::fetch_components, 0))?;
|
|
60
|
+
class.define_method("with", method!(RubyQueryBuilder::with_filter, 1))?;
|
|
61
|
+
class.define_method("without", method!(RubyQueryBuilder::without_filter, 1))?;
|
|
62
|
+
|
|
63
|
+
Ok(())
|
|
64
|
+
}
|