rpgx_wasm/engine/
scene.rs

1use crate::engine::pawn::WasmPawn;
2use crate::eucl::coordinates::WasmCoordinates;
3use crate::eucl::direction::WasmDirection;
4use crate::map::WasmMap;
5use rpgx::prelude::Scene;
6use wasm_bindgen::prelude::*;
7
8#[wasm_bindgen(js_name = Scene)]
9pub struct WasmScene {
10    inner: Scene,
11}
12
13#[wasm_bindgen(js_class = Scene)]
14impl WasmScene {
15    #[wasm_bindgen(constructor)]
16    pub fn new(name: String, map: WasmMap, pawn: Option<WasmPawn>) -> WasmScene {
17        let inner = Scene::new(name, map.into_inner(), pawn.map(|p| p.into_inner()));
18        WasmScene { inner }
19    }
20
21    #[wasm_bindgen]
22    pub fn name(&self) -> String {
23        self.inner.name.clone()
24    }
25
26    #[wasm_bindgen(js_name = loadPawn)]
27    pub fn load_pawn(&mut self, texture_id: u32) {
28        self.inner.load_pawn(texture_id);
29    }
30
31    #[wasm_bindgen(js_name = loadPawnAt)]
32    pub fn load_pawn_at(&mut self, pawn: WasmPawn) {
33        self.inner.load_pawn_at(pawn.into_inner());
34    }
35
36    #[wasm_bindgen(js_name = moveTo)]
37    pub fn move_to(&mut self, target: &WasmCoordinates) -> Result<WasmCoordinates, JsValue> {
38        self.inner
39            .move_to(target.clone().into_inner())
40            .map(WasmCoordinates::from_inner)
41            .map_err(|e| JsValue::from_str(&format!("{:?}", e)))
42    }
43
44    #[wasm_bindgen(js_name = stepTo)]
45    pub fn step_to(&mut self, direction: WasmDirection) -> Result<WasmCoordinates, JsValue> {
46        self.inner
47            .step_to(direction.into_inner())
48            .map(WasmCoordinates::from_inner)
49            .map_err(|e| JsValue::from_str(&format!("{:?}", e)))
50    }
51
52    #[wasm_bindgen(js_name = stepsTo)]
53    pub fn steps_to(&self, target: &WasmCoordinates) -> Result<js_sys::Array, JsValue> {
54        self.inner
55            .steps_to(target.clone().into_inner())
56            .map(|steps| {
57                steps
58                    .into_iter()
59                    .map(WasmCoordinates::from_inner)
60                    .map(JsValue::from)
61                    .collect()
62            })
63            .map_err(|e| JsValue::from_str(&format!("{:?}", e)))
64    }
65
66    #[wasm_bindgen(js_name = walkTo)]
67    pub async fn walk_to(&mut self, target: WasmCoordinates) -> Result<WasmCoordinates, JsValue> {
68        self.inner
69            .walk_to(target.into_inner())
70            .await
71            .map(WasmCoordinates::from_inner)
72            .map_err(|e| JsValue::from_str(&format!("{:?}", e)))
73    }
74
75    #[wasm_bindgen(js_name = getMap)]
76    pub fn get_map(&self) -> WasmMap {
77        WasmMap::from_inner(self.inner.map.clone())
78    }
79
80    #[wasm_bindgen(js_name = getPawn)]
81    pub fn get_pawn(&self) -> Option<WasmPawn> {
82        self.inner.pawn.clone().map(WasmPawn::from_inner)
83    }
84}
85
86impl WasmScene {
87    pub fn into_inner(self) -> Scene {
88        self.inner
89    }
90
91    pub fn from_inner(inner: Scene) -> WasmScene {
92        WasmScene { inner }
93    }
94}