aboutsummaryrefslogtreecommitdiff
path: root/pixel-client/src/menu.rs
diff options
context:
space:
mode:
Diffstat (limited to 'pixel-client/src/menu.rs')
-rw-r--r--pixel-client/src/menu.rs89
1 files changed, 82 insertions, 7 deletions
diff --git a/pixel-client/src/menu.rs b/pixel-client/src/menu.rs
index 22a0244a..9ebe525e 100644
--- a/pixel-client/src/menu.rs
+++ b/pixel-client/src/menu.rs
@@ -1,17 +1,27 @@
use crate::{
- render::{AtlasLayout, Renderer},
+ game::Game,
+ network::Network,
+ render::{sprite::SpriteDraw, AtlasLayout, Renderer},
tilemap::Tilemap,
+ ui::UiState,
+ State,
};
use hurrycurry_protocol::{
glam::{IVec2, Vec2},
TileIndex,
};
use rand::{random, seq::IndexedRandom, thread_rng};
-use sdl2::keyboard::KeyboardState;
+use sdl2::{
+ keyboard::{KeyboardState, Keycode},
+ mouse::MouseState,
+};
-#[derive(Debug)]
pub struct Menu {
map: Tilemap,
+ fade_in: f32,
+ ui_state: UiState,
+ background: Vec2,
+ next_state: Option<Box<State>>,
}
impl Menu {
@@ -44,15 +54,80 @@ impl Menu {
}
}
- Self { map }
+ Self {
+ map,
+ fade_in: 0.,
+ ui_state: UiState::default(),
+ background: Vec2::ZERO,
+ next_state: None,
+ }
+ }
+ pub fn tick(
+ &mut self,
+ dt: f32,
+ 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 tick(&mut self, _dt: f32, _keyboard: &KeyboardState, _layout: &AtlasLayout) {}
- pub fn draw(&self, ctx: &mut Renderer) {
+ pub fn keyboard_event(&mut self, keycode: Keycode, down: bool) {
+ self.ui_state.keyboard_event(keycode, down);
+ }
+ pub fn draw(&mut self, ctx: &mut Renderer) {
ctx.set_world_view(
ctx.size / ctx.get_world_scale() * Vec2::new(0.8, 0.2),
ctx.size.max_element() / 32. / 15.,
);
+
+ for x in -1..=2 {
+ for y in -1..=2 {
+ ctx.draw_ui(SpriteDraw::underlay(
+ ctx.misc_textures.clouds,
+ Vec2::new(x as f32, y as f32) * 256. + self.background,
+ Vec2::ONE * 256.,
+ None,
+ ));
+ }
+ }
+ ctx.draw_ui(SpriteDraw::underlay(
+ ctx.misc_textures.solid,
+ Vec2::ZERO,
+ ctx.ui_size,
+ Some([0, 0, 0, 50]),
+ ));
+
self.map.draw(ctx);
- ctx.draw_text(Vec2::new(1., 1.), "Hello world!");
+
+ let mut request_join = false;
+ self.ui_state.draw(ctx, |ui| {
+ if ui.button(80., "Join") {
+ request_join = true
+ }
+ if ui.button(80., "Settings") {
+ eprintln!("settings button")
+ }
+ if ui.button(80., "Quit") {
+ 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,
+ Vec2::ZERO,
+ ctx.ui_size,
+ Some([0, 0, 0, 255 - (self.fade_in * 255.) as u8]),
+ ));
}
}