| 1 | use crate::sprites::Spr; |
| 2 | use bevy::prelude::*; |
| 3 | use bevy::sprite::Anchor; |
| 4 | |
| 5 | pub const TW: f32 = 64.0; |
| 6 | pub const TH: f32 = 32.0; |
| 7 | pub const PX: f32 = 2.0; |
| 8 | |
| 9 | pub fn to_screen(p: Vec2) -> Vec2 { |
| 10 | Vec2::new((p.x - p.y) * TW, -(p.x + p.y) * TH) |
| 11 | } |
| 12 | |
| 13 | pub fn to_grid(s: Vec2) -> Vec2 { |
| 14 | let a = s.x / TW; |
| 15 | let b = -s.y / TH; |
| 16 | Vec2::new((a + b) * 0.5, (b - a) * 0.5) |
| 17 | } |
| 18 | |
| 19 | pub fn depth(p: Vec2) -> f32 { |
| 20 | (p.x + p.y) * 0.01 |
| 21 | } |
| 22 | |
| 23 | pub fn cell(p: Vec2) -> IVec2 { |
| 24 | IVec2::new(p.x.floor() as i32, p.y.floor() as i32) |
| 25 | } |
| 26 | |
| 27 | pub fn anchor(s: &Spr) -> Anchor { |
| 28 | Anchor(Vec2::new(s.origin.0 / s.cell.0 as f32 - 0.5, 0.5 - s.origin.1 / s.cell.1 as f32)) |
| 29 | } |
| 30 | |
| 31 | pub fn hash(x: i32, y: i32, salt: u32) -> f32 { |
| 32 | let mut h = (x as u32).wrapping_mul(0x8da6b343) ^ (y as u32).wrapping_mul(0xd8163841) ^ salt.wrapping_mul(0xcb1ab31f); |
| 33 | h ^= h >> 13; |
| 34 | h = h.wrapping_mul(0x5bd1e995); |
| 35 | h ^= h >> 15; |
| 36 | (h & 0xffffff) as f32 / 16777216.0 |
| 37 | } |