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
use js_sys::JsString;
use wasm_bindgen::prelude::*;

use crate::{
    constants::Color,
    objects::{RoomObject, RoomPosition},
    prelude::*,
};

#[wasm_bindgen]
extern "C" {
    /// A [`Flag`], which can be placed by the player or created automatically
    /// and are only visible to their owners. You can't create more than
    /// [`FLAGS_LIMIT`].
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Flag)
    ///
    /// [`FLAGS_LIMIT`]: crate::constants::FLAGS_LIMIT
    #[wasm_bindgen(extends = RoomObject)]
    #[derive(Clone, Debug)]
    pub type Flag;

    /// Primary color of the flag.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Flag.color)
    #[wasm_bindgen(method, getter)]
    pub fn color(this: &Flag) -> Color;

    /// A shortcut to `Memory.flags[flag.name]`.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Flag.memory)
    #[wasm_bindgen(method, getter)]
    pub fn memory(this: &Flag) -> JsValue;

    /// Sets a new value to `Memory.flags[flag.name]`.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Flag.memory)
    #[wasm_bindgen(method, setter)]
    pub fn set_memory(this: &Flag, val: &JsValue);

    /// The flag's name as a [`String`].
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Flag.name)
    #[wasm_bindgen(method, getter)]
    pub fn name(this: &Flag) -> String;

    /// The flag's name as a [`JsString`].
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Flag.name)
    #[wasm_bindgen(method, getter = name)]
    pub fn name_jsstring(this: &Flag) -> JsString;

    /// Remove the flag.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Flag.remove)
    #[wasm_bindgen(method)]
    pub fn remove(this: &Flag);

    /// Set the color (and optionally, the secondary color) of the flag.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Flag.setColor)
    #[wasm_bindgen(method, js_name = setColor)]
    pub fn set_color(this: &Flag, color: Color, secondary_color: Option<Color>);

    /// Set the position of the flag
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Flag.setPosition)
    #[wasm_bindgen(method, js_name = setPosition)]
    pub fn set_position(this: &Flag, pos: RoomPosition);
}

impl JsCollectionFromValue for Flag {
    fn from_value(val: JsValue) -> Self {
        val.unchecked_into()
    }
}