Adding camera component for saved entity

attempt/001
Elijah C. Voigt 1 year ago
parent edb15ffe49
commit a54255c363

@ -11,7 +11,8 @@ This custom reading/writing can be found in `src/save.rs`.
All `.entity` files list one component per line. All `.entity` files list one component per line.
Components include: Components include:
* `name <string>` * `name <string>` Human readable name for entity
* `uuid <UUID string>` * `uuid <UUID string>` Globally unique ID for entity
* `transform translation <f32> <f32> <f32> rotation <f32> <f32> <f32> <f32> scale <f32> <f32> <f32>` * `transform translation <f32> <f32> <f32> rotation <f32> <f32> <f32> <f32> scale <f32> <f32> <f32>` 3D Location, Rotation, and Scale for entity
* `model "<path string>" "<string>"` * `model "<path string>" "<string>"` Declares the entity's 3d model
* `camera` Marks the entity as a camera

@ -0,0 +1,4 @@
name camera
uuid 2e45b7e9-6722-4d50-8ea5-67f25b8b0f62
transform translation -5.0 5.0 5.0 rotation 0.0 0.0 0.0 1.0 scale 1.0 1.0 1.0
camera

@ -9,7 +9,7 @@ impl Plugin for MenuPlugin {
} }
fn init_menu(mut commands: Commands) { fn init_menu(mut commands: Commands) {
commands.spawn(Camera3dBundle { ..default() }); // commands.spawn(Camera3dBundle { ..default() });
commands commands
.spawn(NodeBundle { .spawn(NodeBundle {
style: Style { style: Style {

@ -24,6 +24,7 @@ struct SaveEntity {
name: Option<Name>, name: Option<Name>,
uuid: Option<EntityUuid>, uuid: Option<EntityUuid>,
model: Option<(Handle<Gltf>, String)>, model: Option<(Handle<Gltf>, String)>,
camera: Option<bool>,
} }
impl SaveEntity { impl SaveEntity {
@ -43,6 +44,8 @@ impl SaveEntity {
} else if let Ok((gltf_path, scene_name)) = parse::parse_save_model(line) { } else if let Ok((gltf_path, scene_name)) = parse::parse_save_model(line) {
let handle = load_context.load(gltf_path); let handle = load_context.load(gltf_path);
entity.model = Some((handle, scene_name)); entity.model = Some((handle, scene_name));
} else if let Ok(is_camera) = parse::parse_save_camera(line) {
entity.camera = Some(is_camera);
} }
}); });
Ok(entity) Ok(entity)
@ -214,7 +217,6 @@ mod parse {
} }
pub(crate) fn parse_save_model(line: &str) -> Result<(String, String), SaveEntityParseError> { pub(crate) fn parse_save_model(line: &str) -> Result<(String, String), SaveEntityParseError> {
println!("{}", line);
let (rem, (_, _, gltf_name, _, scene_name)) = tuple(( let (rem, (_, _, gltf_name, _, scene_name)) = tuple((
tag("model"), tag("model"),
take(1usize), take(1usize),
@ -236,6 +238,27 @@ mod parse {
assert_eq!(parsed, expected); assert_eq!(parsed, expected);
} }
pub(crate) fn parse_save_camera(line: &str) -> Result<bool, SaveEntityParseError> {
let (_rem, cam) = tag("camera")(line)?;
Ok(cam.len() > 0)
}
#[test]
fn test_parse_camera() {
{
let line = "camera";
let parsed = parse_save_camera(line).unwrap();
let expected = true;
assert_eq!(parsed, expected);
}
{
let line = "notcamera";
let parsed = parse_save_camera(line).unwrap();
let expected = false;
assert_eq!(parsed, expected);
}
}
} }
#[derive(Default)] #[derive(Default)]
@ -308,6 +331,15 @@ fn check_loaded_entity_assets(
); );
let mut e = commands.entity(entity); let mut e = commands.entity(entity);
// Apply camera
// Should be applied early to avoid clobbering transform
if let Some(is_camera) = &saved.camera {
if *is_camera {
e.insert(Camera3dBundle { ..default() });
}
} else {
e.remove::<(Camera, Camera3d)>();
}
// Apply transform // Apply transform
if let Some(transform) = &saved.transform { if let Some(transform) = &saved.transform {
// TODO: Only update if different // TODO: Only update if different

Loading…
Cancel
Save