diff options
author | metamuffin <metamuffin@disroot.org> | 2024-07-25 17:11:38 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-07-25 17:11:38 +0200 |
commit | a8ce0f8cf64a89c18cbb08dd10f2b2d9b22ad56f (patch) | |
tree | 7e94d7ef0e9d62a2266d593eb4fbfc30f204277b /pixel-client/src/menu | |
parent | 6c4b81d58272371bc76e79dd086be3161f6b7193 (diff) | |
download | hurrycurry-a8ce0f8cf64a89c18cbb08dd10f2b2d9b22ad56f.tar hurrycurry-a8ce0f8cf64a89c18cbb08dd10f2b2d9b22ad56f.tar.bz2 hurrycurry-a8ce0f8cf64a89c18cbb08dd10f2b2d9b22ad56f.tar.zst |
menus
Diffstat (limited to 'pixel-client/src/menu')
-rw-r--r-- | pixel-client/src/menu/background.rs | 79 | ||||
-rw-r--r-- | pixel-client/src/menu/main.rs | 100 | ||||
-rw-r--r-- | pixel-client/src/menu/mod.rs | 4 | ||||
-rw-r--r-- | pixel-client/src/menu/settings.rs | 11 |
4 files changed, 194 insertions, 0 deletions
diff --git a/pixel-client/src/menu/background.rs b/pixel-client/src/menu/background.rs new file mode 100644 index 00000000..daf89360 --- /dev/null +++ b/pixel-client/src/menu/background.rs @@ -0,0 +1,79 @@ +use crate::{ + render::{sprite::SpriteDraw, AtlasLayout, Renderer}, + tilemap::Tilemap, +}; +use hurrycurry_protocol::{ + glam::{IVec2, Vec2}, + TileIndex, +}; +use rand::{random, seq::IndexedRandom, thread_rng}; + +pub struct MenuBackground { + background: Vec2, + map: Tilemap, +} + +impl MenuBackground { + pub fn new(layout: &AtlasLayout) -> Self { + let mut map = Tilemap::default(); + map.init( + &[ + "floor", + "tomato-crate", + "raw-steak-crate", + "table", + "chair", + "counter", + "sink", + "stove", + ] + .map(String::from), + layout, + ); + static BUCKETS: &[&[usize]] = &[&[], &[0, 0, 0, 0, 1, 2], &[3, 4, 5], &[6, 7]]; + + for x in -10..11 { + for y in -10..11 { + let p = Vec2::new(x as f32, y as f32); + let w = (-p.length() * 0.15).exp(); + let k = ((random::<f32>() * w) * BUCKETS.len() as f32) as usize; + if let Some(ti) = BUCKETS[k.min(BUCKETS.len())].choose(&mut thread_rng()) { + map.set(IVec2::new(x, y), Some(TileIndex(*ti)), [None; 4]) + } + } + } + Self { + map, + background: Vec2::ZERO, + } + } + + pub fn tick(&mut self, dt: f32) { + self.background += Vec2::new(2., 3.) * dt; + } + pub fn draw(&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); + } +} diff --git a/pixel-client/src/menu/main.rs b/pixel-client/src/menu/main.rs new file mode 100644 index 00000000..37cecd3e --- /dev/null +++ b/pixel-client/src/menu/main.rs @@ -0,0 +1,100 @@ +/* + Hurry Curry! - a game about cooking + Copyright 2024 metamuffin + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ +use super::{background::MenuBackground, settings::Settings}; +use crate::{ + game::Game, + network::Network, + render::{sprite::SpriteDraw, AtlasLayout, Renderer}, + ui::UiState, + State, +}; +use hurrycurry_protocol::glam::Vec2; +use sdl2::{ + keyboard::{KeyboardState, Keycode}, + mouse::MouseState, +}; + +pub struct MainMenu { + background: MenuBackground, + fade_in: f32, + pub ui_state: UiState, + server_address: String, + next_state: Option<Box<State>>, + settings: Option<Settings>, +} + +impl MainMenu { + pub fn new(layout: &AtlasLayout) -> Self { + Self { + background: MenuBackground::new(layout), + fade_in: 0., + server_address: String::from("ws://127.0.0.1"), + ui_state: UiState::default(), + next_state: None, + settings: 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.ui_state.update(keyboard, mouse, dt); + self.background.tick(dt); + self.next_state.take() + } + 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) { + self.background.draw(ctx); + + self.ui_state.draw(ctx, |ui| { + if let Some(settings) = &mut self.settings { + if settings.draw(ui) { + self.settings = None; + } + return; + } + if ui.button(80., "Join") { + self.next_state = Some(Box::new(State::Ingame(Box::new(Game::new( + Network::connect(&self.server_address).unwrap(), + ui.renderer.atlas_layout(), + ))))) + } + ui.textedit(80., &mut self.server_address); + if ui.button(80., "Settings") { + self.settings = Some(Settings::default()) + } + if ui.button(80., "Quit") { + self.next_state = Some(Box::new(State::Quit)); + } + ui.fill(); + }); + + 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]), + )); + } +} diff --git a/pixel-client/src/menu/mod.rs b/pixel-client/src/menu/mod.rs new file mode 100644 index 00000000..d712eaa9 --- /dev/null +++ b/pixel-client/src/menu/mod.rs @@ -0,0 +1,4 @@ + +pub mod main; +pub mod background; +pub mod settings; diff --git a/pixel-client/src/menu/settings.rs b/pixel-client/src/menu/settings.rs new file mode 100644 index 00000000..db524496 --- /dev/null +++ b/pixel-client/src/menu/settings.rs @@ -0,0 +1,11 @@ +use crate::ui::Ui; + +#[derive(Default)] +pub struct Settings {} + +impl Settings { + pub fn draw(&mut self, ui: &mut Ui) -> bool { + ui.text("Settings placeholder"); + return ui.button(80., "Back"); + } +} |