feat(components): added a big set of demo components with various tupple types, structs etc

* added test_components file with example rust components code
 * updated blend file & co with an object using all these components
This commit is contained in:
kaosat.dev 2023-07-27 15:14:17 +02:00
parent 428bfe8efa
commit c402f51355
6 changed files with 155 additions and 11 deletions

Binary file not shown.

Binary file not shown.

View File

@ -125,35 +125,84 @@ pub fn gltf_extras_to_components(
parsed_value = ron::from_str(parsed_value.as_str()).unwrap_or(parsed_value); parsed_value = ron::from_str(parsed_value.as_str()).unwrap_or(parsed_value);
if let Some(type_registration) = type_registry.get_with_short_name(capitalized_type_name.as_str()) { if let Some(type_registration) = type_registry.get_with_short_name(capitalized_type_name.as_str()) {
debug!("parsed value {}",parsed_value); // FIXME : in the long run, replace all the text based, clunky unoptimised things with use of type_registration.type_info()
let blabla = type_registry.get_with_name("bevy_render::color::Color").unwrap();
println!("TYPE INFO {:?}", type_registration.type_info());
// println!("parsed value {}",parsed_value);
if parsed_value == "" { if parsed_value == "" {
parsed_value = "()".to_string(); parsed_value = "()".to_string();
} }
if parsed_value.starts_with("[") && parsed_value.ends_with("]") { else if parsed_value.starts_with("[") && parsed_value.ends_with("]") {
// FIXME/ horrible, and how about actual vec!s and not vec2/vec3 s? // FIXME/ horrible, and how about actual vec!s and not vec2/vec3 s?
// the worst part is, we have the component information, so we SHOULD be able to infer the input struct type
// perhaps use better logic than the unwrap ? we could do parsed:Vec<u64> as well, and drop it otherwise ?
let parsed: Vec<f32> = ron::from_str(&parsed_value).unwrap(); let parsed: Vec<f32> = ron::from_str(&parsed_value).unwrap();
if parsed.len() == 2 {
parsed_value = format!("((x:{},y:{}))", parsed[0], parsed[1]);
}
// FIXME god awfull hacks
if parsed.len() == 3 && !key.to_lowercase().contains("color"){
parsed_value = format!("((x:{},y:{},z:{}))", parsed[0], parsed[1], parsed[2]);
}
if parsed.len() == 3 && key.to_lowercase().contains("color"){
parsed_value = format!("(Rgba(red:{},green:{},blue:{}, alpha: 1.0))", parsed[0], parsed[1], parsed[2]);
}
if parsed.len() == 4 && !key.to_lowercase().contains("color") {
parsed_value = format!("((x:{},y:{},z:{},w:{}))", parsed[0], parsed[1], parsed[2], parsed[3]);
}
if parsed.len() == 4 && key.to_lowercase().contains("color"){
parsed_value = format!("(Rgba(red:{},green:{},blue:{}, alpha:{}))", parsed[0], parsed[1], parsed[2], parsed[3]);
}
/*match ron::from_str(&parsed_value) {
Err(e) => panic!("{:?}", e),
Ok::<Vec<f32>, _>(parsed) => {
if parsed.len() == 2 { if parsed.len() == 2 {
parsed_value = format!("((x:{},y:{}))", parsed[0], parsed[1]); parsed_value = format!("((x:{},y:{}))", parsed[0], parsed[1]);
} }
if parsed.len() == 3 { if parsed.len() == 3 {
parsed_value = format!("((x:{},y:{},z:{}))", parsed[0], parsed[1], parsed[2]); parsed_value = format!("((x:{},y:{},z:{}))", parsed[0], parsed[1], parsed[2]);
} }
},
Ok::<Color, _>(parsed) => {
parsed_value = format!("((x:{},y:{}))", parsed[0], parsed[1]);
}
}*/
} }
// FIXME: inneficient
else if parsed_value.starts_with("\"") && parsed_value.ends_with("\"") {
parsed_value = format!("({})",parsed_value);
}
else {
// FIXME: inneficient
if let Ok(parsed) = parsed_value.parse::<f32>() {
parsed_value = format!("({})",parsed);
}
else if let Ok(parsed) = parsed_value.parse::<f64>() {
parsed_value = format!("({})",parsed);
}
else if let Ok(parsed) = parsed_value.parse::<u64>() {
parsed_value = format!("({})",parsed);
}
else if let Ok(parsed) = parsed_value.parse::<bool>() {
parsed_value = format!("({})",parsed);
}
}
let ron_string = format!("{{ \"{}\":{} }}", let ron_string = format!("{{ \"{}\":{} }}",
type_registration.type_name(), type_registration.type_name(),
parsed_value parsed_value
); );
/*
let test_struct = CameraTrackingOffset::default(); // usefull to determine what an entity looks like Serialized
/*let test_struct = TuppleTestStr::default();
let serializer = ReflectSerializer::new(&test_struct, &type_registry); let serializer = ReflectSerializer::new(&test_struct, &type_registry);
let serialized = let serialized =
ron::ser::to_string_pretty(&serializer, ron::ser::PrettyConfig::default()).unwrap(); ron::ser::to_string_pretty(&serializer, ron::ser::PrettyConfig::default()).unwrap();
println!("serialized player {}", serialized);*/ println!("serialized Component {}", serialized);*/
debug!("component data json string {}", ron_string); debug!("component data ron string {}", ron_string);
let mut deserializer = ron::Deserializer::from_str(ron_string.as_str()).unwrap(); let mut deserializer = ron::Deserializer::from_str(ron_string.as_str()).unwrap();
let reflect_deserializer = UntypedReflectDeserializer::new(&type_registry); let reflect_deserializer = UntypedReflectDeserializer::new(&type_registry);
let component = reflect_deserializer.deserialize(&mut deserializer).expect(format!("failed to deserialize component {} with value: {}", key, value).as_str()); let component = reflect_deserializer.deserialize(&mut deserializer).expect(format!("failed to deserialize component {} with value: {}", key, value).as_str());

View File

@ -33,6 +33,12 @@ pub struct ShouldBeWithPlayer;
/// Demo marker component /// Demo marker component
pub struct Interactible; pub struct Interactible;
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
/// Demo marker component
pub struct Pickable;
fn player_move_demo( fn player_move_demo(
keycode: Res<Input<KeyCode>>, keycode: Res<Input<KeyCode>>,
@ -87,6 +93,7 @@ impl Plugin for DemoPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app app
.register_type::<Interactible>() .register_type::<Interactible>()
.register_type::<Pickable>()
.register_type::<SoundMaterial>() .register_type::<SoundMaterial>()
.register_type::<Player>() .register_type::<Player>()
// little helper utility, to automatically inject components that are dependant on an other component // little helper utility, to automatically inject components that are dependant on an other component

View File

@ -7,7 +7,10 @@ mod core;
use crate::core::*; use crate::core::*;
mod game; mod game;
use crate::game::*; use game::*;
mod test_components;
use test_components::*;
#[derive(Component, Reflect, Default, Debug, )] #[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)] #[reflect(Component)]
@ -43,7 +46,8 @@ fn main(){
RapierDebugRenderPlugin::default(), RapierDebugRenderPlugin::default(),
// our custom plugins // our custom plugins
CorePlugin, // reusable plugins CorePlugin, // reusable plugins
DemoPlugin // specific to our game DemoPlugin, // specific to our game
ComponentsTestPlugin // Showcases different type of components /structs
)) ))
.add_state::<AppState>() .add_state::<AppState>()

84
src/test_components.rs Normal file
View File

@ -0,0 +1,84 @@
use bevy::prelude::*;
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
struct UnitTest;
#[derive(Component, Reflect, Default, Debug, Deref, DerefMut)]
#[reflect(Component)]
struct TuppleTestF32(f32);
#[derive(Component, Reflect, Default, Debug, Deref, DerefMut)]
#[reflect(Component)]
struct TuppleTestU64(u64);
#[derive(Component, Reflect, Default, Debug, Deref, DerefMut)]
#[reflect(Component)]
pub struct TuppleTestStr(String);
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
struct TuppleTest2(f32, u64, String);
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
struct TuppleTestBool(bool);
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
struct TuppleVec2(Vec2);
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
struct TuppleVec3(Vec3);
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
struct TuppleVec(Vec<String>);
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
struct TuppleTestColor(Color);
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
struct BasicTest{
a: f32,
b: u64,
c: String
}
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
pub enum EnumTest{
Metal,
Wood,
Rock,
Cloth,
Squishy,
#[default]
None
}
pub struct ComponentsTestPlugin;
impl Plugin for ComponentsTestPlugin {
fn build(&self, app: &mut App) {
app
.register_type::<BasicTest>()
.register_type::<UnitTest>()
.register_type::<TuppleTestF32>()
.register_type::<TuppleTestU64>()
.register_type::<TuppleTestStr>()
.register_type::<TuppleTestBool>()
.register_type::<TuppleTest2>()
.register_type::<TuppleVec2>()
.register_type::<TuppleVec3>()
.register_type::<EnumTest>()
.register_type::<TuppleTestColor>()
// .register_type::<TuppleVec>()
;
}
}