aboutsummaryrefslogtreecommitdiff
path: root/pixel-client/src/menu/background.rs
blob: 190858a675b5f845f7c6c89ff0d62af8fcf28af5 (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
/*
    Hurry Curry! - a game about cooking
    Copyright (C) 2025 Hurry Curry! Contributors

    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 crate::{
    render::{sprite::SpriteDraw, AtlasLayout, Renderer},
    tilemap::Tilemap,
};
use hurrycurry_protocol::{
    glam::{IVec2, Vec2},
    TileIndex,
};
use rand::{random, rng, seq::IndexedRandom};

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",
                "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 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;
        self.background %= 256.;
    }
    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);
    }
}