aboutsummaryrefslogtreecommitdiff
path: root/pixel-client/src/menu.rs
blob: 9ebe525e7e20a021ca9bce6c972fda781b95160c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use crate::{
    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, Keycode},
    mouse::MouseState,
};

pub struct Menu {
    map: Tilemap,
    fade_in: f32,
    ui_state: UiState,
    background: Vec2,
    next_state: Option<Box<State>>,
}

impl Menu {
    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,
            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 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);

        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]),
        ));
    }
}