-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make ChunkData use Vector2 instead of ChunkCoordinates
- Loading branch information
Showing
9 changed files
with
127 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use std::ops::{Add, Div, Mul, Neg, Sub}; | ||
|
||
use num_traits::Float; | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq)] | ||
pub struct Vector2<T> { | ||
pub x: T, | ||
pub z: T, | ||
} | ||
|
||
impl<T: Math + Copy> Vector2<T> { | ||
pub fn new(x: T, z: T) -> Self { | ||
Vector2 { x, z } | ||
} | ||
|
||
pub fn length_squared(&self) -> T { | ||
self.x * self.x + self.z * self.z | ||
} | ||
|
||
pub fn add(&self, other: &Vector2<T>) -> Self { | ||
Vector2 { | ||
x: self.x + other.x, | ||
z: self.z + other.z, | ||
} | ||
} | ||
|
||
pub fn sub(&self, other: &Vector2<T>) -> Self { | ||
Vector2 { | ||
x: self.x - other.x, | ||
z: self.z - other.z, | ||
} | ||
} | ||
|
||
pub fn multiply(self, x: T, z: T) -> Self { | ||
Self { | ||
x: self.x * x, | ||
z: self.z * z, | ||
} | ||
} | ||
} | ||
|
||
impl<T: Math + Copy + Float> Vector2<T> { | ||
pub fn length(&self) -> T { | ||
self.length_squared().sqrt() | ||
} | ||
pub fn normalize(&self) -> Self { | ||
let length = self.length(); | ||
Vector2 { | ||
x: self.x / length, | ||
z: self.z / length, | ||
} | ||
} | ||
} | ||
|
||
impl<T: Math + Copy> Mul<T> for Vector2<T> { | ||
type Output = Self; | ||
|
||
fn mul(self, scalar: T) -> Self { | ||
Self { | ||
x: self.x * scalar, | ||
z: self.z * scalar, | ||
} | ||
} | ||
} | ||
|
||
impl<T: Math + Copy> Add for Vector2<T> { | ||
type Output = Vector2<T>; | ||
fn add(self, rhs: Self) -> Self::Output { | ||
Self { | ||
x: self.x + rhs.x, | ||
z: self.z + rhs.z, | ||
} | ||
} | ||
} | ||
|
||
impl<T: Math + Copy> Neg for Vector2<T> { | ||
type Output = Self; | ||
|
||
fn neg(self) -> Self { | ||
Vector2 { | ||
x: -self.x, | ||
z: -self.z, | ||
} | ||
} | ||
} | ||
|
||
impl<T> From<(T, T)> for Vector2<T> { | ||
fn from((x, z): (T, T)) -> Self { | ||
Vector2 { x, z } | ||
} | ||
} | ||
|
||
pub trait Math: | ||
Mul<Output = Self> | ||
+ Neg<Output = Self> | ||
+ Add<Output = Self> | ||
+ Div<Output = Self> | ||
+ Sub<Output = Self> | ||
+ Sized | ||
{ | ||
} | ||
impl Math for f64 {} | ||
impl Math for f32 {} | ||
impl Math for i32 {} | ||
impl Math for i64 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters