use std::collections::HashMap; use crate::{entity::{Location, EntityId}, site::Site}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Terrain { Void, DeepOcean, Ocean, Beach, Flats, Hills, Mountains, HighMountains, } pub struct WorldCell { pub terrain: Terrain, } pub struct World { pub map: Vec>, pub size: usize, pub sites: HashMap } impl WorldCell { pub fn new(terrain: Terrain) -> WorldCell { WorldCell { terrain: terrain, } } } impl World { pub fn new(size: usize) -> World { let mut map = Vec::new(); for _ in 0..size { let mut row = Vec::new(); for _ in 0..size { row.push(WorldCell::new(Terrain::Void)); } map.push(row); } World { map: map, size: size, sites: HashMap::new(), } } pub fn add_site(&mut self, mut site: Site) -> EntityId{ let id = self.sites.len() as EntityId; site.entity.id = id; self.sites.insert(site.entity.loc, site); return id; } pub fn get_site_location(&self, site_id: EntityId) -> Location { for (loc, site) in self.sites.iter() { if site.entity.id == site_id { return *loc; } } panic!("Town not found"); } pub fn get_site_at(&self, pos: Location) -> Option<&Site> { return self.sites.get(&pos); } } impl Terrain { pub fn from_height(height: f64) -> Terrain { if height < -2000.0 { Terrain::DeepOcean } else if height < 0.0 { Terrain::Ocean } else if height < 50.0 { Terrain::Beach } else if height < 1000.0 { Terrain::Flats } else if height < 2000.0 { Terrain::Hills } else if height < 3500.0 { Terrain::Mountains } else { Terrain::HighMountains } } pub fn to_color(self) -> [u8; 3] { match self { Terrain::Void => [0, 0, 0], Terrain::DeepOcean => [20, 60, 255], Terrain::Ocean => [20, 120, 255], Terrain::Beach => [255, 255, 100], Terrain::Flats => [0, 204, 0], Terrain::Hills => [102, 153, 0], Terrain::Mountains => [153, 153, 102], Terrain::HighMountains => [230, 230, 230], } } }