|
@@ -2,15 +2,19 @@ use std::{cmp::Ordering::Greater, fmt::{Debug, Display}, ops::{Index, IndexMut}}
|
|
|
use point::Point;
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
+fn p_idx(p: &Point<u32>) -> usize {
|
|
|
+ ((p.y * 32) + p.x) as usize
|
|
|
+}
|
|
|
+
|
|
|
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
|
|
|
pub struct Chunk {
|
|
|
tiles: Vec<u64>
|
|
|
}
|
|
|
|
|
|
-impl Index<Point<u64>> for Chunk {
|
|
|
+impl Index<Point<u32>> for Chunk {
|
|
|
type Output = u64;
|
|
|
- fn index(&self, index: Point<u64>) -> &Self::Output {
|
|
|
- let idx = index.distance(&Point::default()).round_ties_even() as usize;
|
|
|
+ fn index(&self, index: Point<u32>) -> &Self::Output {
|
|
|
+ let idx = p_idx(&index);
|
|
|
if idx >= 32*32 {
|
|
|
panic!("index out of bounds");
|
|
|
}
|
|
@@ -18,9 +22,9 @@ impl Index<Point<u64>> for Chunk {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl IndexMut<Point<u64>> for Chunk {
|
|
|
- fn index_mut(&mut self, index: Point<u64>) -> &mut Self::Output {
|
|
|
- let idx = index.distance(&Point::default()).round_ties_even() as usize;
|
|
|
+impl IndexMut<Point<u32>> for Chunk {
|
|
|
+ fn index_mut(&mut self, index: Point<u32>) -> &mut Self::Output {
|
|
|
+ let idx = p_idx(&index);
|
|
|
if idx >= 32*32 {
|
|
|
panic!("index out of bounds");
|
|
|
}
|
|
@@ -28,10 +32,10 @@ impl IndexMut<Point<u64>> for Chunk {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl Index<&Point<u64>> for Chunk {
|
|
|
+impl Index<&Point<u32>> for Chunk {
|
|
|
type Output = u64;
|
|
|
- fn index(&self, index: &Point<u64>) -> &Self::Output {
|
|
|
- let idx = index.distance(&Point::default()).round_ties_even() as usize;
|
|
|
+ fn index(&self, index: &Point<u32>) -> &Self::Output {
|
|
|
+ let idx = p_idx(index);
|
|
|
if idx >= 32*32 {
|
|
|
panic!("index out of bounds");
|
|
|
}
|
|
@@ -39,9 +43,9 @@ impl Index<&Point<u64>> for Chunk {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl IndexMut<&Point<u64>> for Chunk {
|
|
|
- fn index_mut(&mut self, index: &Point<u64>) -> &mut Self::Output {
|
|
|
- let idx = index.distance(&Point::default()).round_ties_even() as usize;
|
|
|
+impl IndexMut<&Point<u32>> for Chunk {
|
|
|
+ fn index_mut(&mut self, index: &Point<u32>) -> &mut Self::Output {
|
|
|
+ let idx = p_idx(index);
|
|
|
if idx >= 32*32 {
|
|
|
panic!("index out of bounds");
|
|
|
}
|
|
@@ -60,35 +64,35 @@ impl Chunk {
|
|
|
s.tiles.extend_from_slice(&[value; 1024]);
|
|
|
s
|
|
|
}
|
|
|
- pub fn get(&self, pos: &Point<u64>) -> Option<&u64> {
|
|
|
- let idx = pos.distance(&Point::default()).round_ties_even() as usize;
|
|
|
+ pub fn get(&self, pos: &Point<u32>) -> Option<&u64> {
|
|
|
+ let idx = p_idx(pos);
|
|
|
if idx >= 32*32 {
|
|
|
return None;
|
|
|
}
|
|
|
self.tiles.get(idx)
|
|
|
}
|
|
|
- pub fn get_mut(&mut self, pos: &Point<u64>) -> Option<&mut u64> {
|
|
|
- let idx = pos.distance(&Point::default()).round_ties_even() as usize;
|
|
|
+ pub fn get_mut(&mut self, pos: &Point<u32>) -> Option<&mut u64> {
|
|
|
+ let idx = p_idx(pos);
|
|
|
if idx >= 32*32 {
|
|
|
return None;
|
|
|
}
|
|
|
self.tiles.get_mut(idx)
|
|
|
}
|
|
|
- pub fn set(&mut self, pos: &Point<u64>, value: u64) -> bool {
|
|
|
- let idx = pos.distance(&Point::default()).round_ties_even() as usize;
|
|
|
+ pub fn set(&mut self, pos: &Point<u32>, value: u64) -> bool {
|
|
|
+ let idx = p_idx(pos);
|
|
|
if idx >= 32*32 {
|
|
|
return false;
|
|
|
}
|
|
|
self.tiles[idx] = value;
|
|
|
true
|
|
|
}
|
|
|
- pub fn fill(&mut self, pos1: &Point<u64>, pos2: &Point<u64>, value: u64) -> bool {
|
|
|
+ pub fn fill(&mut self, pos1: &Point<u32>, pos2: &Point<u32>, value: u64) -> bool {
|
|
|
let (corn1, corn2) = match pos1.partial_cmp(pos2).unwrap() {
|
|
|
Greater => {
|
|
|
- (pos2.distance(&Point::default()).round_ties_even() as usize, pos1.distance(&Point::default()).round_ties_even() as usize)
|
|
|
+ (p_idx(pos2), p_idx(pos1))
|
|
|
},
|
|
|
_ => {
|
|
|
- (pos1.distance(&Point::default()).round_ties_even() as usize, pos2.distance(&Point::default()).round_ties_even() as usize)
|
|
|
+ (p_idx(pos1), p_idx(pos2))
|
|
|
}
|
|
|
};
|
|
|
if corn1 >= 32*32 || corn2 >= 32*32 {
|