Skip to main content

minigame/
minigame.rs

1use rusterix::{prelude::*, rusterix::Rusterix};
2// use std::path::Path;
3use theframework::prelude::*;
4
5fn main() {
6    let game = MiniGame::new();
7    let app = TheApp::new();
8
9    () = app.run(Box::new(game));
10}
11
12// This example executes the minigame in the Rusterix game API.
13
14pub struct MiniGame {
15    rusterix: Rusterix,
16}
17
18impl TheTrait for MiniGame {
19    fn new() -> Self
20    where
21        Self: Sized,
22    {
23        let mut assets = Assets::default();
24        assets.collect_from_directory("minigame".to_string());
25        // assets.compile_source_maps();
26
27        let mut rusterix = Rusterix::default();
28        rusterix.set_assets(assets);
29        rusterix.create_regions();
30
31        let camera = Box::new(D3FirstPCamera::new());
32        rusterix.client.set_camera_d3(camera);
33
34        // if let Some(map) = rusterix.assets.get_map("world") {
35        //     // Build the 3D scene from the map meta data
36        //     rusterix
37        //         .client
38        //         .build_scene_d3(map, &rusterix.assets, &ValueContainer::default());
39        // }
40
41        // Add logo on top of the scene
42        rusterix.client.scene_d3.d2_static = vec![
43            Batch2D::from_rectangle(0.0, 0.0, 200.0, 200.0)
44                .receives_light(false)
45                .source(PixelSource::StaticTileIndex(0)),
46        ];
47        // rusterix
48        //     .client
49        //     .scene_d3
50        //     .textures
51        //     .push(Tile::from_texture(Texture::from_image(Path::new(
52        //         "images/logo.png",
53        //     ))));
54
55        Self { rusterix }
56    }
57
58    /// Draw the game.
59    fn draw(&mut self, pixels: &mut [u8], ctx: &mut TheContext) {
60        let _start = get_time();
61
62        // Update the entities on the server.
63        self.rusterix.server.update(&mut self.rusterix.assets);
64
65        if let Some(mut map) = self.rusterix.assets.get_map("world").cloned() {
66            self.rusterix.server.apply_entities_items(&mut map);
67            self.rusterix.build_entities_items_d3(&map);
68
69            self.rusterix
70                .draw_scene(&map, pixels, ctx.width, ctx.height);
71        }
72
73        let _stop = get_time();
74        println!("Execution time: {:?} ms.", _stop - _start);
75    }
76
77    // Set the target fps to 60
78    fn target_fps(&self) -> f64 {
79        60.0
80    }
81
82    // Query if the widget needs a redraw
83    fn update(&mut self, _ctx: &mut TheContext) -> bool {
84        true
85    }
86
87    fn window_title(&self) -> String {
88        "Rusterix Map Demo".to_string()
89    }
90
91    fn hover(&mut self, _x: f32, _y: f32, _ctx: &mut TheContext) -> bool {
92        // self.entity
93        //     .set_tilt_from_screen_coordinate(1.0 - y / ctx.height as f32);
94        true
95    }
96
97    fn key_down(
98        &mut self,
99        char: Option<char>,
100        _key: Option<TheKeyCode>,
101        _ctx: &mut TheContext,
102    ) -> bool {
103        if let Some(char) = char {
104            self.rusterix
105                .server
106                .local_player_event("key_down".into(), Value::Str(char.to_string()));
107        }
108        true
109    }
110
111    fn key_up(
112        &mut self,
113        char: Option<char>,
114        _key: Option<TheKeyCode>,
115        _ctx: &mut TheContext,
116    ) -> bool {
117        if let Some(char) = char {
118            self.rusterix
119                .server
120                .local_player_event("key_up".into(), Value::Str(char.to_string()));
121        }
122        true
123    }
124}
125
126fn get_time() -> u128 {
127    #[cfg(target_arch = "wasm32")]
128    {
129        web_sys::window().unwrap().performance().unwrap().now() as u128
130    }
131    #[cfg(not(target_arch = "wasm32"))]
132    {
133        let stop = std::time::SystemTime::now()
134            .duration_since(std::time::UNIX_EPOCH)
135            .expect("Time went backwards");
136        stop.as_millis()
137    }
138}