diff options
Diffstat (limited to 'games/rstnode/rst-client/src/state')
-rw-r--r-- | games/rstnode/rst-client/src/state/_match.rs | 0 | ||||
-rw-r--r-- | games/rstnode/rst-client/src/state/if_impl.rs | 70 | ||||
-rw-r--r-- | games/rstnode/rst-client/src/state/map.rs | 10 | ||||
-rw-r--r-- | games/rstnode/rst-client/src/state/mod.rs | 129 |
4 files changed, 209 insertions, 0 deletions
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 --- /dev/null +++ b/games/rstnode/rst-client/src/state/_match.rs 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<Self>, name: String, pw: String) -> Result<UserId, RegErr> { + todo!() + } + + async fn login(self: Arc<Self>, name: String, pw: String) -> Result<User, AuthErr> { + todo!() + } + + async fn logout(self: Arc<Self>, user: User) -> Result<(), AuthErr> { + todo!() + } + + async fn anonymous(self: Arc<Self>, name: String) -> Result<User, AuthErr> { + todo!() + } + + async fn join(self: Arc<Self>, user: User, lobby: LobbyId) -> Result<Lobby, LobbyErr> { + todo!() + } + + async fn leave(self: Arc<Self>, user: User, lobby: LobbyId) -> Result<(), LobbyErr> { + todo!() + } + + async fn ready( + self: Arc<Self>, + user: User, + lobby: LobbyId, + ready: bool, + ) -> Result<LobbyUpdate, LobbyErr> { + todo!() + } + + async fn start_req( + self: Arc<Self>, + user: UserId, + lobby: LobbyId, + ) -> Result<DateTime<Utc>, LobbyErr> { + todo!() + } + + async fn perform_action( + self: Arc<Self>, + user: User, + mtch: MatchId, + act: Action, + ) -> UpdateState { + todo!() + } + + async fn leave_match(self: Arc<Self>, 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<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) + } +} |