aboutsummaryrefslogtreecommitdiff
path: root/games/rstnode/rst-client/src/graphics/entities/mod.rs
blob: a3289f6f412df69edebd47f35e3adc1f2a44cc6a (plain)
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
//! Game entity rendering
//!
//! Generally the naming convention should be: `{type}Rndr`
//! (`Renderer`, but shorter).

use crate::color;
use super::prelude::*;

use rst_core::data::{Owner, Node, Color};
use std::sync::Arc;

/// A set of universal X/Y coordinates
pub struct Coordinates(pub f32, pub f32);

impl<'a> From<&'a Coordinates> for Point2<f32> {
    fn from(c: &'a Coordinates) -> Self {
        Point2 { x: c.0, y: c.1 }
    }
}

pub struct NodeRndr {
    pub loc: Coordinates,
    pub inner: Arc<Node>,
}

impl Renderer for NodeRndr {
    fn update(&mut self, _: &mut ClientState, _: &mut Context) -> GameResult<()> {
        Ok(())
    }

    fn draw(&self, s: &ClientState, ctx: &mut Context) -> GameResult<()> {
        let frame = s.assets().find("frame/frame_s").unwrap();
        let icon = s.assets().find("relay/relay1").unwrap();

        let x = self.loc.0 - frame.width() as f32;
        let y = self.loc.1 - frame.height() as f32;

        let color = match self.inner.owner {
            Owner::Player(ref p) => color::to(p.color),
            Owner::Neutral => color::to(Color::white()),
        };
        
        frame.draw(ctx, DrawParam::new().dest([x, y]).color(color))?;
        icon.draw(ctx, DrawParam::new().dest([x, y]).color(color))?;

        Ok(())
    }
}