From e5d399c3c502a6c5f6f83bc79fe30afb9503c876 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 26 Dec 2023 22:21:18 -0800 Subject: [PATCH] Added basic ship config --- content/ship.toml | 7 +++++ src/content/content.rs | 27 ++++++++--------- src/content/contenttype.rs | 2 ++ src/content/mod.rs | 1 + src/content/syntax/mod.rs | 1 + src/content/syntax/ship.rs | 60 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 85 insertions(+), 13 deletions(-) create mode 100644 content/ship.toml create mode 100644 src/content/syntax/ship.rs diff --git a/content/ship.toml b/content/ship.toml new file mode 100644 index 0000000..f9dea8a --- /dev/null +++ b/content/ship.toml @@ -0,0 +1,7 @@ +# content type: ship +[ship] +name = "Gypsum" +sprite = "ship::gypsum" +size = 100 + +engines = [{ x = 0.0, y = -105, size = 50.0 }] diff --git a/src/content/content.rs b/src/content/content.rs index 186cc2e..02cc379 100644 --- a/src/content/content.rs +++ b/src/content/content.rs @@ -6,28 +6,29 @@ use super::{syntax, ContentType}; #[derive(Debug)] pub struct Content { pub systems: Vec, + pub ships: Vec, } impl Content { pub fn new(cv: Vec<(PathBuf, ContentType)>) -> Result { - let mut content = Self { - systems: Vec::new(), - }; + let mut systems = Vec::new(); + let mut ships = Vec::new(); // These methods check intra-file consistency for (p, c) in cv { match c { - ContentType::System(v) => content - .add_system(v) - .with_context(|| format!("Could not parse {}", p.display()))?, - }; + ContentType::System(v) => systems.push( + syntax::system::System::parse(v) + .with_context(|| format!("Could not parse {}", p.display()))?, + ), + + ContentType::Ship(v) => ships.push( + syntax::ship::Ship::parse(v) + .with_context(|| format!("Could not parse {}", p.display()))?, + ), + } } - return Ok(content); - } - - fn add_system(&mut self, toml: syntax::system::toml::SystemRoot) -> Result<()> { - self.systems.push(syntax::system::System::parse(toml)?); - return Ok(()); + return Ok(Self { systems, ships }); } } diff --git a/src/content/contenttype.rs b/src/content/contenttype.rs index 50a85b4..4360868 100644 --- a/src/content/contenttype.rs +++ b/src/content/contenttype.rs @@ -6,6 +6,7 @@ use super::syntax; #[derive(Debug)] pub enum ContentType { System(syntax::system::toml::SystemRoot), + Ship(syntax::ship::toml::ShipRoot), } // TODO: check content without loading game @@ -26,6 +27,7 @@ impl ContentType { return Ok(match &type_spec[..] { "system" => Some(Self::System(toml::from_str(&file_string)?)), + "ship" => Some(Self::Ship(toml::from_str(&file_string)?)), _ => bail!("Invalid content type `{}`", type_spec), }); } diff --git a/src/content/mod.rs b/src/content/mod.rs index 80d5ed3..5884a97 100644 --- a/src/content/mod.rs +++ b/src/content/mod.rs @@ -4,6 +4,7 @@ mod syntax; pub use content::Content; pub use contenttype::ContentType; +pub use syntax::ship; pub use syntax::system; use anyhow::{Context, Result}; diff --git a/src/content/syntax/mod.rs b/src/content/syntax/mod.rs index 7a70559..0ac84c8 100644 --- a/src/content/syntax/mod.rs +++ b/src/content/syntax/mod.rs @@ -1,2 +1,3 @@ #![allow(dead_code)] +pub mod ship; pub mod system; diff --git a/src/content/syntax/ship.rs b/src/content/syntax/ship.rs new file mode 100644 index 0000000..4782c3e --- /dev/null +++ b/src/content/syntax/ship.rs @@ -0,0 +1,60 @@ +use anyhow::Result; +use cgmath::Point2; + +/// Toml file syntax +pub(in crate::content) mod toml { + use serde::Deserialize; + + #[derive(Debug, Deserialize)] + pub struct ShipRoot { + pub ship: Ship, + } + + #[derive(Debug, Deserialize)] + pub struct Ship { + pub name: String, + pub sprite: String, + pub size: f32, + pub engines: Vec, + } + + #[derive(Debug, Deserialize)] + pub struct Engine { + pub x: f32, + pub y: f32, + pub size: f32, + } +} + +#[derive(Debug, Clone)] +pub struct Ship { + pub name: String, + pub sprite: String, + pub size: f32, + pub engines: Vec, +} + +#[derive(Debug, Clone)] +pub struct Engine { + pub pos: Point2, + pub size: f32, +} + +impl Ship { + pub fn parse(value: toml::ShipRoot) -> Result { + return Ok(Self { + name: value.ship.name, + sprite: value.ship.sprite, + size: value.ship.size, + engines: value + .ship + .engines + .iter() + .map(|e| Engine { + pos: Point2 { x: e.x, y: e.y }, + size: e.size, + }) + .collect(), + }); + } +}