My personal project and infrastructure archive
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
nomicon/games/rstnode/rst-client/src/state/mod.rs

130 lines
3.3 KiB

//! Game client state handling
mod map;
pub use map::*;
mod if_impl;
use crate::{
assets::Assets,
graphics::{
entities::{Coordinates, NodeRndr},
Renderer,
},
input::InputArbiter,
viewport::Viewport,
GameSettings,
};
use ggez::{event::EventHandler, graphics, Context, GameResult};
use rst_core::{
data::{Color, Level, Link, Node, Owner, Player, Pos, Upgrade},
io::Io,
};
use std::sync::Arc;
pub struct ClientState {
assets: Assets,
settings: GameSettings,
input: InputArbiter,
vp: Viewport,
// Game state
node1: NodeRndr,
node2: NodeRndr,
link: Arc<Link>,
}
impl ClientState {
pub fn new(settings: GameSettings, assets: Assets) -> Self {
info!("Initialising game client state");
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: InputArbiter::new(),
node1: NodeRndr {
inner: Arc::new(Node {
id: 0,
pos: Pos { x: 512.0, y: 512.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 {
inner: Arc::new(Node {
id: 0,
pos: Pos { x: 128.0, y: 128.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)
}
}