src/world.rs 4.2 K · 158 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 color(self) -> Srgba {
30 match self {
31 Terrain::Grass => Srgba::rgb_u8(92, 140, 64),
32 Terrain::Soil => Srgba::rgb_u8(122, 92, 60),
33 Terrain::Sand => Srgba::rgb_u8(214, 196, 140),
34 Terrain::ShallowWater => Srgba::rgb_u8(72, 132, 184),
35 Terrain::DeepWater => Srgba::rgb_u8(36, 76, 140),
36 Terrain::Stone => Srgba::rgb_u8(118, 118, 124),
37 }
38 }
39 }
40
41 #[derive(Resource, Clone, Debug)]
42 pub struct WorldMap {
43 pub size: UVec2,
44 tiles: Vec<Terrain>,
45 }
46
47 impl WorldMap {
48 pub fn filled(size: UVec2, terrain: Terrain) -> Self {
49 Self {
50 size,
51 tiles: vec![terrain; size.element_product() as usize],
52 }
53 }
54
55 pub fn in_bounds(&self, pos: IVec2) -> bool {
56 pos.x >= 0 && pos.y >= 0 && (pos.x as u32) < self.size.x && (pos.y as u32) < self.size.y
57 }
58
59 fn index(&self, pos: UVec2) -> usize {
60 (pos.y * self.size.x + pos.x) as usize
61 }
62
63 pub fn get(&self, pos: IVec2) -> Option<Terrain> {
64 self.in_bounds(pos)
65 .then(|| self.tiles[self.index(pos.as_uvec2())])
66 }
67
68 pub fn set(&mut self, pos: UVec2, terrain: Terrain) {
69 let i = self.index(pos);
70 self.tiles[i] = terrain;
71 }
72
73 pub fn positions(&self) -> impl Iterator<Item = UVec2> + use<> {
74 let size = self.size;
75 (0..size.y).flat_map(move |y| (0..size.x).map(move |x| UVec2::new(x, y)))
76 }
77
78 pub fn world_size(&self) -> Vec2 {
79 self.size.as_vec2() * TILE_SIZE
80 }
81
82 }
83
84 pub fn hash2(x: u32, y: u32) -> u32 {
85 let mut h = x.wrapping_mul(0x9E37_79B1) ^ y.wrapping_mul(0x85EB_CA77);
86 h ^= h >> 15;
87 h = h.wrapping_mul(0x2C1B_3C6D);
88 h ^= h >> 12;
89 h = h.wrapping_mul(0x297A_2D39);
90 h ^ (h >> 15)
91 }
92
93 pub fn static_world() -> WorldMap {
94 let size = UVec2::new(192, 128);
95 let mut map = WorldMap::filled(size, Terrain::Grass);
96 let lake = Vec2::new(140.0, 40.0);
97 let mountain = Vec2::new(150.0, 105.0);
98
99 for pos in map.positions() {
100 let p = pos.as_vec2();
101 let river_x = 60.0 + 8.0 * (p.y / 14.0).sin();
102 let river_dist = (p.x - river_x).abs();
103 let lake_dist = p.distance(lake);
104 let mountain_dist = ((p - mountain) / Vec2::new(36.0, 20.0)).length();
105
106 let terrain = if mountain_dist < 1.0 {
107 Terrain::Stone
108 } else if lake_dist < 9.0 {
109 Terrain::DeepWater
110 } else if lake_dist < 14.0 || river_dist < 2.5 {
111 Terrain::ShallowWater
112 } else if lake_dist < 17.0 || river_dist < 4.0 {
113 Terrain::Sand
114 } else if hash2(pos.x / 6, pos.y / 6) % 7 == 0 {
115 Terrain::Soil
116 } else {
117 Terrain::Grass
118 };
119 map.set(pos, terrain);
120 }
121 map
122 }
123
124 #[cfg(test)]
125 mod tests {
126 use super::*;
127
128 #[test]
129 fn get_out_of_bounds_is_none() {
130 let map = WorldMap::filled(UVec2::new(4, 3), Terrain::Grass);
131 assert_eq!(map.get(IVec2::new(-1, 0)), None);
132 assert_eq!(map.get(IVec2::new(4, 0)), None);
133 assert_eq!(map.get(IVec2::new(0, 3)), None);
134 assert_eq!(map.get(IVec2::new(3, 2)), Some(Terrain::Grass));
135 }
136
137 #[test]
138 fn tiles_are_stored_row_major_from_bottom_left() {
139 let mut map = WorldMap::filled(UVec2::new(4, 3), Terrain::Grass);
140 map.set(UVec2::new(1, 2), Terrain::Stone);
141 assert_eq!(map.tiles[2 * 4 + 1], Terrain::Stone);
142 }
143
144 #[test]
145 fn terrain_index_matches_position_in_all() {
146 for (i, t) in Terrain::ALL.iter().enumerate() {
147 assert_eq!(t.index() as usize, i);
148 }
149 }
150
151 #[test]
152 fn static_world_contains_every_terrain() {
153 let map = static_world();
154 for t in Terrain::ALL {
155 assert!(map.positions().any(|p| map.get(p.as_ivec2()) == Some(t)), "{t:?}");
156 }
157 }
158 }