veloren/world/src/site2/tile.rs

71 lines
1.7 KiB
Rust
Raw Normal View History

2020-12-26 15:53:06 +00:00
use super::*;
2021-02-04 12:47:46 +00:00
pub const TILE_SIZE: u32 = 7;
pub const ZONE_SIZE: u32 = 16;
pub const ZONE_RADIUS: u32 = 16;
pub const TILE_RADIUS: u32 = ZONE_SIZE * ZONE_RADIUS;
pub const MAX_BLOCK_RADIUS: u32 = TILE_SIZE * TILE_RADIUS;
2020-12-26 15:53:06 +00:00
pub struct TileGrid {
zones: Grid<Option<Grid<Option<Tile>>>>,
}
2021-02-04 12:47:46 +00:00
impl Default for TileGrid {
fn default() -> Self {
2020-12-26 15:53:06 +00:00
Self {
zones: Grid::populate_from(Vec2::broadcast(ZONE_RADIUS as i32 * 2 + 1), |_| None),
}
}
2021-02-04 12:47:46 +00:00
}
2020-12-26 15:53:06 +00:00
2021-02-04 12:47:46 +00:00
impl TileGrid {
2020-12-26 15:53:06 +00:00
pub fn get(&self, tpos: Vec2<i32>) -> Option<&Tile> {
let tpos = tpos + TILE_RADIUS as i32;
self.zones
.get(tpos)
2021-02-06 23:53:25 +00:00
.and_then(|zone| {
zone.as_ref()?
.get(tpos.map(|e| e.rem_euclid(ZONE_SIZE as i32)))
})
2020-12-26 15:53:06 +00:00
.and_then(|tile| tile.as_ref())
}
pub fn get_mut(&mut self, tpos: Vec2<i32>) -> Option<&mut Tile> {
let tpos = tpos + TILE_RADIUS as i32;
2021-02-06 23:53:25 +00:00
self.zones.get_mut(tpos).and_then(|zone| {
zone.get_or_insert_with(|| {
Grid::populate_from(Vec2::broadcast(ZONE_RADIUS as i32 * 2 + 1), |_| None)
})
.get_mut(tpos.map(|e| e.rem_euclid(ZONE_SIZE as i32)))
.map(|tile| tile.get_or_insert_with(|| Tile::empty()))
})
2020-12-26 15:53:06 +00:00
}
2021-02-04 12:47:46 +00:00
pub fn find_near(&self, tpos: Vec2<i32>, f: impl Fn(&Tile) -> bool) -> Option<Vec2<i32>> {
None
}
}
#[derive(PartialEq)]
pub enum TileKind {
Empty,
Farmland,
Building { levels: u32 },
2020-12-26 15:53:06 +00:00
}
pub struct Tile {
2021-02-04 12:47:46 +00:00
kind: TileKind,
2020-12-26 15:53:06 +00:00
plot: Option<Id<Plot>>,
}
impl Tile {
pub fn empty() -> Self {
Self {
2021-02-04 12:47:46 +00:00
kind: TileKind::Empty,
2020-12-26 15:53:06 +00:00
plot: None,
}
}
2021-02-04 12:47:46 +00:00
2021-02-06 23:53:25 +00:00
pub fn is_empty(&self) -> bool { self.kind == TileKind::Empty }
2020-12-26 15:53:06 +00:00
}