screeps/objects/impls/
construction_site.rs

1use js_sys::JsString;
2use wasm_bindgen::prelude::*;
3
4use crate::{
5    constants::StructureType,
6    enums::action_error_codes::construction_site::*,
7    objects::{Owner, RoomObject},
8    prelude::*,
9};
10
11#[wasm_bindgen]
12extern "C" {
13    /// A [`ConstructionSite`] which is an object representing a structure under
14    /// construction.
15    ///
16    /// [Screeps documentation](https://docs.screeps.com/api/#ConstructionSite)
17    #[wasm_bindgen(extends = RoomObject)]
18    #[derive(Clone, Debug)]
19    pub type ConstructionSite;
20
21    #[wasm_bindgen(method, getter = id)]
22    fn id_internal(this: &ConstructionSite) -> Option<JsString>;
23
24    #[wasm_bindgen(method, getter = my)]
25    fn my_internal(this: &ConstructionSite) -> bool;
26
27    #[wasm_bindgen(method, getter = owner)]
28    fn owner_internal(this: &ConstructionSite) -> Owner;
29
30    #[wasm_bindgen(method, getter = progress)]
31    fn progress_internal(this: &ConstructionSite) -> u32;
32
33    #[wasm_bindgen(method, getter = progressTotal)]
34    fn progress_total_internal(this: &ConstructionSite) -> u32;
35
36    #[wasm_bindgen(method, getter = structureType)]
37    fn structure_type_internal(this: &ConstructionSite) -> StructureType;
38
39    #[wasm_bindgen(method, js_name = remove)]
40    fn remove_internal(this: &ConstructionSite) -> i8;
41}
42
43impl ConstructionSite {
44    /// Whether you own the [`ConstructionSite`].
45    ///
46    /// [Screeps documentation](https://docs.screeps.com/api/#ConstructionSite.my)
47    pub fn my(&self) -> bool {
48        self.my_internal()
49    }
50
51    /// The [`Owner`] of this construction site, which contains the owner's
52    /// username.
53    ///
54    /// [Screeps documentation](https://docs.screeps.com/api/#ConstructionSite.owner)
55    pub fn owner(&self) -> Owner {
56        self.owner_internal()
57    }
58
59    /// The current progress toward completion of the structure being built.
60    ///
61    /// [Screeps documentation](https://docs.screeps.com/api/#ConstructionSite.progress)
62    pub fn progress(&self) -> u32 {
63        self.progress_internal()
64    }
65
66    /// The total progess toward constuction progress needed for the structure
67    /// to be completed.
68    ///
69    /// [Screeps documentation](https://docs.screeps.com/api/#ConstructionSite.progressTotal)
70    pub fn progress_total(&self) -> u32 {
71        self.progress_total_internal()
72    }
73
74    /// The type of structure being constructed.
75    ///
76    /// [Screeps documentation](https://docs.screeps.com/api/#Structure.structureType)
77    pub fn structure_type(&self) -> StructureType {
78        self.structure_type_internal()
79    }
80
81    /// Remove the [`ConstructionSite`].
82    ///
83    /// [Screeps documentation](https://docs.screeps.com/api/#ConstructionSite.remove)
84    pub fn remove(&self) -> Result<(), ConstructionSiteRemoveErrorCode> {
85        ConstructionSiteRemoveErrorCode::result_from_i8(self.remove_internal())
86    }
87}
88
89impl MaybeHasId for ConstructionSite {
90    /// The Object ID of the [`ConstructionSite`], or `None` if it was created
91    /// this tick.
92    ///
93    /// [Screeps documentation](https://docs.screeps.com/api/#ConstructionSite.id)
94    fn try_js_raw_id(&self) -> Option<JsString> {
95        self.id_internal()
96    }
97}
98
99impl JsCollectionFromValue for ConstructionSite {
100    fn from_value(val: JsValue) -> Self {
101        val.unchecked_into()
102    }
103}