1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use ahash::AHashSet;
use bevy::utils::HashSet;
use de_types::objects::UnitType;
use glam::Vec2;
use serde::{Deserialize, Serialize};

pub struct Factory {
    products: AHashSet<UnitType>,
    position: Vec2,
}

impl Factory {
    pub fn products(&self) -> &AHashSet<UnitType> {
        &self.products
    }

    pub fn position(&self) -> Vec2 {
        self.position
    }
}

impl TryFrom<FactorySerde> for Factory {
    type Error = anyhow::Error;

    fn try_from(factory_serde: FactorySerde) -> Result<Self, Self::Error> {
        Ok(Self {
            products: AHashSet::from_iter(factory_serde.products),
            position: factory_serde.position.into(),
        })
    }
}

#[derive(Serialize, Deserialize)]
pub(crate) struct FactorySerde {
    products: HashSet<UnitType>,
    position: [f32; 2],
}