screeps/objects/impls/
structure.rs1use js_sys::JsString;
2use wasm_bindgen::prelude::*;
3
4use crate::{
5 constants::{ErrorCode, StructureType},
6 objects::RoomObject,
7 prelude::*,
8};
9
10#[wasm_bindgen]
11extern "C" {
12 #[wasm_bindgen(extends = RoomObject)]
17 #[derive(Clone, Debug)]
18 pub type Structure;
19
20 #[wasm_bindgen(method, getter = hits)]
21 fn hits_internal(this: &Structure) -> Option<u32>;
22
23 #[wasm_bindgen(method, getter = hitsMax)]
24 fn hits_max_internal(this: &Structure) -> Option<u32>;
25
26 #[wasm_bindgen(method, getter = id)]
31 fn id_internal(this: &Structure) -> JsString;
32
33 #[wasm_bindgen(method, getter = structureType)]
37 pub fn structure_type(this: &Structure) -> StructureType;
38
39 #[wasm_bindgen(method)]
43 pub fn destroy(this: &Structure) -> i8;
44
45 #[wasm_bindgen(method, js_name = isActive)]
49 pub fn is_active(this: &Structure) -> bool;
50
51 #[wasm_bindgen(method, js_name = notifyWhenAttacked)]
56 pub fn notify_when_attacked(this: &Structure, val: bool) -> i8;
57}
58
59impl Structure {
60 pub fn hits(&self) -> u32 {
66 self.hits_internal().unwrap_or(0)
67 }
68
69 pub fn hits_max(&self) -> u32 {
75 self.hits_max_internal().unwrap_or(0)
76 }
77}
78
79impl<T> HasId for T
80where
81 T: AsRef<Structure> + JsCast,
82{
83 fn js_raw_id(&self) -> JsString {
84 Structure::id_internal(self.as_ref())
85 }
86}
87
88impl<T> HasHits for T
89where
90 T: AsRef<Structure>,
91{
92 fn hits(&self) -> u32 {
93 Structure::hits(self.as_ref())
94 }
95
96 fn hits_max(&self) -> u32 {
97 Structure::hits_max(self.as_ref())
98 }
99}
100
101impl<T> StructureProperties for T
102where
103 T: AsRef<Structure>,
104{
105 fn structure_type(&self) -> StructureType {
106 Structure::structure_type(self.as_ref())
107 }
108
109 fn destroy(&self) -> Result<(), ErrorCode> {
110 ErrorCode::result_from_i8(Structure::destroy(self.as_ref()))
111 }
112
113 fn is_active(&self) -> bool {
114 Structure::is_active(self.as_ref())
115 }
116
117 fn notify_when_attacked(&self, val: bool) -> Result<(), ErrorCode> {
118 ErrorCode::result_from_i8(Structure::notify_when_attacked(self.as_ref(), val))
119 }
120}