tavern_keeper/src/world.rs

120 lines
2.6 KiB
Rust

use std::rc::Rc;
use rand::prelude::*;
use crate::generators::TownNameGenerator;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Terrain {
Void,
DeepOcean,
Ocean,
Beach,
Flats,
Hills,
Mountains,
HighMountains,
}
pub struct Town {
pub name: String,
}
pub enum Structure {
Town(Rc<Town>),
}
pub struct WorldCell {
pub terrain: Terrain,
pub structure: Option<Structure>,
}
pub struct World {
pub map: Vec<Vec<WorldCell>>,
pub size: usize,
}
impl WorldCell {
pub fn new(terrain: Terrain) -> WorldCell {
WorldCell {
terrain: terrain,
structure: None,
}
}
}
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,
}
}
pub fn add_structure(&mut self, x: usize, y: usize, structure: Structure) {
self.map[x][y].structure = Some(structure);
}
pub fn get_town_location(&self, town: &Rc<Town>) -> [usize; 2] {
for x in 0..self.size {
for y in 0..self.size {
if let Some(Structure::Town(t)) = &self.map[x][y].structure {
if Rc::ptr_eq(&t, town) {
return [x, y];
}
}
}
}
panic!("Town not found");
}
}
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],
}
}
}
impl Town {
pub fn new() -> Town {
Town {
name: TownNameGenerator::name(),
}
}
}