diff options
Diffstat (limited to 'pixel-client')
-rw-r--r-- | pixel-client/src/game.rs | 12 | ||||
-rw-r--r-- | pixel-client/src/main.rs | 25 | ||||
-rw-r--r-- | pixel-client/src/menu.rs | 21 | ||||
-rw-r--r-- | pixel-client/src/ui.rs | 4 |
4 files changed, 41 insertions, 21 deletions
diff --git a/pixel-client/src/game.rs b/pixel-client/src/game.rs index cc1645d9..af387b95 100644 --- a/pixel-client/src/game.rs +++ b/pixel-client/src/game.rs @@ -24,6 +24,7 @@ use crate::{ AtlasLayout, Renderer, }, tilemap::Tilemap, + State, }; use hurrycurry_protocol::{ glam::{IVec2, Vec2}, @@ -106,7 +107,12 @@ impl Game { } } - pub fn tick(&mut self, dt: f32, keyboard: &KeyboardState, layout: &AtlasLayout) { + pub fn tick( + &mut self, + dt: f32, + keyboard: &KeyboardState, + layout: &AtlasLayout, + ) -> Option<Box<State>> { self.network.poll(); // TODO perf @@ -192,7 +198,9 @@ impl Game { self.items_removed.retain_mut(|i| { i.tick(0., dt); i.alive > 0.01 - }) + }); + + None } pub fn packet_in(&mut self, packet: PacketC, layout: &AtlasLayout) { diff --git a/pixel-client/src/main.rs b/pixel-client/src/main.rs index fb98d12f..05e19194 100644 --- a/pixel-client/src/main.rs +++ b/pixel-client/src/main.rs @@ -22,12 +22,7 @@ use hurrycurry_protocol::glam::Vec2; use menu::Menu; use network::Network; use render::Renderer; -use sdl2::{ - event::Event, - keyboard::{KeyboardState, Keycode}, - mouse::MouseState, - pixels::Color, -}; +use sdl2::{event::Event, keyboard::KeyboardState, mouse::MouseState, pixels::Color}; use std::time::{Duration, Instant}; pub mod game; @@ -54,9 +49,10 @@ pub enum Action { }, } -enum State { +pub enum State { Ingame(Box<Game>), Menu(Menu), + Quit, } fn main() { @@ -110,9 +106,13 @@ fn main() { let mouse = MouseState::new(&events); let dt = last_tick.elapsed().min(Duration::from_secs_f32(1. / 30.)); - match &mut state { + let next = match &mut state { State::Ingame(x) => x.tick(dt.as_secs_f32(), &keyboard, renderer.atlas_layout()), State::Menu(x) => x.tick(dt.as_secs_f32(), &keyboard, &mouse, renderer.atlas_layout()), + State::Quit => break, + }; + if let Some(next) = next { + state = *next; } last_tick += dt; @@ -121,6 +121,7 @@ fn main() { match &mut state { State::Ingame(x) => x.draw(&mut renderer), State::Menu(x) => x.draw(&mut renderer), + State::Quit => (), } canvas.set_draw_color(Color::BLACK); @@ -130,17 +131,14 @@ fn main() { for event in events.poll_iter() { match event { - Event::Quit { .. } - | Event::KeyDown { - keycode: Option::Some(Keycode::Escape), - .. - } => break 'mainloop, + Event::Quit { .. } => break 'mainloop, Event::KeyUp { keycode: Some(keycode), .. } => match &mut state { State::Ingame(_) => (), State::Menu(menu) => menu.keyboard_event(keycode, false), + _ => (), }, Event::KeyDown { keycode: Some(keycode), @@ -148,6 +146,7 @@ fn main() { } => match &mut state { State::Ingame(_) => (), State::Menu(menu) => menu.keyboard_event(keycode, true), + _ => (), }, _ => {} } diff --git a/pixel-client/src/menu.rs b/pixel-client/src/menu.rs index bce5198b..9ebe525e 100644 --- a/pixel-client/src/menu.rs +++ b/pixel-client/src/menu.rs @@ -1,7 +1,10 @@ use crate::{ + game::Game, + network::Network, render::{sprite::SpriteDraw, AtlasLayout, Renderer}, tilemap::Tilemap, ui::UiState, + State, }; use hurrycurry_protocol::{ glam::{IVec2, Vec2}, @@ -13,12 +16,12 @@ use sdl2::{ mouse::MouseState, }; -#[derive(Debug)] pub struct Menu { map: Tilemap, fade_in: f32, ui_state: UiState, background: Vec2, + next_state: Option<Box<State>>, } impl Menu { @@ -56,6 +59,7 @@ impl Menu { fade_in: 0., ui_state: UiState::default(), background: Vec2::ZERO, + next_state: None, } } pub fn tick( @@ -64,10 +68,12 @@ impl Menu { keyboard: &KeyboardState, mouse: &MouseState, _layout: &AtlasLayout, - ) { + ) -> Option<Box<State>> { self.fade_in = (self.fade_in + dt).min(1.); self.background += Vec2::new(2., 3.) * dt; self.ui_state.update(keyboard, mouse, dt); + + self.next_state.take() } pub fn keyboard_event(&mut self, keycode: Keycode, down: bool) { self.ui_state.keyboard_event(keycode, down); @@ -97,18 +103,25 @@ impl Menu { self.map.draw(ctx); + let mut request_join = false; self.ui_state.draw(ctx, |ui| { if ui.button(80., "Join") { - eprintln!("join button") + request_join = true } if ui.button(80., "Settings") { eprintln!("settings button") } if ui.button(80., "Quit") { - eprintln!("quit button") + self.next_state = Some(Box::new(State::Quit)); } ui.fill(); }); + if request_join { + self.next_state = Some(Box::new(State::Ingame(Box::new(Game::new( + Network::connect("ws://127.0.0.1").unwrap(), + ctx.atlas_layout(), + ))))) + } ctx.draw_ui(SpriteDraw::overlay( ctx.misc_textures.solid, diff --git a/pixel-client/src/ui.rs b/pixel-client/src/ui.rs index 9a04c5c0..802c8e2f 100644 --- a/pixel-client/src/ui.rs +++ b/pixel-client/src/ui.rs @@ -5,7 +5,7 @@ use sdl2::{ mouse::MouseState, }; -#[derive(Debug, Default)] +#[derive(Default)] pub struct FocusDevice { focus: usize, pressing: Option<usize>, @@ -14,7 +14,7 @@ pub struct FocusDevice { interact_down: bool, } -#[derive(Default, Debug)] +#[derive(Default)] pub struct UiState { mouse_position: Vec2, ui_scale: Vec2, |