|
| 1 | +use super::{ |
| 2 | + CircleMovement, LineMovement, MovementStrategy, RandomMovement, Ship, StationaryMovement, |
| 3 | +}; |
1 | 4 | use serde::Deserialize; |
2 | 5 |
|
3 | 6 | #[derive(Debug, Deserialize)] |
4 | 7 | pub struct SimulationConfig { |
5 | 8 | pub emission_interval: u64, |
6 | | - pub ships: ShipsConfig, |
| 9 | + pub initial_ships: Vec<ShipConfig>, |
| 10 | + pub controller_bind_addr: String, |
7 | 11 | } |
8 | 12 |
|
9 | 13 | #[derive(Debug, Deserialize)] |
10 | | -pub struct ShipsConfig { |
11 | | - pub line: Vec<LineMovementConfig>, |
12 | | - pub circle: Vec<CircleMovementConfig>, |
13 | | - pub random: Vec<RandomMovementConfig>, |
14 | | - pub stationary: Vec<StationaryMovementConfig>, |
15 | | -} |
16 | | - |
17 | | -#[derive(Debug, Deserialize)] |
18 | | -pub struct LineMovementConfig { |
| 14 | +pub struct ShipConfig { |
19 | 15 | pub initial_position: (f64, f64), |
20 | | - pub angle: f64, |
21 | | - pub speed: f64, |
| 16 | + #[serde(flatten)] |
| 17 | + pub movement: MovementType, |
22 | 18 | } |
23 | 19 |
|
24 | 20 | #[derive(Debug, Deserialize)] |
25 | | -pub struct CircleMovementConfig { |
26 | | - pub initial_position: (f64, f64), |
27 | | - pub radius: f64, |
28 | | - pub speed: f64, |
| 21 | +#[serde(tag = "type", rename_all = "lowercase")] |
| 22 | +pub enum MovementType { |
| 23 | + Line { angle: f64, speed: f64 }, |
| 24 | + Circle { radius: f64, speed: f64 }, |
| 25 | + Random { max_speed: f64 }, |
| 26 | + Stationary, |
29 | 27 | } |
30 | 28 |
|
31 | | -#[derive(Debug, Deserialize)] |
32 | | -pub struct RandomMovementConfig { |
33 | | - pub initial_position: (f64, f64), |
34 | | - pub max_speed: f64, |
35 | | -} |
| 29 | +pub fn build_ship_from_config(id: u64, config: ShipConfig, emission_interval: u64) -> Ship { |
| 30 | + let movement_strategy: Box<dyn MovementStrategy> = match config.movement { |
| 31 | + MovementType::Line { angle, speed } => Box::new(LineMovement::new(angle, speed)), |
| 32 | + MovementType::Circle { radius, speed } => { |
| 33 | + Box::new(CircleMovement::new(radius, speed, emission_interval)) |
| 34 | + } |
| 35 | + MovementType::Random { max_speed } => Box::new(RandomMovement::new(max_speed)), |
| 36 | + MovementType::Stationary => Box::new(StationaryMovement {}), |
| 37 | + }; |
36 | 38 |
|
37 | | -#[derive(Debug, Deserialize)] |
38 | | -pub struct StationaryMovementConfig { |
39 | | - pub initial_position: (f64, f64), |
| 39 | + Ship { |
| 40 | + id, |
| 41 | + position: config.initial_position, |
| 42 | + emission_interval, |
| 43 | + movement_strategy, |
| 44 | + } |
40 | 45 | } |
0 commit comments