use std::fmt;
use std::sync::atomic::{AtomicU64, Ordering};
use nadir_assets::Handle;
use nadir_core::{GenArena, IVec2, Rect, UVec2, Vec2};
use crate::{Flip, Texture};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TileSet {
pub texture: Handle<Texture>,
pub columns: u16,
pub rows: u16,
}
impl TileSet {
pub fn block_uv(&self, index: u16, size: UVec2) -> Rect {
let columns = u32::from(self.columns.max(1));
let rows = u32::from(self.rows.max(1));
let index = u32::from(index) % (columns * rows);
let corner = UVec2::new(index % columns, index / columns).as_vec2();
let grid = UVec2::new(columns, rows).as_vec2();
Rect::new(corner / grid, (corner + size.as_vec2()) / grid)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Tile {
pub index: u16,
pub frames: u16,
pub frame_ms: u16,
pub flip: Flip,
}
impl Tile {
pub const fn new(index: u16) -> Self {
Self::animated(index, 1, 0)
}
pub const fn animated(index: u16, frames: u16, frame_ms: u16) -> Self {
Self {
index,
frames,
frame_ms,
flip: Flip { x: false, y: false },
}
}
}
pub const TILE_INSTANCE_FLOATS: usize = 6;
static REVISIONS: AtomicU64 = AtomicU64::new(0);
fn next_revision() -> u64 {
REVISIONS.fetch_add(1, Ordering::Relaxed) + 1
}
#[derive(Clone, Debug)]
pub struct TileLayer {
tileset: TileSet,
size: UVec2,
tile_size: f32,
origin: Vec2,
cells: Vec<Option<Tile>>,
revisions: Vec<u64>,
}
impl TileLayer {
pub const CHUNK: u32 = 32;
pub fn new(tileset: TileSet, size: UVec2, tile_size: f32, origin: Vec2) -> Self {
assert!(
tile_size.is_finite() && tile_size > 0.0,
"a tile layer's tiles must have a finite size above 0, not {tile_size}"
);
let cells = usize::try_from(u64::from(size.x) * u64::from(size.y))
.expect("a tile layer has no more cells than memory can count");
let chunks = Self::chunks_of(size);
let revision = next_revision();
Self {
tileset,
size,
tile_size,
origin,
cells: vec![None; cells],
revisions: vec![revision; (chunks.x * chunks.y) as usize],
}
}
pub fn tileset(&self) -> TileSet {
self.tileset
}
pub fn size(&self) -> UVec2 {
self.size
}
pub fn tile_size(&self) -> f32 {
self.tile_size
}
pub fn origin(&self) -> Vec2 {
self.origin
}
pub fn chunks(&self) -> UVec2 {
Self::chunks_of(self.size)
}
fn chunks_of(size: UVec2) -> UVec2 {
UVec2::new(size.x.div_ceil(Self::CHUNK), size.y.div_ceil(Self::CHUNK))
}
pub fn get(&self, cell: IVec2) -> Option<Tile> {
self.index(cell).and_then(|index| self.cells[index])
}
pub fn set(&mut self, cell: IVec2, tile: Option<Tile>) {
let Some(index) = self.index(cell) else {
panic!(
"cell ({}, {}) is outside the {} × {} tile layer",
cell.x, cell.y, self.size.x, self.size.y
);
};
if self.cells[index] != tile {
self.cells[index] = tile;
let chunk = self.chunk_index(UVec2::new(
cell.x as u32 / Self::CHUNK,
cell.y as u32 / Self::CHUNK,
));
self.revisions[chunk] = next_revision();
}
}
pub fn pack_chunk(&self, chunk: UVec2, out: &mut Vec<f32>) -> u32 {
let chunks = self.chunks();
if chunk.x >= chunks.x || chunk.y >= chunks.y {
return 0;
}
let first = chunk * Self::CHUNK;
let end = (first + UVec2::splat(Self::CHUNK)).min(self.size);
let mut count = 0;
for y in first.y..end.y {
let row = y as usize * self.size.x as usize;
for x in first.x..end.x {
let Some(tile) = self.cells[row + x as usize] else {
continue;
};
let flip = u8::from(tile.flip.x) | u8::from(tile.flip.y) << 1;
out.extend_from_slice(&[
x as f32,
y as f32,
f32::from(tile.index),
f32::from(tile.frames),
f32::from(tile.frame_ms),
f32::from(flip),
]);
count += 1;
}
}
count
}
pub(crate) fn revision(&self, chunk: usize) -> u64 {
self.revisions[chunk]
}
pub(crate) fn chunk_index(&self, chunk: UVec2) -> usize {
chunk.y as usize * self.chunks().x as usize + chunk.x as usize
}
fn index(&self, cell: IVec2) -> Option<usize> {
let x = u32::try_from(cell.x).ok().filter(|&x| x < self.size.x)?;
let y = u32::try_from(cell.y).ok().filter(|&y| y < self.size.y)?;
Some(y as usize * self.size.x as usize + x as usize)
}
}
#[derive(Default)]
pub struct TileLayers {
layers: GenArena<TileLayer>,
}
impl TileLayers {
pub fn new() -> Self {
Self::default()
}
pub fn add(&mut self, layer: TileLayer) -> Handle<TileLayer> {
Handle::new(self.layers.insert(layer))
}
pub fn get(&self, handle: Handle<TileLayer>) -> Option<&TileLayer> {
self.layers.get(handle.id())
}
pub fn get_mut(&mut self, handle: Handle<TileLayer>) -> Option<&mut TileLayer> {
self.layers.get_mut(handle.id())
}
pub fn remove(&mut self, handle: Handle<TileLayer>) -> Option<TileLayer> {
self.layers.remove(handle.id())
}
pub fn len(&self) -> usize {
self.layers.len()
}
pub fn is_empty(&self) -> bool {
self.layers.is_empty()
}
}
impl fmt::Debug for TileLayers {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TileLayers")
.field("len", &self.layers.len())
.finish_non_exhaustive()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TileLayerDraw {
pub tiles: Handle<TileLayer>,
pub layer: u8,
pub depth: u32,
}
impl TileLayerDraw {
pub const fn new(tiles: Handle<TileLayer>) -> Self {
Self {
tiles,
layer: 0,
depth: 0,
}
}
}
#[cfg(test)]
mod tests {
use nadir_core::GenIndex;
use super::*;
fn tileset() -> TileSet {
TileSet {
texture: Handle::new(GenIndex::new(0, std::num::NonZeroU32::MIN)),
columns: 8,
rows: 4,
}
}
fn layer(width: u32, height: u32) -> TileLayer {
TileLayer::new(tileset(), UVec2::new(width, height), 16.0, Vec2::ZERO)
}
fn revisions(layer: &TileLayer) -> Vec<u64> {
layer.revisions.clone()
}
#[test]
fn a_layer_is_cut_into_chunks_the_last_ones_short() {
assert_eq!(layer(64, 48).chunks(), UVec2::new(2, 2));
assert_eq!(layer(33, 1).chunks(), UVec2::new(2, 1));
assert_eq!(layer(32, 32).chunks(), UVec2::new(1, 1));
assert_eq!(layer(0, 10).chunks(), UVec2::new(0, 1));
assert!(layer(0, 10).revisions.is_empty());
}
#[test]
fn a_change_marks_only_its_own_chunk() {
let mut layer = layer(64, 48);
let before = revisions(&layer);
layer.set(IVec2::new(31, 0), Some(Tile::new(3)));
let after = revisions(&layer);
assert_ne!(after[0], before[0]);
assert_eq!(after[1..], before[1..]);
layer.set(IVec2::new(32, 0), Some(Tile::new(3)));
let later = revisions(&layer);
assert_eq!(later[0], after[0]);
assert_ne!(later[1], after[1]);
assert_eq!(later[2..], after[2..]);
layer.set(IVec2::new(63, 47), Some(Tile::new(1)));
assert_ne!(revisions(&layer)[3], later[3]);
assert_eq!(layer.get(IVec2::new(63, 47)), Some(Tile::new(1)));
}
#[test]
fn setting_what_is_there_changes_nothing() {
let mut layer = layer(40, 40);
layer.set(IVec2::new(5, 5), Some(Tile::new(2)));
let before = revisions(&layer);
layer.set(IVec2::new(5, 5), Some(Tile::new(2)));
layer.set(IVec2::new(6, 5), None);
assert_eq!(revisions(&layer), before);
layer.set(IVec2::new(5, 5), None);
assert_ne!(revisions(&layer)[0], before[0]);
assert_eq!(layer.get(IVec2::new(5, 5)), None);
}
#[test]
fn cells_outside_are_empty() {
let layer = layer(4, 4);
for cell in [(-1, 0), (0, -1), (4, 0), (0, 4), (i32::MIN, i32::MAX)] {
assert_eq!(layer.get(IVec2::new(cell.0, cell.1)), None, "{cell:?}");
}
}
#[test]
#[should_panic(expected = "outside the 4 × 4 tile layer")]
fn setting_a_cell_outside_panics() {
layer(4, 4).set(IVec2::new(4, 0), Some(Tile::new(0)));
}
#[test]
#[should_panic(expected = "finite size above 0")]
fn tiles_have_a_size() {
TileLayer::new(tileset(), UVec2::ONE, 0.0, Vec2::ZERO);
}
#[test]
fn no_two_layers_share_a_revision_however_they_were_made() {
let fill = |index: u16| {
let mut layer = layer(40, 40);
for y in 0..40 {
for x in 0..40 {
layer.set(IVec2::new(x, y), Some(Tile::new(index)));
}
}
layer
};
let (a, b) = (fill(1), fill(2));
for (a, b) in a.revisions.iter().zip(&b.revisions) {
assert_ne!(a, b);
}
let (c, d) = (layer(40, 40), layer(40, 40));
assert_ne!(c.revisions, d.revisions);
assert_eq!(a.clone().revisions, a.revisions);
assert!(!a.revisions.contains(&0));
}
#[test]
fn two_layers_changed_in_turn_never_reach_the_same_revision() {
let (mut a, mut b) = (layer(4, 4), layer(4, 4));
let mut seen_a = std::collections::HashSet::new();
let mut seen_b = std::collections::HashSet::new();
for turn in 0..50_000u16 {
a.set(IVec2::ZERO, Some(Tile::new(turn % 2)));
seen_a.insert(a.revisions[0]);
if turn % 3 != 0 {
b.set(IVec2::ZERO, Some(Tile::new(turn % 2)));
}
seen_b.insert(b.revisions[0]);
}
assert!(seen_a.is_disjoint(&seen_b));
}
#[test]
fn a_chunk_packs_its_tiles_row_by_row() {
let mut layer = layer(40, 34);
let mut flipped = Tile::animated(9, 4, 120);
flipped.flip = Flip { x: true, y: true };
layer.set(IVec2::new(33, 1), Some(flipped));
layer.set(IVec2::new(32, 0), Some(Tile::new(7)));
layer.set(IVec2::new(0, 0), Some(Tile::new(1)));
layer.set(IVec2::new(39, 33), Some(Tile::new(2)));
let mut out = vec![-1.0];
assert_eq!(layer.pack_chunk(UVec2::new(1, 0), &mut out), 2);
assert_eq!(
out,
[
-1.0, 32.0, 0.0, 7.0, 1.0, 0.0, 0.0, 33.0, 1.0, 9.0, 4.0, 120.0, 3.0,
]
);
out.clear();
assert_eq!(layer.pack_chunk(UVec2::new(1, 1), &mut out), 1);
assert_eq!(out, [39.0, 33.0, 2.0, 1.0, 0.0, 0.0]);
out.clear();
assert_eq!(layer.pack_chunk(UVec2::new(2, 0), &mut out), 0);
assert_eq!(layer.pack_chunk(UVec2::new(0, 2), &mut out), 0);
assert!(out.is_empty());
}
#[test]
fn a_block_of_tiles_is_its_part_of_the_atlas() {
let set = tileset();
assert_eq!(
set.block_uv(11, UVec2::ONE),
Rect::new(Vec2::new(3.0 / 8.0, 0.25), Vec2::new(0.5, 0.5))
);
assert_eq!(
set.block_uv(4, UVec2::new(2, 3)),
Rect::new(Vec2::new(0.5, 0.0), Vec2::new(0.75, 0.75))
);
assert_eq!(set.block_uv(33, UVec2::ONE), set.block_uv(1, UVec2::ONE));
let single = TileSet {
columns: 0,
rows: 0,
..set
};
assert_eq!(
single.block_uv(5, UVec2::ONE),
Rect::new(Vec2::ZERO, Vec2::ONE)
);
}
#[test]
fn layers_come_and_go_by_handle() {
let mut layers = TileLayers::new();
let a = layers.add(layer(2, 2));
let b = layers.add(layer(3, 3));
assert_eq!(layers.len(), 2);
assert_eq!(layers.get(b).map(TileLayer::size), Some(UVec2::new(3, 3)));
layers
.get_mut(a)
.expect("a is there")
.set(IVec2::ZERO, Some(Tile::new(1)));
assert!(layers.remove(a).is_some());
assert!(layers.get(a).is_none());
let c = layers.add(layer(2, 2));
assert_ne!(a, c);
assert!(layers.get(a).is_none());
assert_eq!(format!("{layers:?}"), "TileLayers { len: 2, .. }");
}
}