rpgx_wasm/map/
layer.rs

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