85 lines
1.8 KiB
Rust
85 lines
1.8 KiB
Rust
|
use std::collections::HashMap;
|
||
|
|
||
|
use anyhow::{bail, Result};
|
||
|
|
||
|
use crate::{handle::TextureHandle, Content};
|
||
|
|
||
|
pub(crate) mod syntax {
|
||
|
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<Engine>,
|
||
|
pub steering: Option<Steering>,
|
||
|
}
|
||
|
|
||
|
#[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<TextureHandle>,
|
||
|
}
|
||
|
|
||
|
impl crate::Build for Outfit {
|
||
|
type InputSyntax = HashMap<String, syntax::Outfit>;
|
||
|
|
||
|
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,
|
||
|
};
|
||
|
|
||
|
// 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(());
|
||
|
}
|
||
|
}
|