aboutsummaryrefslogtreecommitdiff
path: root/games/rstnode/rst-client/src/state.rs
blob: b1b5e89f3d131b29ab3dc334463178da14b1e1aa (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! Game client state handling

use crate::{
    assets::Assets,
    graphics::{
        entities::{Coordinates, NodeRndr},
        Renderer,
    },
    input::InputHandle,
    GameSettings,
};
use ggez::{event::EventHandler, graphics, Context, GameResult};
use rst_core::data::{Color, Level, Node, Owner, Player, Upgrade};
use std::sync::Arc;

pub struct ClientState {
    assets: Assets,
    settings: GameSettings,
    input: InputHandle,

    // Game state
    node: NodeRndr,
}

impl ClientState {
    pub fn new(settings: GameSettings, assets: Assets) -> Self {
        Self {
            assets,
            settings,
            input: InputHandle::new(),
            node: NodeRndr {
                loc: Coordinates(512.0, 512.0),
                inner: Arc::new(Node {
                    id: 0,
                    health: 100.into(),
                    max_health: 100.into(),
                    owner: Owner::Player(Player {
                        id: 0,
                        name: "kookie".into(),
                        color: Color::blue(),
                        money: 1000.into(),
                    }),
                    type_: Upgrade::Relay(Level::One),
                    links: 0,
                    link_states: vec![],
                    buffer: vec![],
                }),
            },
        }
    }

    pub fn assets(&self) -> &Assets {
        &self.assets
    }
}

impl EventHandler for ClientState {
    fn update(&mut self, ctx: &mut Context) -> GameResult<()> {
        self.input.update(ctx)?;
        Ok(())
    }

    fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
        graphics::clear(ctx, graphics::Color::from_rgb(15, 15, 15));

        // Render the node
        self.node.draw(&self, ctx).unwrap();

        graphics::present(ctx)
    }
}