use super::errors::GridError; use crate::prelude::*; pub fn grid_start_x(grid_width: u32) -> f32 { -(grid_width as f32 * TILE_SIZE) / 2.0 + TILE_SIZE / 2.0 } pub fn grid_start_y(grid_height: u32) -> f32 { -(grid_height as f32 * TILE_SIZE) / 2.0 + TILE_SIZE / 2.0 } pub fn world_to_grid_coords( world_pos: Vec3, grid_width: u32, grid_height: u32, ) -> Result<(u32, u32), GridError> { let start_x = grid_start_x(grid_width); let start_y = grid_start_y(grid_height); let x = ((world_pos.x - start_x + TILE_SIZE / 2.0) / TILE_SIZE).floor(); let y = ((world_pos.y - start_y + TILE_SIZE / 2.0) / TILE_SIZE).floor(); if x >= grid_width as f32 || y >= grid_height as f32 || x < 0.0 || y < 0.0 { return Err(GridError::OutOfBounds { x: x as i32, y: x as i32, }); } Ok((x as u32, y as u32)) } pub fn grid_to_world_coords( grid_x: u32, grid_y: u32, z: Option, grid_width: u32, grid_height: u32, ) -> Vec3 { Vec3::new( grid_start_x(grid_width) + grid_x as f32 * TILE_SIZE, grid_start_y(grid_height) + grid_y as f32 * TILE_SIZE, z.unwrap_or(0.0), ) }