use std::collections::HashMap; use anyhow::{bail, Result}; use crate::{handle::TextureHandle, Content}; use super::OutfitSpace; pub(crate) mod syntax { use super::super::shared; use serde::Deserialize; // Raw serde syntax structs. // These are never seen by code outside this crate. #[derive(Debug, Deserialize)] pub struct Outfit { pub engine: Option, pub steering: Option, pub space: shared::syntax::OutfitSpace, } #[derive(Debug, Deserialize)] pub struct Engine { pub thrust: f32, pub flare_texture: String, } #[derive(Debug, Deserialize)] pub struct Steering { pub power: f32, } } /// Represents an outfit that may be attached to a ship. #[derive(Debug, Clone)] pub struct Outfit { /// The name of this outfit pub name: String, /// How much engine thrust this outfit produces pub engine_thrust: f32, /// How much steering power this outfit provids pub steer_power: f32, /// The engine flare sprite this outfit creates. /// Its location and size is determined by a ship's /// engine points. pub engine_flare_texture: Option, /// How much space this outfit requires pub space: OutfitSpace, } impl crate::Build for Outfit { type InputSyntax = HashMap; fn build(outfits: Self::InputSyntax, ct: &mut Content) -> Result<()> { for (outfit_name, outfit) in outfits { let mut o = Self { name: outfit_name.clone(), engine_thrust: 0.0, steer_power: 0.0, engine_flare_texture: None, space: OutfitSpace::from(outfit.space), }; // Engine stats if let Some(engine) = outfit.engine { let th = match ct.texture_index.get(&engine.flare_texture) { None => bail!( "In outfit `{}`: texture `{}` doesn't exist", outfit_name, engine.flare_texture ), Some(t) => *t, }; o.engine_thrust = engine.thrust; o.engine_flare_texture = Some(th); } // Steering stats if let Some(steer) = outfit.steering { o.steer_power = steer.power; } ct.outfits.push(o); } return Ok(()); } }