tavern_keeper/src/world.rs

129 lines
3.0 KiB
Rust

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<Vec<WorldCell>>,
pub size: usize,
pub sites: HashMap<Location, Site>
}
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 u32;
site.entity.id.1 = id;
let id = site.entity.id.clone();
self.sites.insert(site.loc, site);
id
}
pub fn update_site(&mut self, site: Site) {
self.sites.insert(site.loc, site);
}
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);
}
pub fn get_site(&self, id: EntityId) -> Option<&Site> {
for site in self.sites.values() {
if site.entity.id == id {
return Some(site);
}
}
None
}
pub fn add_coins(&mut self, id: EntityId, coins: u32) {
for (_, mut site) in self.sites.iter_mut() {
if site.entity.id == id {
site.coins += coins;
break;
}
}
}
}
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],
}
}
}