use nadir_assets::Handle;
use nadir_collision::{ColliderKind, Shape};
use nadir_core::{Color, IVec2, Rect, UVec2, Vec2};
use nadir_debug::Ui;
use nadir_ecs::World;
use nadir_render::{
Camera2D, Font, RenderFrame, SortMode, SpriteDraw, TextDraw, Texture, TileLayer, TileLayers,
};
use crate::{Collider, Collisions, Parent, PrevTransform, Transform};
pub(crate) const SOLID: Color = Color::rgb(0, 220, 255);
pub(crate) const TRIGGER: Color = Color::rgb(255, 96, 255);
pub(crate) const GRID: Color = Color::rgba(0, 220, 255, 56);
pub(crate) const HASH: Color = Color::rgba(255, 255, 255, 160);
pub(crate) const CHUNK: Color = Color::rgb(255, 160, 0);
pub(crate) const CAMERA: Color = Color::rgb(255, 64, 64);
const CROSS: f32 = 6.0;
const SIDES: u32 = 24;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Overlays {
pub(crate) colliders: bool,
pub(crate) triggers: bool,
pub(crate) grid: bool,
pub(crate) hash: bool,
pub(crate) chunks: bool,
pub(crate) camera: bool,
}
impl Overlays {
pub(crate) fn new() -> Self {
Self {
colliders: true,
triggers: true,
grid: true,
hash: false,
chunks: false,
camera: false,
}
}
pub(crate) fn show(&mut self, ui: &mut Ui<'_>) {
let rows: [(&str, Color, &mut bool); 6] = [
("Colliders##colliders", SOLID, &mut self.colliders),
("Triggers##triggers", TRIGGER, &mut self.triggers),
("Tile grid##grid", Color::rgb(0, 220, 255), &mut self.grid),
(
"Spatial hash##hash",
Color::rgb(255, 255, 255),
&mut self.hash,
),
("Tile chunks##chunks", CHUNK, &mut self.chunks),
("Camera##camera", CAMERA, &mut self.camera),
];
for (text, color, on) in rows {
let (name, id) = text.split_once("##").unwrap_or((text, text));
ui.horizontal(|ui| {
ui.with_id(id, |ui| {
ui.checkbox("##on", on);
});
ui.label_colored(color, name);
});
}
}
pub(crate) fn draw(
&self,
world: &World,
white: Handle<Texture>,
font: Handle<Font>,
virtual_resolution: UVec2,
alpha: f32,
) {
let mut frame = world.resource_mut::<RenderFrame>();
let RenderFrame {
camera,
sprites,
texts,
tile_layers,
..
} = &mut *frame;
let (view, zoom) = view(camera, virtual_resolution);
let mut pen = Pen {
sprites,
white,
width: 1.0 / zoom,
};
if self.colliders || self.triggers {
let transforms = world.read::<Transform>();
let previous = world.read::<PrevTransform>();
let parents = world.read::<Parent>();
let drawn_at = |entity| {
let current = transforms.get(entity)?.position;
Some(
previous
.get(entity)
.map_or(current, |last| last.0.position.lerp(current, alpha)),
)
};
for (entity, collider) in world.read::<Collider>().iter() {
let collider = collider.0;
let (on, color) = match collider.kind {
ColliderKind::Solid => (self.colliders, SOLID),
ColliderKind::Trigger => (self.triggers, TRIGGER),
};
if !on || !transforms.contains(entity) {
continue;
}
let parented = if parents.contains(entity) {
crate::glue::root(&parents, entity)
.and_then(|(root, offset)| Some(drawn_at(root)? + offset))
} else {
None
};
let Some(at) = parented.or_else(|| drawn_at(entity)) else {
continue;
};
if !touches(collider.bounds(at), view) {
continue;
}
let center = collider.center(at);
match collider.shape {
Shape::Aabb { half } => {
pen.outline(Rect::new(center - half, center + half), color)
}
Shape::Circle { radius } => pen.circle(center, radius, color),
}
}
}
if self.grid || self.hash {
let collisions = world.resource::<Collisions>();
if self.grid {
let grid = collisions.grid();
if grid.width() > 0 && grid.height() > 0 && grid.cell_size() > 0.0 {
let last = IVec2::new(
i32::try_from(grid.width() - 1).unwrap_or(i32::MAX),
i32::try_from(grid.height() - 1).unwrap_or(i32::MAX),
);
let first = grid.cell_at(view.min).max(IVec2::ZERO);
let end = grid.cell_at(view.max).min(last);
for y in first.y..=end.y {
for x in first.x..=end.x {
let cell = IVec2::new(x, y);
if grid.is_solid(cell) {
pen.fill(grid.cell_rect(cell), GRID);
}
}
}
}
}
if self.hash {
collisions.world().hash_cells(|cell, count| {
if !touches(cell, view) {
return;
}
pen.outline(cell, HASH);
let mut draw = TextDraw::new(font, cell.min + Vec2::splat(2.0 / zoom));
draw.color = HASH;
draw.layer = u8::MAX;
draw.sort = SortMode::Fixed(u32::MAX);
draw.layout.markup = false;
texts.write(draw, format_args!("{count}"));
});
}
}
if self.chunks {
let layers = world.resource::<TileLayers>();
for drawn in tile_layers.iter() {
let Some(layer) = layers.get(drawn.tiles) else {
continue;
};
chunks(layer, view, |rect| pen.outline(rect, CHUNK));
}
}
if self.camera {
let arm = CROSS / zoom;
let at = camera.position;
pen.line(at - Vec2::new(arm, 0.0), at + Vec2::new(arm, 0.0), CAMERA);
pen.line(at - Vec2::new(0.0, arm), at + Vec2::new(0.0, arm), CAMERA);
pen.outline(view, CAMERA);
}
}
}
pub(crate) fn view(camera: &Camera2D, virtual_resolution: UVec2) -> (Rect, f32) {
let zoom = (camera.zoom + 0.5).floor().max(1.0);
let zoom = if zoom.is_finite() { zoom } else { 1.0 };
let half = virtual_resolution.max(UVec2::ONE).as_vec2() / (2.0 * zoom);
let center = camera.position + camera.shake;
(Rect::new(center - half, center + half), zoom)
}
pub(crate) fn chunks(layer: &TileLayer, view: Rect, mut each: impl FnMut(Rect)) {
let tile = layer.tile_size();
let side = TileLayer::CHUNK as f32 * tile;
if !(side > 0.0 && side.is_finite()) {
return;
}
let origin = layer.origin();
let end = origin + layer.size().as_vec2() * tile;
let count = layer.chunks();
for y in 0..count.y {
for x in 0..count.x {
let min = origin + Vec2::new(x as f32, y as f32) * side;
let rect = Rect::new(min, (min + Vec2::splat(side)).min(end));
if touches(rect, view) {
each(rect);
}
}
}
}
fn touches(a: Rect, b: Rect) -> bool {
a.min.x <= b.max.x && a.max.x >= b.min.x && a.min.y <= b.max.y && a.max.y >= b.min.y
}
struct Pen<'a> {
sprites: &'a mut Vec<SpriteDraw>,
white: Handle<Texture>,
width: f32,
}
impl Pen<'_> {
fn snap(&self, point: Vec2) -> Vec2 {
let zoom = 1.0 / self.width;
(point * zoom + Vec2::splat(0.5)).floor() / zoom
}
fn fill(&mut self, rect: Rect, color: Color) {
let (min, max) = (self.snap(rect.min), self.snap(rect.max));
self.sprite(min, max - min, color);
}
fn sprite(&mut self, at: Vec2, size: Vec2, color: Color) {
let mut draw = SpriteDraw::new(self.white, at, size);
draw.pivot = Vec2::ZERO;
draw.color = color;
draw.layer = u8::MAX;
draw.sort = SortMode::Fixed(u32::MAX);
self.sprites.push(draw);
}
fn outline(&mut self, rect: Rect, color: Color) {
let (min, max) = (rect.min.min(rect.max), rect.min.max(rect.max));
let (min, max) = (self.snap(min), self.snap(max));
let w = self.width;
if max.x - min.x < 2.0 * w || max.y - min.y < 2.0 * w {
self.sprite(min, (max - min).max(Vec2::splat(w)), color);
return;
}
self.sprite(min, Vec2::new(max.x - min.x, w), color);
self.sprite(
Vec2::new(min.x, max.y - w),
Vec2::new(max.x - min.x, w),
color,
);
let side = Vec2::new(w, max.y - min.y - 2.0 * w);
self.sprite(Vec2::new(min.x, min.y + w), side, color);
self.sprite(Vec2::new(max.x - w, min.y + w), side, color);
}
fn line(&mut self, from: Vec2, to: Vec2, color: Color) {
let along = to - from;
let length = along.length();
if !(length > 0.0 && length.is_finite()) {
return;
}
let mut draw = SpriteDraw::new(self.white, from, Vec2::new(length, self.width));
draw.pivot = Vec2::new(0.0, 0.5);
draw.rotation = along.y.atan2(along.x);
draw.color = color;
draw.layer = u8::MAX;
draw.sort = SortMode::Fixed(u32::MAX);
self.sprites.push(draw);
}
fn circle(&mut self, center: Vec2, radius: f32, color: Color) {
let point = |side: u32| {
let angle = side as f32 / SIDES as f32 * std::f32::consts::TAU;
center + Vec2::new(angle.cos(), angle.sin()) * radius
};
for side in 0..SIDES {
self.line(point(side), point(side + 1), color);
}
}
}