aboutsummaryrefslogtreecommitdiff
path: root/games/rstnode/rst-client/src/state/mod.rs
blob: 45e69eee10bb5387d141a3cfb71d2e1b1e9cd527 (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
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
123
124
125
126
127
128
129
//! Game client state handling

mod map;
pub use map::*;

mod if_impl;

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

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

    // Game state
    node1: NodeRndr,
    node2: NodeRndr,
    link: Arc<Link>,
}

impl ClientState {
    pub fn new(settings: GameSettings, assets: Assets) -> Self {
        let link = Arc::new(Link {
            id: 0,
            a: 0,
            b: 1,
            length: 12,
            pp: vec![],
            io: Io::default(),
        });

        Self {
            assets,
            settings,
            vp: Viewport::new(),
            input: InputHandle::new(),
            node1: 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: 1,
                    router: None,
                    link_states: vec![Arc::clone(&link)],
                    buffer: vec![].into(),
                }),
            },
            node2: NodeRndr {
                loc: Coordinates(128.0, 128.0),
                inner: Arc::new(Node {
                    id: 0,
                    health: 100.into(),
                    max_health: 100.into(),
                    owner: Owner::Neutral,
                    type_: Upgrade::Relay(Level::One),
                    links: 1,
                    router: None,
                    link_states: vec![Arc::clone(&link)],
                    buffer: vec![].into(),
                }),
            },
            link,
        }
    }

    pub fn viewport(&mut self) -> &mut Viewport {
        &mut self.vp
    }

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

impl EventHandler for ClientState {
    fn update(&mut self, ctx: &mut Context) -> GameResult<()> {
        // TODO: get simulation updates

        // Get new input state
        self.input.update(ctx)?;

        // Apply inputs to viewpoirt
        self.vp.apply(ctx, &self.input)?;

        // Update viewport
        self.vp.update(ctx)?;
        Ok(())
    }

    fn mouse_wheel_event(&mut self, ctx: &mut Context, x: f32, y: f32) {
        self.input.mouse_wheel_event(ctx, x, y);
        let zoom = self.input.scroll_offset;
        self.viewport().zoom(zoom);
    }

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

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

        graphics::present(ctx)
    }
}