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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use ahash::AHashMap;
use anyhow::Context;
use bevy::{
    asset::{io::Reader, AssetLoader, AsyncReadExt, LoadContext},
    ecs::system::SystemParam,
    prelude::*,
    reflect::TypePath,
    utils::BoxedFuture,
};
use de_core::state::AppState;
use de_types::objects::ObjectType;
use iyes_progress::prelude::*;
use serde::{Deserialize, Serialize};

use crate::{
    cannon::{LaserCannon, LaserCannonSerde},
    collection::AssetCollectionLoader,
    collider::{ColliderSerde, ObjectCollider},
    factory::{Factory, FactorySerde},
    flight::{Flight, FlightSerde},
    ichnography::{FootprintSerde, Ichnography},
    AssetCollection,
};

const OBJECT_EXTENSION: [&str; 1] = ["obj.json"];

pub(crate) struct SolidsPlugin;

impl Plugin for SolidsPlugin {
    fn build(&self, app: &mut App) {
        app.init_asset::<SolidObject>()
            .register_asset_loader(SolidObjectLoader)
            .add_systems(OnEnter(AppState::AppLoading), setup)
            .add_systems(
                Update,
                check_status
                    .track_progress()
                    .run_if(in_state(AppState::AppLoading)),
            );
    }
}

#[derive(Resource)]
pub(crate) struct Solids(AHashMap<ObjectType, Handle<SolidObject>>);

impl AssetCollection for Solids {
    type Key = ObjectType;
    type Asset = SolidObject;

    fn get(&self, object_type: ObjectType) -> &Handle<SolidObject> {
        self.0.get(&object_type).unwrap()
    }
}

impl AssetCollectionLoader for Solids {
    const DIRECTORY: &'static str = "objects";

    const SUFFIX: &'static str = OBJECT_EXTENSION[0];

    fn new(map: AHashMap<Self::Key, Handle<Self::Asset>>) -> Self {
        Self(map)
    }

    fn label() -> Option<String> {
        None
    }
}

#[derive(Asset, TypePath)]
pub struct SolidObject {
    ichnography: Ichnography,
    collider: ObjectCollider,
    cannon: Option<LaserCannon>,
    flight: Option<Flight>,
    factory: Option<Factory>,
}

impl SolidObject {
    pub fn cannon(&self) -> Option<&LaserCannon> {
        self.cannon.as_ref()
    }

    /// Flight configuration configuration. It is None for objects which cannot
    /// fly.
    pub fn flight(&self) -> Option<&Flight> {
        self.flight.as_ref()
    }

    /// Returns None if the object has no manufacturing capabilities, otherwise
    /// it returns info about object manufacturing capabilities.
    pub fn factory(&self) -> Option<&Factory> {
        self.factory.as_ref()
    }

    pub fn ichnography(&self) -> &Ichnography {
        &self.ichnography
    }

    pub fn collider(&self) -> &ObjectCollider {
        &self.collider
    }
}

impl TryFrom<SolidObjectSerde> for SolidObject {
    type Error = anyhow::Error;

    fn try_from(solid_serde: SolidObjectSerde) -> Result<Self, Self::Error> {
        Ok(Self {
            ichnography: Ichnography::try_from(solid_serde.footprint)?,
            collider: ObjectCollider::try_from(solid_serde.shape)?,
            cannon: solid_serde.cannon.map(LaserCannon::try_from).transpose()?,
            flight: solid_serde.flight.map(Flight::try_from).transpose()?,
            factory: solid_serde.factory.map(Factory::try_from).transpose()?,
        })
    }
}

#[derive(Serialize, Deserialize)]
struct SolidObjectSerde {
    footprint: FootprintSerde,
    shape: ColliderSerde,
    cannon: Option<LaserCannonSerde>,
    flight: Option<FlightSerde>,
    factory: Option<FactorySerde>,
}

struct SolidObjectLoader;

impl AssetLoader for SolidObjectLoader {
    type Asset = SolidObject;
    type Settings = ();
    type Error = anyhow::Error;

    fn load<'a>(
        &'a self,
        reader: &'a mut Reader,
        _settings: &'a Self::Settings,
        _load_context: &'a mut LoadContext,
    ) -> BoxedFuture<'a, anyhow::Result<Self::Asset>> {
        Box::pin(async move {
            let mut bytes = Vec::new();
            reader.read_to_end(&mut bytes).await?;
            let solid_serde: SolidObjectSerde =
                serde_json::from_slice(&bytes).context("Failed to parse object JSON")?;
            SolidObject::try_from(solid_serde)
        })
    }

    fn extensions(&self) -> &[&str] {
        OBJECT_EXTENSION.as_slice()
    }
}

#[derive(SystemParam)]
pub struct SolidObjects<'w> {
    solids: Res<'w, Solids>,
    assets: Res<'w, Assets<SolidObject>>,
}

impl<'w> SolidObjects<'w> {
    pub fn get(&self, object_type: ObjectType) -> &SolidObject {
        self.assets.get(self.solids.get(object_type)).unwrap()
    }
}

fn setup(mut commands: Commands, server: Res<AssetServer>) {
    commands.insert_resource(Solids::init(server.as_ref()));
}

fn check_status(server: Res<AssetServer>, solids: Res<Solids>) -> Progress {
    solids.progress(server.as_ref())
}