From effbdeed66e8de8e769b8ac069926ad1a9110e62 Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Sun, 14 Feb 2021 00:06:14 +0100 Subject: rstnode: refactoring server and client components into rst-core * Add an inbox/ outbox system to server components * Define a data flow from Request -> computation -> Update * Create simple handlers to call server or client code for requests --- .../rst-client/src/graphics/entities/mod.rs | 35 ++++-- games/rstnode/rst-client/src/graphics/mod.rs | 2 +- games/rstnode/rst-client/src/input.rs | 2 +- games/rstnode/rst-client/src/main.rs | 6 +- games/rstnode/rst-client/src/net.rs | 1 + games/rstnode/rst-client/src/settings.rs | 1 - games/rstnode/rst-client/src/state.rs | 94 --------------- games/rstnode/rst-client/src/state/_match.rs | 0 games/rstnode/rst-client/src/state/if_impl.rs | 70 +++++++++++ games/rstnode/rst-client/src/state/map.rs | 10 ++ games/rstnode/rst-client/src/state/mod.rs | 129 +++++++++++++++++++++ 11 files changed, 245 insertions(+), 105 deletions(-) create mode 100644 games/rstnode/rst-client/src/net.rs delete mode 100644 games/rstnode/rst-client/src/state.rs create mode 100644 games/rstnode/rst-client/src/state/_match.rs create mode 100644 games/rstnode/rst-client/src/state/if_impl.rs create mode 100644 games/rstnode/rst-client/src/state/map.rs create mode 100644 games/rstnode/rst-client/src/state/mod.rs (limited to 'games/rstnode/rst-client/src') diff --git a/games/rstnode/rst-client/src/graphics/entities/mod.rs b/games/rstnode/rst-client/src/graphics/entities/mod.rs index a3289f6f412d..db675f816fbf 100644 --- a/games/rstnode/rst-client/src/graphics/entities/mod.rs +++ b/games/rstnode/rst-client/src/graphics/entities/mod.rs @@ -3,10 +3,10 @@ //! Generally the naming convention should be: `{type}Rndr` //! (`Renderer`, but shorter). -use crate::color; use super::prelude::*; +use crate::color; -use rst_core::data::{Owner, Node, Color}; +use rst_core::data::{Color, Link, Node, Owner}; use std::sync::Arc; /// A set of universal X/Y coordinates @@ -24,10 +24,6 @@ pub struct NodeRndr { } 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(); @@ -39,10 +35,35 @@ impl Renderer for NodeRndr { 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(()) } } + +pub struct LinkRndr { + pub loc: Coordinates, + pub inner: Arc, +} + +impl Renderer for LinkRndr { + 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(()) + } +} diff --git a/games/rstnode/rst-client/src/graphics/mod.rs b/games/rstnode/rst-client/src/graphics/mod.rs index a7ba676ecc6b..757d44d312d4 100644 --- a/games/rstnode/rst-client/src/graphics/mod.rs +++ b/games/rstnode/rst-client/src/graphics/mod.rs @@ -31,6 +31,6 @@ pub(self) mod prelude { /// A rendering trait which is given graphics context, and game state pub trait Renderer { - fn update(&mut self, _state: &mut ClientState, _ctx: &mut Context) -> GameResult<()>; + // fn update(&mut self, _state: &mut ClientState, _ctx: &mut Context) -> GameResult<()>: fn draw(&self, _state: &ClientState, _ctx: &mut Context) -> GameResult<()>; } diff --git a/games/rstnode/rst-client/src/input.rs b/games/rstnode/rst-client/src/input.rs index e9d2516a94c3..83ae17286d61 100644 --- a/games/rstnode/rst-client/src/input.rs +++ b/games/rstnode/rst-client/src/input.rs @@ -60,7 +60,7 @@ impl EventHandler for InputHandle { } fn mouse_wheel_event(&mut self, _ctx: &mut Context, _: f32, y: f32) { - self.scroll_offset += y * 0.1; + self.scroll_offset -= y * 0.25; } fn draw(&mut self, _: &mut Context) -> GameResult<()> { diff --git a/games/rstnode/rst-client/src/main.rs b/games/rstnode/rst-client/src/main.rs index a6ba4a59cdc1..4159a37d1d4a 100644 --- a/games/rstnode/rst-client/src/main.rs +++ b/games/rstnode/rst-client/src/main.rs @@ -1,5 +1,8 @@ //! RST Node game client +// Remove the warning spam +#![allow(warnings)] + #[macro_use] extern crate tracing; @@ -21,7 +24,8 @@ mod window; pub(crate) use settings::{GameSettings, GraphicsSettings, WindowSettings}; pub(crate) use state::*; -fn main() { +#[async_std::main] +async fn main() { // Initialise logging mechanism log::initialise(); diff --git a/games/rstnode/rst-client/src/net.rs b/games/rstnode/rst-client/src/net.rs new file mode 100644 index 000000000000..ff3c47774ac6 --- /dev/null +++ b/games/rstnode/rst-client/src/net.rs @@ -0,0 +1 @@ +//! Client networking endpoint logic diff --git a/games/rstnode/rst-client/src/settings.rs b/games/rstnode/rst-client/src/settings.rs index cb44eacca3a3..7fd0ba6881c1 100644 --- a/games/rstnode/rst-client/src/settings.rs +++ b/games/rstnode/rst-client/src/settings.rs @@ -43,7 +43,6 @@ pub struct Samples(pub u8); impl<'s> From<&'s Samples> for NumSamples { fn from(s: &'s Samples) -> Self { match s.0 { - 0 => Self::Zero, 1 => Self::One, 2 => Self::Two, 4 => Self::Four, diff --git a/games/rstnode/rst-client/src/state.rs b/games/rstnode/rst-client/src/state.rs deleted file mode 100644 index 8099cf9add96..000000000000 --- a/games/rstnode/rst-client/src/state.rs +++ /dev/null @@ -1,94 +0,0 @@ -//! Game client state handling - -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, Node, Owner, Player, Upgrade}; -use std::sync::Arc; - -pub struct ClientState { - assets: Assets, - settings: GameSettings, - input: InputHandle, - vp: Viewport, - - // Game state - node: NodeRndr, -} - -impl ClientState { - pub fn new(settings: GameSettings, assets: Assets) -> Self { - Self { - assets, - settings, - vp: Viewport::new(), - 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 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.node.draw(&self, ctx).unwrap(); - - graphics::present(ctx) - } -} diff --git a/games/rstnode/rst-client/src/state/_match.rs b/games/rstnode/rst-client/src/state/_match.rs new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/games/rstnode/rst-client/src/state/if_impl.rs b/games/rstnode/rst-client/src/state/if_impl.rs new file mode 100644 index 000000000000..38bac369d3cc --- /dev/null +++ b/games/rstnode/rst-client/src/state/if_impl.rs @@ -0,0 +1,70 @@ +#![allow(unused)] + +use super::ClientState; +use async_trait::async_trait; +use chrono::{DateTime, Utc}; +use rst_core::{ + wire::{ + game::Action, AuthErr, Lobby, LobbyErr, LobbyId, LobbyUpdate, MatchErr, MatchId, RegErr, + UpdateState, User, UserId, + }, + GameIf, Match, +}; +use std::sync::Arc; + +#[async_trait] +impl GameIf for ClientState { + async fn register(self: Arc, name: String, pw: String) -> Result { + todo!() + } + + async fn login(self: Arc, name: String, pw: String) -> Result { + todo!() + } + + async fn logout(self: Arc, user: User) -> Result<(), AuthErr> { + todo!() + } + + async fn anonymous(self: Arc, name: String) -> Result { + todo!() + } + + async fn join(self: Arc, user: User, lobby: LobbyId) -> Result { + todo!() + } + + async fn leave(self: Arc, user: User, lobby: LobbyId) -> Result<(), LobbyErr> { + todo!() + } + + async fn ready( + self: Arc, + user: User, + lobby: LobbyId, + ready: bool, + ) -> Result { + todo!() + } + + async fn start_req( + self: Arc, + user: UserId, + lobby: LobbyId, + ) -> Result, LobbyErr> { + todo!() + } + + async fn perform_action( + self: Arc, + user: User, + mtch: MatchId, + act: Action, + ) -> UpdateState { + todo!() + } + + async fn leave_match(self: Arc, user: User, mtch: MatchId) -> Result<(), MatchErr> { + todo!() + } +} diff --git a/games/rstnode/rst-client/src/state/map.rs b/games/rstnode/rst-client/src/state/map.rs new file mode 100644 index 000000000000..1c4666da6b4f --- /dev/null +++ b/games/rstnode/rst-client/src/state/map.rs @@ -0,0 +1,10 @@ +use rst_core::map::{Map, MapNode}; + +/// Client map state wrapper +/// +/// The map state is calculated by the server and updates are streamed +/// to all clients in a [Match](rst_core::Match). +pub struct MapState { + inner: Map, +} + diff --git a/games/rstnode/rst-client/src/state/mod.rs b/games/rstnode/rst-client/src/state/mod.rs new file mode 100644 index 000000000000..45e69eee10bb --- /dev/null +++ b/games/rstnode/rst-client/src/state/mod.rs @@ -0,0 +1,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, +} + +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) + } +} -- cgit v1.2.3