screeps/objects/impls/
structure_tower.rs

1use wasm_bindgen::prelude::*;
2
3use crate::{
4    enums::action_error_codes::structure_tower::*,
5    objects::{OwnedStructure, RoomObject, Store, Structure},
6    prelude::*,
7};
8
9#[wasm_bindgen]
10extern "C" {
11    /// An object representing a [`StructureTower`], which can heal, repair, or
12    /// attack anywhere in the room.
13    ///
14    /// [Screeps documentation](https://docs.screeps.com/api/#StructureTower)
15    #[wasm_bindgen(extends = RoomObject, extends = Structure, extends = OwnedStructure)]
16    #[derive(Clone, Debug)]
17    pub type StructureTower;
18
19    /// The [`Store`] of the tower, which contains energy which is consumed when
20    /// it takes actions.
21    ///
22    /// [Screeps documentation](https://docs.screeps.com/api/#StructureTower.store)
23    #[wasm_bindgen(method, getter)]
24    pub fn store(this: &StructureTower) -> Store;
25
26    #[wasm_bindgen(method, js_name = attack)]
27    fn attack_internal(this: &StructureTower, target: &RoomObject) -> i8;
28
29    #[wasm_bindgen(method, js_name = heal)]
30    fn heal_internal(this: &StructureTower, target: &RoomObject) -> i8;
31
32    #[wasm_bindgen(method, js_name = repair)]
33    fn repair_internal(this: &StructureTower, target: &Structure) -> i8;
34}
35
36impl StructureTower {
37    /// Attack a [`Creep`], [`PowerCreep`], or [`Structure`] in the room,
38    /// dealing damage depending on range.
39    ///
40    /// [Screeps documentation](https://docs.screeps.com/api/#StructureTower.attack)
41    ///
42    /// [`Creep`]: crate::objects::Creep
43    /// [`PowerCreep`]: crate::objects::PowerCreep
44    pub fn attack<T>(&self, target: &T) -> Result<(), TowerAttackErrorCode>
45    where
46        T: ?Sized + Attackable,
47    {
48        TowerAttackErrorCode::result_from_i8(self.attack_internal(target.as_ref()))
49    }
50
51    /// Heal a [`Creep`] or [`PowerCreep`] in the room, adding hit points
52    /// depending on range.
53    ///
54    /// [Screeps documentation](https://docs.screeps.com/api/#StructureTower.heal)
55    ///
56    /// [`Creep`]: crate::objects::Creep
57    /// [`PowerCreep`]: crate::objects::PowerCreep
58    pub fn heal<T>(&self, target: &T) -> Result<(), TowerHealErrorCode>
59    where
60        T: ?Sized + Healable,
61    {
62        TowerHealErrorCode::result_from_i8(self.heal_internal(target.as_ref()))
63    }
64
65    /// Repair a [`Structure`] in the room, adding hit points depending on
66    /// range.
67    ///
68    /// [Screeps documentation](https://docs.screeps.com/api/#StructureTower.repair)
69    pub fn repair<T>(&self, target: &T) -> Result<(), TowerRepairErrorCode>
70    where
71        T: ?Sized + Repairable,
72    {
73        TowerRepairErrorCode::result_from_i8(self.repair_internal(target.as_ref()))
74    }
75}
76
77impl HasStore for StructureTower {
78    fn store(&self) -> Store {
79        Self::store(self)
80    }
81}
82
83impl Attackable for StructureTower {}
84impl Dismantleable for StructureTower {}
85impl Repairable for StructureTower {}
86impl Transferable for StructureTower {}
87impl Withdrawable for StructureTower {}