veloren/world/src/land.rs

40 lines
1.2 KiB
Rust
Raw Normal View History

2021-02-17 01:47:16 +00:00
use crate::sim;
2021-03-05 01:23:11 +00:00
use common::{terrain::TerrainChunkSize, vol::RectVolSize};
2021-02-17 01:47:16 +00:00
use vek::*;
2021-03-05 01:23:11 +00:00
/// A wrapper type that may contain a reference to a generated world. If not,
/// default values will be provided.
2021-02-17 01:47:16 +00:00
pub struct Land<'a> {
sim: Option<&'a sim::WorldSim>,
}
impl<'a> Land<'a> {
2021-03-05 01:23:11 +00:00
pub fn empty() -> Self { Self { sim: None } }
2021-02-17 01:47:16 +00:00
2021-03-05 01:23:11 +00:00
pub fn from_sim(sim: &'a sim::WorldSim) -> Self { Self { sim: Some(sim) } }
2021-02-17 01:47:16 +00:00
pub fn get_alt_approx(&self, wpos: Vec2<i32>) -> f32 {
2021-03-05 01:23:11 +00:00
self.sim
.and_then(|sim| sim.get_alt_approx(wpos))
.unwrap_or(0.0)
2021-02-17 01:47:16 +00:00
}
2021-02-23 12:42:45 +00:00
pub fn get_gradient_approx(&self, wpos: Vec2<i32>) -> f32 {
self.sim
2022-01-28 15:17:56 +00:00
.and_then(|sim| sim.get_gradient_approx(self.wpos_chunk_pos(wpos)))
2021-02-23 12:42:45 +00:00
.unwrap_or(0.0)
}
2022-01-28 15:17:56 +00:00
pub fn wpos_chunk_pos(&self, wpos: Vec2<i32>) -> Vec2<i32> {
wpos.map2(TerrainChunkSize::RECT_SIZE, |e, sz| e.div_euclid(sz as i32))
}
pub fn get_chunk(&self, chunk_pos: Vec2<i32>) -> Option<&sim::SimChunk> {
self.sim.and_then(|sim| sim.get(chunk_pos))
}
pub fn get_chunk_wpos(&self, wpos: Vec2<i32>) -> Option<&sim::SimChunk> {
2021-02-23 12:42:45 +00:00
self.sim.and_then(|sim| sim.get_wpos(wpos))
}
2021-02-17 01:47:16 +00:00
}