src/world.rs 5.3 K · 195 lines · raw · history

1 use bevy::prelude::*;
2
3 pub const TILE_SIZE: f32 = 16.0;
4
5 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
6 pub enum Terrain {
7 Grass,
8 Soil,
9 Sand,
10 ShallowWater,
11 DeepWater,
12 Stone,
13 }
14
15 impl Terrain {
16 pub const ALL: [Terrain; 6] = [
17 Terrain::Grass,
18 Terrain::Soil,
19 Terrain::Sand,
20 Terrain::ShallowWater,
21 Terrain::DeepWater,
22 Terrain::Stone,
23 ];
24
25 pub fn index(self) -> u16 {
26 self as u16
27 }
28
29 pub fn move_cost(self) -> Option<u32> {
30 match self {
31 Terrain::Grass | Terrain::Soil => Some(100),
32 Terrain::Sand => Some(130),
33 Terrain::ShallowWater => Some(300),
34 Terrain::DeepWater | Terrain::Stone => None,
35 }
36 }
37
38 pub fn color(self) -> Srgba {
39 match self {
40 Terrain::Grass => Srgba::rgb_u8(92, 140, 64),
41 Terrain::Soil => Srgba::rgb_u8(122, 92, 60),
42 Terrain::Sand => Srgba::rgb_u8(214, 196, 140),
43 Terrain::ShallowWater => Srgba::rgb_u8(72, 132, 184),
44 Terrain::DeepWater => Srgba::rgb_u8(36, 76, 140),
45 Terrain::Stone => Srgba::rgb_u8(118, 118, 124),
46 }
47 }
48 }
49
50 #[derive(Resource, Clone, Debug)]
51 pub struct WorldMap {
52 pub size: UVec2,
53 tiles: Vec<Terrain>,
54 }
55
56 impl WorldMap {
57 pub fn filled(size: UVec2, terrain: Terrain) -> Self {
58 Self {
59 size,
60 tiles: vec![terrain; size.element_product() as usize],
61 }
62 }
63
64 pub fn in_bounds(&self, pos: IVec2) -> bool {
65 pos.x >= 0 && pos.y >= 0 && (pos.x as u32) < self.size.x && (pos.y as u32) < self.size.y
66 }
67
68 fn index(&self, pos: UVec2) -> usize {
69 (pos.y * self.size.x + pos.x) as usize
70 }
71
72 pub fn get(&self, pos: IVec2) -> Option<Terrain> {
73 self.in_bounds(pos)
74 .then(|| self.tiles[self.index(pos.as_uvec2())])
75 }
76
77 pub fn set(&mut self, pos: UVec2, terrain: Terrain) {
78 let i = self.index(pos);
79 self.tiles[i] = terrain;
80 }
81
82 pub fn move_cost(&self, pos: IVec2) -> Option<u32> {
83 self.get(pos).and_then(Terrain::move_cost)
84 }
85
86 pub fn positions(&self) -> impl Iterator<Item = UVec2> + use<> {
87 let size = self.size;
88 (0..size.y).flat_map(move |y| (0..size.x).map(move |x| UVec2::new(x, y)))
89 }
90
91 pub fn world_size(&self) -> Vec2 {
92 self.size.as_vec2() * TILE_SIZE
93 }
94
95 pub fn tile_center(pos: UVec2) -> Vec2 {
96 (pos.as_vec2() + 0.5) * TILE_SIZE
97 }
98
99 pub fn tile_at(&self, world: Vec2) -> Option<UVec2> {
100 let pos = (world / TILE_SIZE).floor().as_ivec2();
101 self.in_bounds(pos).then(|| pos.as_uvec2())
102 }
103
104 }
105
106 pub fn hash2(x: u32, y: u32) -> u32 {
107 let mut h = x.wrapping_mul(0x9E37_79B1) ^ y.wrapping_mul(0x85EB_CA77);
108 h ^= h >> 15;
109 h = h.wrapping_mul(0x2C1B_3C6D);
110 h ^= h >> 12;
111 h = h.wrapping_mul(0x297A_2D39);
112 h ^ (h >> 15)
113 }
114
115 pub fn static_world() -> WorldMap {
116 let size = UVec2::new(192, 128);
117 let mut map = WorldMap::filled(size, Terrain::Grass);
118 let lake = Vec2::new(140.0, 40.0);
119 let mountain = Vec2::new(150.0, 105.0);
120
121 for pos in map.positions() {
122 let p = pos.as_vec2();
123 let river_x = 60.0 + 8.0 * (p.y / 14.0).sin();
124 let river_dist = (p.x - river_x).abs();
125 let lake_dist = p.distance(lake);
126 let mountain_dist = ((p - mountain) / Vec2::new(36.0, 20.0)).length();
127
128 let terrain = if mountain_dist < 1.0 {
129 Terrain::Stone
130 } else if lake_dist < 9.0 {
131 Terrain::DeepWater
132 } else if lake_dist < 14.0 || river_dist < 2.5 {
133 Terrain::ShallowWater
134 } else if lake_dist < 17.0 || river_dist < 4.0 {
135 Terrain::Sand
136 } else if hash2(pos.x / 6, pos.y / 6) % 7 == 0 {
137 Terrain::Soil
138 } else {
139 Terrain::Grass
140 };
141 map.set(pos, terrain);
142 }
143 map
144 }
145
146 #[cfg(test)]
147 mod tests {
148 use super::*;
149
150 #[test]
151 fn get_out_of_bounds_is_none() {
152 let map = WorldMap::filled(UVec2::new(4, 3), Terrain::Grass);
153 assert_eq!(map.get(IVec2::new(-1, 0)), None);
154 assert_eq!(map.get(IVec2::new(4, 0)), None);
155 assert_eq!(map.get(IVec2::new(0, 3)), None);
156 assert_eq!(map.get(IVec2::new(3, 2)), Some(Terrain::Grass));
157 }
158
159 #[test]
160 fn tiles_are_stored_row_major_from_bottom_left() {
161 let mut map = WorldMap::filled(UVec2::new(4, 3), Terrain::Grass);
162 map.set(UVec2::new(1, 2), Terrain::Stone);
163 assert_eq!(map.tiles[2 * 4 + 1], Terrain::Stone);
164 }
165
166 #[test]
167 fn tile_at_inverts_tile_center() {
168 let map = static_world();
169 for pos in [UVec2::ZERO, UVec2::new(17, 99), map.size - 1] {
170 assert_eq!(map.tile_at(WorldMap::tile_center(pos)), Some(pos));
171 }
172 }
173
174 #[test]
175 fn tile_at_outside_world_is_none() {
176 let map = static_world();
177 assert_eq!(map.tile_at(Vec2::new(-0.1, 0.0)), None);
178 assert_eq!(map.tile_at(map.world_size()), None);
179 }
180
181 #[test]
182 fn terrain_index_matches_position_in_all() {
183 for (i, t) in Terrain::ALL.iter().enumerate() {
184 assert_eq!(t.index() as usize, i);
185 }
186 }
187
188 #[test]
189 fn static_world_contains_every_terrain() {
190 let map = static_world();
191 for t in Terrain::ALL {
192 assert!(map.positions().any(|p| map.get(p.as_ivec2()) == Some(t)), "{t:?}");
193 }
194 }
195 }