screeps/objects/impls/
flag.rs

1use js_sys::JsString;
2use wasm_bindgen::prelude::*;
3
4use crate::{
5    constants::Color,
6    enums::action_error_codes::flag::*,
7    objects::{RoomObject, RoomPosition},
8    prelude::*,
9};
10
11#[wasm_bindgen]
12extern "C" {
13    /// A [`Flag`], which can be placed by the player or created automatically
14    /// and are only visible to their owners. You can't create more than
15    /// [`FLAGS_LIMIT`].
16    ///
17    /// [Screeps documentation](https://docs.screeps.com/api/#Flag)
18    ///
19    /// [`FLAGS_LIMIT`]: crate::constants::FLAGS_LIMIT
20    #[wasm_bindgen(extends = RoomObject)]
21    #[derive(Clone, Debug)]
22    pub type Flag;
23
24    /// Primary color of the flag.
25    ///
26    /// [Screeps documentation](https://docs.screeps.com/api/#Flag.color)
27    #[wasm_bindgen(method, getter)]
28    pub fn color(this: &Flag) -> Color;
29
30    /// A shortcut to `Memory.flags[flag.name]`.
31    ///
32    /// [Screeps documentation](https://docs.screeps.com/api/#Flag.memory)
33    #[wasm_bindgen(method, getter)]
34    pub fn memory(this: &Flag) -> JsValue;
35
36    /// Sets a new value to `Memory.flags[flag.name]`.
37    ///
38    /// [Screeps documentation](https://docs.screeps.com/api/#Flag.memory)
39    #[wasm_bindgen(method, setter)]
40    pub fn set_memory(this: &Flag, val: &JsValue);
41
42    /// The flag's name as a [`String`].
43    ///
44    /// [Screeps documentation](https://docs.screeps.com/api/#Flag.name)
45    #[wasm_bindgen(method, getter)]
46    pub fn name(this: &Flag) -> String;
47
48    /// The flag's name as a [`JsString`].
49    ///
50    /// [Screeps documentation](https://docs.screeps.com/api/#Flag.name)
51    #[wasm_bindgen(method, getter = name)]
52    pub fn name_jsstring(this: &Flag) -> JsString;
53
54    #[wasm_bindgen(method, js_name = remove)]
55    fn remove_internal(this: &Flag) -> i8;
56
57    #[wasm_bindgen(method, js_name = setColor)]
58    fn set_color_internal(this: &Flag, color: Color, secondary_color: Option<Color>) -> i8;
59
60    #[wasm_bindgen(method, js_name = setPosition)]
61    fn set_position_internal(this: &Flag, pos: RoomPosition) -> i8;
62}
63
64impl Flag {
65    /// Remove the flag.
66    ///
67    /// [Screeps documentation](https://docs.screeps.com/api/#Flag.remove)
68    pub fn remove(&self) -> Result<(), FlagRemoveErrorCode> {
69        FlagRemoveErrorCode::result_from_i8(self.remove_internal())
70    }
71
72    /// Set the color (and optionally, the secondary color) of the flag.
73    ///
74    /// [Screeps documentation](https://docs.screeps.com/api/#Flag.setColor)
75    pub fn set_color(
76        &self,
77        color: Color,
78        secondary_color: Option<Color>,
79    ) -> Result<(), SetColorErrorCode> {
80        SetColorErrorCode::result_from_i8(self.set_color_internal(color, secondary_color))
81    }
82
83    /// Set the position of the flag
84    ///
85    /// [Screeps documentation](https://docs.screeps.com/api/#Flag.setPosition)
86    pub fn set_position(&self, pos: RoomPosition) -> Result<(), SetPositionErrorCode> {
87        SetPositionErrorCode::result_from_i8(self.set_position_internal(pos))
88    }
89}
90
91impl JsCollectionFromValue for Flag {
92    fn from_value(val: JsValue) -> Self {
93        val.unchecked_into()
94    }
95}