2023-12-27 19:51:58 -08:00
|
|
|
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<Engine>,
|
|
|
|
pub guns: Vec<Gun>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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,
|
2023-12-28 17:04:41 -08:00
|
|
|
pub engines: Vec<EnginePoint>,
|
|
|
|
pub guns: Vec<GunPoint>,
|
2023-12-27 19:51:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2023-12-28 17:04:41 -08:00
|
|
|
pub struct EnginePoint {
|
2023-12-27 19:51:58 -08:00
|
|
|
pub pos: Point2<f32>,
|
|
|
|
pub size: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2023-12-28 17:04:41 -08:00
|
|
|
pub struct GunPoint {
|
2023-12-27 19:51:58 -08:00
|
|
|
pub pos: Point2<f32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl super::Build for Ship {
|
|
|
|
fn build(root: &super::syntax::Root) -> Result<Vec<Self>> {
|
2023-12-27 20:13:39 -08:00
|
|
|
let ship = if let Some(ship) = &root.ship {
|
|
|
|
ship
|
|
|
|
} else {
|
|
|
|
return Ok(vec![]);
|
|
|
|
};
|
|
|
|
|
2023-12-27 19:51:58 -08:00
|
|
|
let mut out = Vec::new();
|
2023-12-27 20:13:39 -08:00
|
|
|
for (ship_name, ship) in ship {
|
2023-12-27 19:51:58 -08:00
|
|
|
out.push(Self {
|
|
|
|
name: ship_name.to_owned(),
|
|
|
|
sprite: ship.sprite.to_owned(),
|
|
|
|
size: ship.size,
|
|
|
|
engines: ship
|
|
|
|
.engines
|
|
|
|
.iter()
|
2023-12-28 17:04:41 -08:00
|
|
|
.map(|e| EnginePoint {
|
2023-12-27 19:51:58 -08:00
|
|
|
pos: Point2 { x: e.x, y: e.y },
|
|
|
|
size: e.size,
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
guns: ship
|
|
|
|
.guns
|
|
|
|
.iter()
|
2023-12-28 17:04:41 -08:00
|
|
|
.map(|e| GunPoint {
|
2023-12-27 19:51:58 -08:00
|
|
|
pos: Point2 { x: e.x, y: e.y },
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(out);
|
|
|
|
}
|
|
|
|
}
|