| 1 |
use bevy::prelude::*; |
| 2 |
|
| 3 |
pub const TILE_SIZE: f32 = 16.0; |
| 4 |
pub const MAX_HEIGHT: f32 = 100.0; |
| 5 |
pub const UPHILL_COST_PER_METER: f32 = 20.0; |
| 6 |
|
| 7 |
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 8 |
pub enum Terrain { |
| 9 |
Grass, |
| 10 |
Soil, |
| 11 |
Sand, |
| 12 |
ShallowWater, |
| 13 |
DeepWater, |
| 14 |
Stone, |
| 15 |
IronOre, |
| 16 |
GoldOre, |
| 17 |
} |
| 18 |
|
| 19 |
impl Terrain { |
| 20 |
pub const ALL: [Terrain; 8] = [ |
| 21 |
Terrain::Grass, |
| 22 |
Terrain::Soil, |
| 23 |
Terrain::Sand, |
| 24 |
Terrain::ShallowWater, |
| 25 |
Terrain::DeepWater, |
| 26 |
Terrain::Stone, |
| 27 |
Terrain::IronOre, |
| 28 |
Terrain::GoldOre, |
| 29 |
]; |
| 30 |
|
| 31 |
pub fn index(self) -> u16 { |
| 32 |
self as u16 |
| 33 |
} |
| 34 |
|
| 35 |
pub fn move_cost(self) -> Option<u32> { |
| 36 |
match self { |
| 37 |
Terrain::Grass | Terrain::Soil => Some(100), |
| 38 |
Terrain::Sand => Some(130), |
| 39 |
Terrain::ShallowWater => Some(300), |
| 40 |
Terrain::DeepWater | Terrain::Stone | Terrain::IronOre | Terrain::GoldOre => None, |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
pub fn is_water(self) -> bool { |
| 45 |
matches!(self, Terrain::ShallowWater | Terrain::DeepWater) |
| 46 |
} |
| 47 |
|
| 48 |
pub fn is_rock(self) -> bool { |
| 49 |
matches!(self, Terrain::Stone | Terrain::IronOre | Terrain::GoldOre) |
| 50 |
} |
| 51 |
|
| 52 |
pub fn color(self) -> Srgba { |
| 53 |
match self { |
| 54 |
Terrain::Grass => Srgba::rgb_u8(92, 140, 64), |
| 55 |
Terrain::Soil => Srgba::rgb_u8(122, 92, 60), |
| 56 |
Terrain::Sand => Srgba::rgb_u8(214, 196, 140), |
| 57 |
Terrain::ShallowWater => Srgba::rgb_u8(72, 132, 184), |
| 58 |
Terrain::DeepWater => Srgba::rgb_u8(36, 76, 140), |
| 59 |
Terrain::Stone | Terrain::IronOre | Terrain::GoldOre => Srgba::rgb_u8(118, 118, 124), |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
pub fn speck_color(self) -> Option<Srgba> { |
| 64 |
match self { |
| 65 |
Terrain::IronOre => Some(Srgba::rgb_u8(178, 104, 72)), |
| 66 |
Terrain::GoldOre => Some(Srgba::rgb_u8(236, 196, 60)), |
| 67 |
_ => None, |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 73 |
pub enum Plant { |
| 74 |
Tree, |
| 75 |
} |
| 76 |
|
| 77 |
impl Plant { |
| 78 |
pub const ALL: [Plant; 1] = [Plant::Tree]; |
| 79 |
|
| 80 |
pub fn index(self) -> u16 { |
| 81 |
self as u16 |
| 82 |
} |
| 83 |
|
| 84 |
pub fn extra_move_cost(self) -> u32 { |
| 85 |
match self { |
| 86 |
Plant::Tree => 100, |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
#[derive(Resource, Clone, Debug)] |
| 92 |
pub struct WorldMap { |
| 93 |
pub size: UVec2, |
| 94 |
tiles: Vec<Terrain>, |
| 95 |
plants: Vec<Option<Plant>>, |
| 96 |
heights: Vec<f32>, |
| 97 |
} |
| 98 |
|
| 99 |
impl WorldMap { |
| 100 |
pub fn filled(size: UVec2, terrain: Terrain) -> Self { |
| 101 |
let n = size.element_product() as usize; |
| 102 |
Self { |
| 103 |
size, |
| 104 |
tiles: vec![terrain; n], |
| 105 |
plants: vec![None; n], |
| 106 |
heights: vec![0.0; n], |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
pub fn in_bounds(&self, pos: IVec2) -> bool { |
| 111 |
pos.x >= 0 && pos.y >= 0 && (pos.x as u32) < self.size.x && (pos.y as u32) < self.size.y |
| 112 |
} |
| 113 |
|
| 114 |
fn index(&self, pos: UVec2) -> usize { |
| 115 |
(pos.y * self.size.x + pos.x) as usize |
| 116 |
} |
| 117 |
|
| 118 |
pub fn get(&self, pos: IVec2) -> Option<Terrain> { |
| 119 |
self.in_bounds(pos) |
| 120 |
.then(|| self.tiles[self.index(pos.as_uvec2())]) |
| 121 |
} |
| 122 |
|
| 123 |
pub fn set(&mut self, pos: UVec2, terrain: Terrain) { |
| 124 |
let i = self.index(pos); |
| 125 |
self.tiles[i] = terrain; |
| 126 |
} |
| 127 |
|
| 128 |
pub fn plant(&self, pos: IVec2) -> Option<Plant> { |
| 129 |
self.in_bounds(pos) |
| 130 |
.then(|| self.plants[self.index(pos.as_uvec2())]) |
| 131 |
.flatten() |
| 132 |
} |
| 133 |
|
| 134 |
pub fn set_plant(&mut self, pos: UVec2, plant: Option<Plant>) { |
| 135 |
let i = self.index(pos); |
| 136 |
self.plants[i] = plant; |
| 137 |
} |
| 138 |
|
| 139 |
pub fn height(&self, pos: IVec2) -> Option<f32> { |
| 140 |
self.in_bounds(pos) |
| 141 |
.then(|| self.heights[self.index(pos.as_uvec2())]) |
| 142 |
} |
| 143 |
|
| 144 |
pub fn set_height(&mut self, pos: UVec2, height: f32) { |
| 145 |
let i = self.index(pos); |
| 146 |
self.heights[i] = height; |
| 147 |
} |
| 148 |
|
| 149 |
pub fn move_cost(&self, pos: IVec2) -> Option<u32> { |
| 150 |
let terrain = self.get(pos)?.move_cost()?; |
| 151 |
Some(terrain + self.plant(pos).map_or(0, Plant::extra_move_cost)) |
| 152 |
} |
| 153 |
|
| 154 |
pub fn step_cost(&self, from: IVec2, to: IVec2) -> Option<u32> { |
| 155 |
let climb = (self.height(to)? - self.height(from)?).max(0.0); |
| 156 |
Some(self.move_cost(to)? + (climb * UPHILL_COST_PER_METER).round() as u32) |
| 157 |
} |
| 158 |
|
| 159 |
pub fn positions(&self) -> impl Iterator<Item = UVec2> + use<> { |
| 160 |
let size = self.size; |
| 161 |
(0..size.y).flat_map(move |y| (0..size.x).map(move |x| UVec2::new(x, y))) |
| 162 |
} |
| 163 |
|
| 164 |
pub fn world_size(&self) -> Vec2 { |
| 165 |
self.size.as_vec2() * TILE_SIZE |
| 166 |
} |
| 167 |
|
| 168 |
pub fn tile_center(pos: UVec2) -> Vec2 { |
| 169 |
(pos.as_vec2() + 0.5) * TILE_SIZE |
| 170 |
} |
| 171 |
|
| 172 |
pub fn tile_at(&self, world: Vec2) -> Option<UVec2> { |
| 173 |
let pos = (world / TILE_SIZE).floor().as_ivec2(); |
| 174 |
self.in_bounds(pos).then(|| pos.as_uvec2()) |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
pub fn hash2(x: u32, y: u32) -> u32 { |
| 179 |
let mut h = x.wrapping_mul(0x9E37_79B1) ^ y.wrapping_mul(0x85EB_CA77); |
| 180 |
h ^= h >> 15; |
| 181 |
h = h.wrapping_mul(0x2C1B_3C6D); |
| 182 |
h ^= h >> 12; |
| 183 |
h = h.wrapping_mul(0x297A_2D39); |
| 184 |
h ^ (h >> 15) |
| 185 |
} |
| 186 |
|
| 187 |
#[cfg(test)] |
| 188 |
mod tests { |
| 189 |
use super::*; |
| 190 |
|
| 191 |
#[test] |
| 192 |
fn get_out_of_bounds_is_none() { |
| 193 |
let map = WorldMap::filled(UVec2::new(4, 3), Terrain::Grass); |
| 194 |
assert_eq!(map.get(IVec2::new(-1, 0)), None); |
| 195 |
assert_eq!(map.get(IVec2::new(4, 0)), None); |
| 196 |
assert_eq!(map.get(IVec2::new(0, 3)), None); |
| 197 |
assert_eq!(map.get(IVec2::new(3, 2)), Some(Terrain::Grass)); |
| 198 |
} |
| 199 |
|
| 200 |
#[test] |
| 201 |
fn tiles_are_stored_row_major_from_bottom_left() { |
| 202 |
let mut map = WorldMap::filled(UVec2::new(4, 3), Terrain::Grass); |
| 203 |
map.set(UVec2::new(1, 2), Terrain::Stone); |
| 204 |
assert_eq!(map.tiles[2 * 4 + 1], Terrain::Stone); |
| 205 |
} |
| 206 |
|
| 207 |
#[test] |
| 208 |
fn tile_at_inverts_tile_center() { |
| 209 |
let map = WorldMap::filled(UVec2::new(32, 128), Terrain::Grass); |
| 210 |
for pos in [UVec2::ZERO, UVec2::new(17, 99), map.size - 1] { |
| 211 |
assert_eq!(map.tile_at(WorldMap::tile_center(pos)), Some(pos)); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
#[test] |
| 216 |
fn tile_at_outside_world_is_none() { |
| 217 |
let map = WorldMap::filled(UVec2::new(32, 128), Terrain::Grass); |
| 218 |
assert_eq!(map.tile_at(Vec2::new(-0.1, 0.0)), None); |
| 219 |
assert_eq!(map.tile_at(map.world_size()), None); |
| 220 |
} |
| 221 |
|
| 222 |
#[test] |
| 223 |
fn trees_slow_movement_but_do_not_block_it() { |
| 224 |
let mut map = WorldMap::filled(UVec2::new(2, 1), Terrain::Grass); |
| 225 |
let open = map.move_cost(IVec2::ZERO).unwrap(); |
| 226 |
map.set_plant(UVec2::ZERO, Some(Plant::Tree)); |
| 227 |
assert!(map.move_cost(IVec2::ZERO).unwrap() > open); |
| 228 |
} |
| 229 |
|
| 230 |
#[test] |
| 231 |
fn walking_uphill_costs_more_than_downhill_or_flat() { |
| 232 |
let mut map = WorldMap::filled(UVec2::new(3, 1), Terrain::Grass); |
| 233 |
map.set_height(UVec2::new(1, 0), 5.0); |
| 234 |
let (low, high, flat) = (IVec2::new(0, 0), IVec2::new(1, 0), IVec2::new(2, 0)); |
| 235 |
let up = map.step_cost(low, high).unwrap(); |
| 236 |
let down = map.step_cost(high, low).unwrap(); |
| 237 |
assert!(up > down); |
| 238 |
assert_eq!(down, map.step_cost(low, flat).unwrap()); |
| 239 |
} |
| 240 |
|
| 241 |
#[test] |
| 242 |
fn rock_and_water_classification() { |
| 243 |
assert!(Terrain::IronOre.is_rock() && Terrain::IronOre.move_cost().is_none()); |
| 244 |
assert!(Terrain::ShallowWater.is_water() && !Terrain::ShallowWater.is_rock()); |
| 245 |
} |
| 246 |
|
| 247 |
#[test] |
| 248 |
fn terrain_index_matches_position_in_all() { |
| 249 |
for (i, t) in Terrain::ALL.iter().enumerate() { |
| 250 |
assert_eq!(t.index() as usize, i); |
| 251 |
} |
| 252 |
for (i, p) in Plant::ALL.iter().enumerate() { |
| 253 |
assert_eq!(p.index() as usize, i); |
| 254 |
} |
| 255 |
} |
| 256 |
} |