1use std::cmp::Ordering;
2
3use bevy::prelude::{IVec2, Vec2};
4
5use crate::{CHUNK_SIZE, TILE_SIZE};
6
7pub fn align_loc_to_chunk(mut loc: i32) -> i32 {
8 match loc.cmp(&0) {
9 Ordering::Greater => loc / CHUNK_SIZE as i32,
10 Ordering::Less => {
11 let mut result = 0;
12 while loc < 0 {
13 loc += CHUNK_SIZE as i32;
14 result -= 1;
15 }
16 result
17 }
18 Ordering::Equal => 0,
19 }
20}
21
22pub fn chunk_from_location(loc: IVec2) -> IVec2 {
23 IVec2::new(align_loc_to_chunk(loc.x), align_loc_to_chunk(loc.y))
24}
25
26pub fn tile_from_location(loc: IVec2) -> IVec2 {
27 let loc_x = loc.x.rem_euclid(CHUNK_SIZE as i32) as usize;
28 let loc_y = loc.y.rem_euclid(CHUNK_SIZE as i32) as usize;
29
30 IVec2::new(loc_x as i32, loc_y as i32)
31}
32
33pub fn world_unit_to_tile(loc: Vec2) -> IVec2 {
35 IVec2::new(
36 (loc.x).ceil() as i32 + CHUNK_SIZE as i32 / 2 - 1,
37 (loc.y).ceil() as i32 + CHUNK_SIZE as i32 / 2 - 1,
38 )
39}
40
41pub fn world_unit_to_pixel(loc: Vec2) -> (IVec2, IVec2) {
43 let world_loc = world_unit_to_tile(loc);
44
45 let mut leftover_loc = Vec2::new(
46 world_loc.x as f32 - loc.x - 7.0,
47 world_loc.y as f32 - loc.y - 7.0,
48 );
49 leftover_loc.x *= TILE_SIZE as f32;
50 leftover_loc.y *= TILE_SIZE as f32;
51
52 let pixel = IVec2::new(
53 8 - (leftover_loc.x.ceil() as i32),
54 leftover_loc.y.ceil() as i32 - 1,
55 );
56
57 (world_loc, pixel)
58}