use anyhow::Result; use cgmath::Point2; pub(super) mod syntax { use serde::Deserialize; // Raw serde syntax structs. // These are never seen by code outside this crate. #[derive(Debug, Deserialize)] pub struct Ship { pub sprite: String, pub size: f32, pub engines: Vec, pub guns: Vec, } #[derive(Debug, Deserialize)] pub struct Engine { pub x: f32, pub y: f32, pub size: f32, } #[derive(Debug, Deserialize)] pub struct Gun { pub x: f32, pub y: f32, } } // Processed data structs. // These are exported. #[derive(Debug, Clone)] pub struct Ship { pub name: String, pub sprite: String, pub size: f32, pub engines: Vec, pub guns: Vec, } #[derive(Debug, Clone)] pub struct Engine { pub pos: Point2, pub size: f32, } #[derive(Debug, Clone)] pub struct ShipGun { pub pos: Point2, pub cooldown: f32, pub active_cooldown: f32, } impl super::Build for Ship { fn build(root: &super::syntax::Root) -> Result> { let ship = if let Some(ship) = &root.ship { ship } else { return Ok(vec![]); }; let mut out = Vec::new(); for (ship_name, ship) in ship { out.push(Self { name: ship_name.to_owned(), sprite: ship.sprite.to_owned(), size: ship.size, engines: ship .engines .iter() .map(|e| Engine { pos: Point2 { x: e.x, y: e.y }, size: e.size, }) .collect(), guns: ship .guns .iter() .map(|e| ShipGun { pos: Point2 { x: e.x, y: e.y }, cooldown: 0.2, active_cooldown: 0.0, }) .collect(), }); } return Ok(out); } }