veloren/world/src/site/mod.rs

121 lines
2.6 KiB
Rust
Raw Normal View History

mod settlement;
2020-04-15 19:41:40 +00:00
mod dungeon;
// Reexports
pub use self::settlement::Settlement;
2020-04-15 19:41:40 +00:00
pub use self::dungeon::Dungeon;
use crate::{
column::ColumnSample,
util::{Grid, Sampler},
};
use common::{
terrain::Block,
2020-04-15 19:41:40 +00:00
vol::{Vox, BaseVol, RectSizedVol, ReadVol, WriteVol},
};
use std::{fmt, sync::Arc};
use vek::*;
2020-04-15 19:41:40 +00:00
#[derive(Copy, Clone)]
pub struct BlockMask {
block: Block,
priority: i32,
}
impl BlockMask {
pub fn new(block: Block, priority: i32) -> Self {
Self { block, priority }
}
pub fn nothing() -> Self {
Self {
block: Block::empty(),
priority: 0,
}
}
pub fn with_priority(mut self, priority: i32) -> Self {
self.priority = priority;
self
}
pub fn resolve_with(self, other: Self) -> Self {
if self.priority >= other.priority {
self
} else {
other
}
}
pub fn finish(self) -> Option<Block> {
if self.priority > 0 {
Some(self.block)
} else {
None
}
}
}
pub struct SpawnRules {
pub trees: bool,
}
2020-04-15 19:41:40 +00:00
impl Default for SpawnRules {
fn default() -> Self {
Self {
trees: true,
}
}
}
#[derive(Clone)]
pub enum Site {
Settlement(Arc<Settlement>),
2020-04-15 19:41:40 +00:00
Dungeon(Arc<Dungeon>),
}
impl Site {
pub fn radius(&self) -> f32 {
match self {
Site::Settlement(settlement) => settlement.radius(),
2020-04-15 19:41:40 +00:00
Site::Dungeon(dungeon) => dungeon.radius(),
}
}
pub fn spawn_rules(&self, wpos: Vec2<i32>) -> SpawnRules {
match self {
2020-04-15 19:41:40 +00:00
Site::Settlement(s) => s.spawn_rules(wpos),
Site::Dungeon(d) => d.spawn_rules(wpos),
}
}
pub fn apply_to<'a>(
&'a self,
wpos2d: Vec2<i32>,
get_column: impl FnMut(Vec2<i32>) -> Option<&'a ColumnSample<'a>>,
2020-04-11 19:04:19 +00:00
vol: &mut (impl BaseVol<Vox = Block> + RectSizedVol + ReadVol + WriteVol),
) {
2020-02-08 19:58:42 +00:00
match self {
Site::Settlement(settlement) => settlement.apply_to(wpos2d, get_column, vol),
2020-04-15 19:41:40 +00:00
Site::Dungeon(dungeon) => dungeon.apply_to(wpos2d, get_column, vol),
2020-02-08 19:58:42 +00:00
}
}
}
impl From<Settlement> for Site {
fn from(settlement: Settlement) -> Self { Site::Settlement(Arc::new(settlement)) }
}
2020-04-15 19:41:40 +00:00
impl From<Dungeon> for Site {
fn from(dungeon: Dungeon) -> Self { Site::Dungeon(Arc::new(dungeon)) }
}
impl fmt::Debug for Site {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Site::Settlement(_) => write!(f, "Settlement"),
2020-04-15 19:41:40 +00:00
Site::Dungeon(_) => write!(f, "Dungeon"),
}
}
}