~starkingdoms/starkingdoms

ref: 4691fee626abf07691719c3a4a6fb90ad031d0b0 starkingdoms/client/src/rendering/renderer_playercentric.rs -rw-r--r-- 5.8 KiB
4691fee6TerraMaster85 core module bounding box fix 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use std::error::Error;
use async_trait::async_trait;
use log::debug;
use web_sys::{CanvasRenderingContext2d, HtmlCanvasElement, HtmlImageElement};
use crate::rendering::Renderer;
use wasm_bindgen::{JsCast, JsValue};
use crate::CLIENT;
use crate::textures::TextureManager;

pub const STARFIELD_RENDER_SCALE: f64 = 1.0;

#[derive(Debug)]
pub struct WebRenderer {
    canvas_element_id: String
}

#[async_trait]
impl Renderer for WebRenderer {
    async fn get(canvas_element_id: &str) -> Result<Self, Box<dyn Error>> {
        Ok(Self {
            canvas_element_id: canvas_element_id.to_string()
        })
    }

    async fn render_frame(&self, _time_delta_ms: f64) -> Result<(), Box<dyn Error>> {
        // TODO - terra is working on this, please no touchy without telling him
        // TODO - until this notice is removed
        // time_delta_ms is the delta, in ms, from when the last render_frame was called by the browser
        let window = web_sys::window().ok_or("window needs to exist")?;
        let document = window.document().ok_or("window.document needs to exist")?;
        let canvas_element = document.get_element_by_id(&self.canvas_element_id).ok_or("canvas element does not exist")?;
        let typed_canvas_element: HtmlCanvasElement = canvas_element.dyn_into::<web_sys::HtmlCanvasElement>().map_err(|_| ()).unwrap();
        let context = typed_canvas_element.get_context("2d").unwrap().unwrap().dyn_into::<CanvasRenderingContext2d>().unwrap();
        let client = CLIENT.read()?;
        if client.client_data.is_none() {
            return Err("client not yet initialized".into());
        }
        let client_data = client.client_data.as_ref().unwrap();

        //let camera_translate_x = -client.x + (typed_canvas_element.width() / 2) as f64;
        let viewer_size_x = typed_canvas_element.width() as f64;
        //let camera_translate_y = -client.y + (typed_canvas_element.height() / 2) as f64;
        let viewer_size_y = typed_canvas_element.height() as f64;

        typed_canvas_element.style().set_property("background-position", &format!("{}px {}px", -client.x / STARFIELD_RENDER_SCALE, -client.y / STARFIELD_RENDER_SCALE)).map_err(|e| e.as_string().unwrap())?;

        context.set_transform(1f64, 0f64, 0f64, 1f64, 0f64, 0f64).map_err(|e: JsValue| e.as_string().unwrap())?;
        context.clear_rect(0f64, 0f64, viewer_size_x, viewer_size_y);

        // *dont* translate the camera. we're movign everything else around us. cameracentrism.
        // only translation will be to center our core module.
        //context.translate(camera_translate_x, camera_translate_y).map_err(|e: JsValue| e.as_string().unwrap())?;
        context.translate(viewer_size_x / 2.0, viewer_size_y / 2.0).map_err(|e: JsValue| e.as_string().unwrap())?;

        for planet in &client.planets {
            //context.save();

            //context.set_transform(1f64, 0f64, 0f64, 1f64, 0f64, 0f64).map_err(|e: JsValue| e.as_string().unwrap())?;
            //context.translate(-planet.x, -planet.y).map_err(|e: JsValue| e.as_string().unwrap())?;

            let texture_image = document.get_element_by_id(&format!("tex-{}", planet.planet_type.as_texture_id())).unwrap().dyn_into::<HtmlImageElement>().unwrap();
            // pos:
            //debug!("P {} {}", planet.x - planet.radius - client.x, planet.y - planet.radius - client.y);
            debug!("P {} {}", planet.x - planet.radius - client.x, planet.y - planet.radius - client.y);
            context.draw_image_with_html_image_element_and_dw_and_dh(&texture_image, planet.x - planet.radius - client.x, planet.y - planet.radius - client.y, planet.radius * 2f64, planet.radius * 2f64).map_err(|e: JsValue| e.as_string().unwrap())?;

            //context.restore();
        }

        for player in &client.players {
            context.save();

            //context.translate(player.x, player.y).map_err(|e: JsValue| e.as_string().unwrap())?;
            //gaah fuck why noo i didnt want this. godforsaken canvas rotation
            context.translate(player.x - client.x, player.y - client.y).map_err(|e: JsValue| e.as_string().unwrap())?; // fwip

            context.set_text_align("center");
            context.set_font("30px Segoe UI");
            context.set_fill_style(&JsValue::from_str("white")); // CssStyleColor
            context.fill_text(&player.username, 0f64, -35f64).map_err(|e: JsValue| e.as_string().unwrap())?;

            context.rotate(player.rotation).map_err(|e: JsValue| e.as_string().unwrap())?; // fwip

            let texture_image = document.get_element_by_id("tex-hearty").unwrap().dyn_into::<HtmlImageElement>().unwrap();
            debug!("H {} {}", player.x - client.x, player.y - client.y);
            //context.draw_image_with_html_image_element_and_dw_and_dh(&texture_image, player.x - 25f64 - client.x, player.y - 25f64 - client.y, 50f64, 50f64).map_err(|e: JsValue| e.as_string().unwrap())?;
            context.draw_image_with_html_image_element_and_dw_and_dh(&texture_image, -25f64, -25f64, 50f64, 50f64).map_err(|e: JsValue| e.as_string().unwrap())?; // sktch

            //context.rotate(-player.rotation).map_err(|e: JsValue| e.as_string().unwrap())?; // fwoop

            context.restore();
        }

/*
        context.begin_path();

        // Draw the outer circle.
        context
            .arc(75.0, 75.0, 50.0, 0.0, std::f64::consts::PI * 2.0)
            .unwrap();

        // Draw the mouth.
        context.move_to(110.0, 75.0);
        context.arc(75.0, 75.0, 35.0, 0.0, std::f64::consts::PI).unwrap();

        // Draw the left eye.
        context.move_to(65.0, 65.0);
        context
            .arc(60.0, 65.0, 5.0, 0.0, std::f64::consts::PI * 2.0)
            .unwrap();

        // Draw the right eye.
        context.move_to(95.0, 65.0);
        context
            .arc(90.0, 65.0, 5.0, 0.0, std::f64::consts::PI * 2.0)
            .unwrap();

        context.stroke();
*/
        Ok(())
    }
}