rpgx_wasm/map/
tile.rs

1use rpgx::prelude::Tile;
2use wasm_bindgen::prelude::*;
3
4use crate::prelude::{WasmCoordinates, WasmDelta, WasmEffect, WasmRect};
5
6#[wasm_bindgen(js_name = Tile)]
7#[derive(Clone, Debug, PartialEq, Eq)]
8pub struct WasmTile {
9    inner: Tile,
10}
11
12#[wasm_bindgen(js_class = Tile)]
13impl WasmTile {
14    /// Create a new tile from an effect and area
15    #[wasm_bindgen(constructor)]
16    pub fn new(effect: &WasmEffect, area: &WasmRect) -> WasmTile {
17        WasmTile {
18            inner: Tile::new(effect.inner().clone(), area.inner().clone()),
19        }
20    }
21
22    /// Create a tile with default effect from area
23    #[wasm_bindgen(js_name = fromArea)]
24    pub fn from_area(area: &WasmRect) -> WasmTile {
25        WasmTile {
26            inner: Tile::from_area(area.inner().clone()),
27        }
28    }
29
30    /// Get the effect of this tile
31    #[wasm_bindgen(getter)]
32    pub fn effect(&self) -> WasmEffect {
33        WasmEffect::from_inner(self.inner.effect.clone())
34    }
35
36    /// Set the effect of this tile
37    #[wasm_bindgen(setter, js_name = setEffect)]
38    pub fn set_effect(&mut self, effect: &WasmEffect) {
39        self.inner.effect = effect.inner().clone();
40    }
41
42    /// Get the area covered by this tile
43    #[wasm_bindgen(getter)]
44    pub fn area(&self) -> WasmRect {
45        WasmRect::from_inner(self.inner.area.clone())
46    }
47
48    /// Set the area covered by this tile
49    #[wasm_bindgen(setter, js_name=setArea)]
50    pub fn set_area(&mut self, area: &WasmRect) {
51        self.inner.area = area.inner().clone();
52    }
53
54    /// Apply a new effect to this tile (overwrites existing)
55    #[wasm_bindgen]
56    pub fn apply(&mut self, effect: &WasmEffect) {
57        self.inner.apply(effect.inner().clone());
58    }
59
60    /// Returns true if tile blocks movement or interaction at the given coordinate
61    #[wasm_bindgen(js_name = isBlockingAt)]
62    pub fn is_blocking_at(&self, target: &WasmCoordinates) -> bool {
63        self.inner.is_blocking_at(target.inner().clone())
64    }
65
66    /// Offsets the tile’s area and effect’s blocking region by the given delta
67    #[wasm_bindgen]
68    pub fn offset(&mut self, delta: &WasmDelta) {
69        self.inner.offset(delta.inner().clone());
70    }
71
72    /// Translate the tile by a delta (alias for offset)
73    #[wasm_bindgen]
74    pub fn translate(&mut self, delta: &WasmDelta) {
75        self.inner.translate(delta.inner().clone());
76    }
77
78    /// Returns true if the tile’s area contains the specified coordinate
79    #[wasm_bindgen]
80    pub fn contains(&self, coord: &WasmCoordinates) -> bool {
81        self.inner.contains(coord.inner().clone())
82    }
83
84    /// Display the tile as a string
85    #[wasm_bindgen(js_name = toString)]
86    pub fn to_string(&self) -> String {
87        format!("{}", self.inner)
88    }
89}
90
91// Internal Rust API
92impl WasmTile {
93    pub fn from_inner(inner: Tile) -> Self {
94        WasmTile { inner }
95    }
96
97    pub fn inner(&self) -> &Tile {
98        &self.inner
99    }
100
101    pub fn into_inner(self) -> Tile {
102        self.inner
103    }
104}