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