aboutsummaryrefslogtreecommitdiff
path: root/games/rstnode/rst-client/src/state/mod.rs
//! 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)
    }
}