summaryrefslogtreecommitdiff
path: root/pixel-client/src/tilemap.rs
blob: b67fc224123f85c03b65c4c3e93fd9c456c27483 (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
/*
    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 crate::render::{
    sprite::{Sprite, SpriteDraw},
    Renderer,
};
use hurrycurry_protocol::{glam::IVec2, TileIndex};
use log::warn;
use sdl2::rect::Rect;
use std::collections::{HashMap, HashSet};

#[derive(Default, Debug)]
pub struct Tilemap {
    connect_group_by_tile: Vec<Option<usize>>,
    connect_members_by_group: Vec<HashSet<Option<TileIndex>>>,
    tile_srcs: Vec<[Rect; 16]>,
    tiles: HashMap<IVec2, SpriteDraw>,
}

impl Tilemap {
    pub fn init(&mut self, tile_names: &[String], sprite_rects: &HashMap<String, Rect>) {
        let tile_index = tile_names
            .iter()
            .enumerate()
            .map(|(t, i)| (i.to_string(), t))
            .collect::<HashMap<_, _>>();
        self.connect_group_by_tile = vec![None; tile_names.len()];
        self.connect_members_by_group = include_str!("../assets/connect.csv")
            .lines()
            .enumerate()
            .map(|(gid, line)| {
                line.split(",")
                    .flat_map(|tile| tile_index.get(tile).copied())
                    .map(|ti| {
                        self.connect_group_by_tile[ti] = Some(gid);
                        Some(TileIndex(ti))
                    })
                    .collect::<HashSet<_>>()
            })
            .collect::<Vec<_>>();

        self.tile_srcs = tile_names
            .iter()
            .map(|name| {
                let fallback = sprite_rects
                    .get(&format!("{name}+a"))
                    .copied()
                    .unwrap_or_else(|| {
                        warn!("no sprite for tile {name:?}");
                        Rect::new(0, 0, 0, 0)
                    });

                [
                    sprite_rects.get(&format!("{name}+")),
                    sprite_rects.get(&format!("{name}+w")),
                    sprite_rects.get(&format!("{name}+e")),
                    sprite_rects.get(&format!("{name}+we")),
                    sprite_rects.get(&format!("{name}+n")),
                    sprite_rects.get(&format!("{name}+wn")),
                    sprite_rects.get(&format!("{name}+en")),
                    sprite_rects.get(&format!("{name}+wen")),
                    sprite_rects.get(&format!("{name}+s")),
                    sprite_rects.get(&format!("{name}+ws")),
                    sprite_rects.get(&format!("{name}+es")),
                    sprite_rects.get(&format!("{name}+wes")),
                    sprite_rects.get(&format!("{name}+ns")),
                    sprite_rects.get(&format!("{name}+wns")),
                    sprite_rects.get(&format!("{name}+ens")),
                    sprite_rects.get(&format!("{name}+wens")),
                ]
                .map(|e| e.copied().unwrap_or(fallback))
            })
            .collect();
    }

    pub fn set(&mut self, pos: IVec2, tile: Option<TileIndex>, neighbors: [Option<TileIndex>; 4]) {
        let Some(tile) = tile else {
            self.tiles.remove(&pos);
            return;
        };

        let mut idx = 0;
        if let Some(gid) = self.connect_group_by_tile[tile.0] {
            let cgroup = &self.connect_members_by_group[gid];
            idx |= 0b0100 * (cgroup.contains(&neighbors[0])) as usize;
            idx |= (cgroup.contains(&neighbors[1])) as usize;
            idx |= 0b1000 * (cgroup.contains(&neighbors[2])) as usize;
            idx |= 0b0010 * (cgroup.contains(&neighbors[3])) as usize;
        }

        let src = self.tile_srcs[tile.0][idx];
        self.tiles
            .insert(pos, Sprite::new_tile(src).at(pos.as_vec2()));
    }

    pub fn draw(&self, ctx: &mut Renderer) {
        for &sprite in self.tiles.values() {
            ctx.draw_world(sprite);
        }
    }
}