Added basic ship config

master
Mark 2023-12-26 22:21:18 -08:00
parent 1bd78d3cfd
commit e5d399c3c5
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
6 changed files with 85 additions and 13 deletions

7
content/ship.toml Normal file
View File

@ -0,0 +1,7 @@
# content type: ship
[ship]
name = "Gypsum"
sprite = "ship::gypsum"
size = 100
engines = [{ x = 0.0, y = -105, size = 50.0 }]

View File

@ -6,28 +6,29 @@ use super::{syntax, ContentType};
#[derive(Debug)]
pub struct Content {
pub systems: Vec<syntax::system::System>,
pub ships: Vec<syntax::ship::Ship>,
}
impl Content {
pub fn new(cv: Vec<(PathBuf, ContentType)>) -> Result<Self> {
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)
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 });
}
}

View File

@ -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),
});
}

View File

@ -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};

View File

@ -1,2 +1,3 @@
#![allow(dead_code)]
pub mod ship;
pub mod system;

View File

@ -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<Engine>,
}
#[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<Engine>,
}
#[derive(Debug, Clone)]
pub struct Engine {
pub pos: Point2<f32>,
pub size: f32,
}
impl Ship {
pub fn parse(value: toml::ShipRoot) -> Result<Self> {
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(),
});
}
}