screeps/objects/impls/
structure.rs1use js_sys::JsString;
2use wasm_bindgen::prelude::*;
3
4use crate::{
5 constants::StructureType, enums::action_error_codes::structure::*, objects::RoomObject,
6 prelude::*,
7};
8
9#[wasm_bindgen]
10extern "C" {
11 #[wasm_bindgen(extends = RoomObject)]
16 #[derive(Clone, Debug)]
17 pub type Structure;
18
19 #[wasm_bindgen(method, getter = hits)]
20 fn hits_internal(this: &Structure) -> Option<u32>;
21
22 #[wasm_bindgen(method, getter = hitsMax)]
23 fn hits_max_internal(this: &Structure) -> Option<u32>;
24
25 #[wasm_bindgen(method, getter = id)]
30 fn id_internal(this: &Structure) -> JsString;
31
32 #[wasm_bindgen(method, getter = structureType)]
36 pub fn structure_type(this: &Structure) -> StructureType;
37
38 #[wasm_bindgen(method, js_name = destroy)]
39 fn destroy_internal(this: &Structure) -> i8;
40
41 #[wasm_bindgen(method, js_name = isActive)]
45 pub fn is_active(this: &Structure) -> bool;
46
47 #[wasm_bindgen(method, js_name = notifyWhenAttacked)]
48 fn notify_when_attacked_internal(this: &Structure, val: bool) -> i8;
49}
50
51impl Structure {
52 pub fn hits(&self) -> u32 {
58 self.hits_internal().unwrap_or(0)
59 }
60
61 pub fn hits_max(&self) -> u32 {
67 self.hits_max_internal().unwrap_or(0)
68 }
69
70 pub fn destroy(&self) -> Result<(), DestroyErrorCode> {
74 DestroyErrorCode::result_from_i8(self.destroy_internal())
75 }
76
77 pub fn notify_when_attacked(
82 &self,
83 val: bool,
84 ) -> Result<(), StructureNotifyWhenAttackedErrorCode> {
85 StructureNotifyWhenAttackedErrorCode::result_from_i8(
86 self.notify_when_attacked_internal(val),
87 )
88 }
89}
90
91impl<T> HasId for T
92where
93 T: AsRef<Structure> + JsCast,
94{
95 fn js_raw_id(&self) -> JsString {
96 Structure::id_internal(self.as_ref())
97 }
98}
99
100impl<T> HasHits for T
101where
102 T: AsRef<Structure>,
103{
104 fn hits(&self) -> u32 {
105 Structure::hits(self.as_ref())
106 }
107
108 fn hits_max(&self) -> u32 {
109 Structure::hits_max(self.as_ref())
110 }
111}
112
113impl<T> StructureProperties for T
114where
115 T: AsRef<Structure>,
116{
117 fn structure_type(&self) -> StructureType {
118 Structure::structure_type(self.as_ref())
119 }
120
121 fn destroy(&self) -> Result<(), DestroyErrorCode> {
122 Structure::destroy(self.as_ref())
123 }
124
125 fn is_active(&self) -> bool {
126 Structure::is_active(self.as_ref())
127 }
128
129 fn notify_when_attacked(&self, val: bool) -> Result<(), StructureNotifyWhenAttackedErrorCode> {
130 Structure::notify_when_attacked(self.as_ref(), val)
131 }
132}