rpgx_wasm/map/
layer.rs

1use rpgx::prelude::Layer;
2use wasm_bindgen::prelude::*;
3
4use crate::prelude::{WasmCoordinates, WasmDelta, WasmMask, WasmShape, WasmTile};
5
6#[wasm_bindgen(js_name = Layer)]
7pub struct WasmLayer {
8    inner: Layer,
9}
10
11#[wasm_bindgen(js_class = Layer)]
12impl WasmLayer {
13    #[wasm_bindgen(constructor)]
14    pub fn new(name: String, masks: Vec<WasmMask>, z: u32) -> WasmLayer {
15        let inner_masks = masks.into_iter().map(|m| m.into_inner()).collect();
16        WasmLayer {
17            inner: Layer::new(name, inner_masks, z),
18        }
19    }
20
21    #[wasm_bindgen(getter)]
22    pub fn name(&self) -> String {
23        self.inner.name.clone()
24    }
25
26    #[wasm_bindgen(getter)]
27    pub fn z(&self) -> u32 {
28        self.inner.z
29    }
30
31    #[wasm_bindgen(getter)]
32    pub fn masks(&self) -> Vec<WasmMask> {
33        self.inner
34            .masks
35            .iter()
36            .cloned()
37            .map(WasmMask::from_inner)
38            .collect()
39    }
40
41    /// Returns the first tile at the given coordinate or null if none.
42    #[wasm_bindgen(js_name = getTileAt)]
43    pub fn get_tile_at(&self, coord: &WasmCoordinates) -> Option<WasmTile> {
44        self.inner
45            .get_tile_at(*coord.inner())
46            .map(WasmTile::from_inner)
47    }
48
49    /// Returns true if any tile blocks movement at the coordinate.
50    #[wasm_bindgen(js_name = isBlockingAt)]
51    pub fn is_blocking_at(&self, coord: &WasmCoordinates) -> bool {
52        self.inner.is_blocking_at(coord.inner())
53    }
54
55    /// Returns shapes of all masks.
56    #[wasm_bindgen(js_name = getShapes)]
57    pub fn get_shapes(&self) -> Vec<WasmShape> {
58        self.inner
59            .get_shapes()
60            .into_iter()
61            .map(WasmShape::from_inner)
62            .collect()
63    }
64
65    /// Returns overall bounding shape of the layer.
66    #[wasm_bindgen(js_name = getShape)]
67    pub fn get_shape(&self) -> WasmShape {
68        WasmShape::from_inner(self.inner.get_shape())
69    }
70
71    /// Returns all tiles flattened.
72    #[wasm_bindgen]
73    pub fn render(&self) -> Vec<WasmTile> {
74        self.inner
75            .render()
76            .into_iter()
77            .map(WasmTile::from_inner)
78            .collect()
79    }
80
81    /// Offset all tiles by delta.
82    #[wasm_bindgen]
83    pub fn offset(&mut self, delta: &WasmDelta) {
84        self.inner.offset(*delta.inner());
85    }
86}
87
88impl WasmLayer {
89    /// Get a reference to the inner Layer
90    pub fn inner(&self) -> &Layer {
91        &self.inner
92    }
93
94    /// Consume WasmLayer and return the inner Layer
95    pub fn into_inner(self) -> Layer {
96        self.inner
97    }
98
99    /// Create WasmLayer from inner Layer directly
100    pub fn from_inner(inner: Layer) -> WasmLayer {
101        WasmLayer { inner }
102    }
103}