Point2D -> Point
authorTomas Wenström <tomas.wenstrom@gmail.com>
Sat, 30 Jan 2021 15:21:56 +0000 (16:21 +0100)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Sat, 30 Jan 2021 15:21:56 +0000 (16:21 +0100)
src/boll.rs
src/common/geometry.rs
src/common/mod.rs
src/core/app.rs
src/core/controller.rs
src/core/game.rs
src/core/level/lvlgen.rs
src/core/level/mod.rs

index abf6c87..5d09563 100644 (file)
@@ -1,7 +1,7 @@
 use core::render::Renderer;
 use sdl2::rect::Rect;
 
-use common::Point2D;
+use common::Point;
 use sdl2::gfx::primitives::DrawRenderer;
 use {SCREEN_HEIGHT, SCREEN_WIDTH};
 
@@ -11,8 +11,8 @@ pub trait Boll {
 }
 
 pub struct SquareBoll {
-    pub pos: Point2D<f64>,
-    pub vel: Point2D<f64>,
+    pub pos: Point<f64>,
+    pub vel: Point<f64>,
 }
 
 impl Boll for SquareBoll {
@@ -56,7 +56,7 @@ pub struct CircleBoll {
 }
 
 impl CircleBoll {
-    pub fn new(pos: Point2D<f64>, vel: Point2D<f64>) -> CircleBoll {
+    pub fn new(pos: Point<f64>, vel: Point<f64>) -> CircleBoll {
         CircleBoll {
             boll: SquareBoll { pos, vel },
         }
index 1cf205e..02f93b1 100644 (file)
@@ -3,17 +3,17 @@ use std::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign, N
 #[macro_export]
 macro_rules! point {
     ( $x:expr, $y:expr ) => {
-        Point2D { x: $x, y: $y }
+        Point { x: $x, y: $y }
     };
 }
 
 #[derive(Debug, Default, Copy, Clone, PartialEq)]
-pub struct Point2D<T> {
+pub struct Point<T> {
     pub x: T,
     pub y: T,
 }
 
-impl Point2D<f64> {
+impl Point<f64> {
     pub fn length(&self) -> f64 {
         ((self.x * self.x) + (self.y * self.y)).sqrt()
     }
@@ -34,8 +34,8 @@ impl Point2D<f64> {
        self.to_radians().to_degrees()
     }
 
-    pub fn to_i32(self) -> Point2D<i32> {
-       Point2D {
+    pub fn to_i32(self) -> Point<i32> {
+       Point {
            x: self.x as i32,
            y: self.y as i32,
        }
@@ -44,7 +44,7 @@ impl Point2D<f64> {
 
 macro_rules! point_op {
     ($op:tt, $trait:ident($fn:ident), $trait_assign:ident($fn_assign:ident), $rhs:ident = $Rhs:ty => $x:expr, $y:expr) => {
-        impl<T: $trait<Output = T>> $trait<$Rhs> for Point2D<T> {
+        impl<T: $trait<Output = T>> $trait<$Rhs> for Point<T> {
             type Output = Self;
 
             fn $fn(self, $rhs: $Rhs) -> Self {
@@ -55,7 +55,7 @@ macro_rules! point_op {
             }
         }
 
-        impl<T: $trait<Output = T> + Copy> $trait_assign<$Rhs> for Point2D<T> {
+        impl<T: $trait<Output = T> + Copy> $trait_assign<$Rhs> for Point<T> {
             fn $fn_assign(&mut self, $rhs: $Rhs) {
                 *self = Self {
                     x: self.x $op $x,
@@ -66,17 +66,17 @@ macro_rules! point_op {
     }
 }
 
-point_op!(+, Add(add), AddAssign(add_assign), rhs = Point2D<T> => rhs.x, rhs.y);
-point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point2D<T> => rhs.x, rhs.y);
-point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point2D<T> => rhs.x, rhs.y);
-point_op!(/, Div(div), DivAssign(div_assign), rhs = Point2D<T> => rhs.x, rhs.y);
+point_op!(+, Add(add), AddAssign(add_assign), rhs = Point<T> => rhs.x, rhs.y);
+point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point<T> => rhs.x, rhs.y);
+point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point<T> => rhs.x, rhs.y);
+point_op!(/, Div(div), DivAssign(div_assign), rhs = Point<T> => rhs.x, rhs.y);
 point_op!(+, Add(add), AddAssign(add_assign), rhs = (T, T) => rhs.0, rhs.1);
 point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = (T, T) => rhs.0, rhs.1);
 point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = (T, T) => rhs.0, rhs.1);
 point_op!(/, Div(div), DivAssign(div_assign), rhs = (T, T) => rhs.0, rhs.1);
 
 ////////// multiply point with scalar //////////////////////////////////////////
-impl<T: Mul<Output = T> + Copy> Mul<T> for Point2D<T> {
+impl<T: Mul<Output = T> + Copy> Mul<T> for Point<T> {
     type Output = Self;
 
     fn mul(self, rhs: T) -> Self {
@@ -87,7 +87,7 @@ impl<T: Mul<Output = T> + Copy> Mul<T> for Point2D<T> {
     }
 }
 
-impl<T: Mul<Output = T> + Copy> MulAssign<T> for Point2D<T> {
+impl<T: Mul<Output = T> + Copy> MulAssign<T> for Point<T> {
     fn mul_assign(&mut self, rhs: T) {
        *self = Self {
            x: self.x * rhs,
@@ -97,7 +97,7 @@ impl<T: Mul<Output = T> + Copy> MulAssign<T> for Point2D<T> {
 }
 
 ////////// divide point with scalar ////////////////////////////////////////////
-impl<T: Div<Output = T> + Copy> Div<T> for Point2D<T> {
+impl<T: Div<Output = T> + Copy> Div<T> for Point<T> {
     type Output = Self;
 
     fn div(self, rhs: T) -> Self {
@@ -108,7 +108,7 @@ impl<T: Div<Output = T> + Copy> Div<T> for Point2D<T> {
     }
 }
 
-impl<T: Div<Output = T> + Copy> DivAssign<T> for Point2D<T> {
+impl<T: Div<Output = T> + Copy> DivAssign<T> for Point<T> {
     fn div_assign(&mut self, rhs: T) {
        *self = Self {
            x: self.x / rhs,
@@ -117,7 +117,7 @@ impl<T: Div<Output = T> + Copy> DivAssign<T> for Point2D<T> {
     }
 }
 
-impl<T: Neg<Output = T>> Neg for Point2D<T> {
+impl<T: Neg<Output = T>> Neg for Point<T> {
     type Output = Self;
 
     fn neg(self) -> Self {
@@ -128,34 +128,34 @@ impl<T: Neg<Output = T>> Neg for Point2D<T> {
     }
 }
 
-impl<T> From<(T, T)> for Point2D<T> {
+impl<T> From<(T, T)> for Point<T> {
     fn from(item: (T, T)) -> Self {
-        Point2D {
+        Point {
             x: item.0,
             y: item.1,
         }
     }
 }
 
-impl<T> From<Point2D<T>> for (T, T) {
-    fn from(item: Point2D<T>) -> Self {
+impl<T> From<Point<T>> for (T, T) {
+    fn from(item: Point<T>) -> Self {
         (item.x, item.y)
     }
 }
 
-impl From<Degrees> for Point2D<f64> {
+impl From<Degrees> for Point<f64> {
     fn from(item: Degrees) -> Self {
        let r = item.0.to_radians();
-        Point2D {
+        Point {
             x: r.cos(),
             y: r.sin(),
         }
     }
 }
 
-impl From<Radians> for Point2D<f64> {
+impl From<Radians> for Point<f64> {
     fn from(item: Radians) -> Self {
-        Point2D {
+        Point {
             x: item.0.cos(),
             y: item.0.sin(),
         }
@@ -286,8 +286,8 @@ mod tests {
        assert_eq!(Radians(0.0).to_degrees(), Degrees(0.0));
        assert_eq!(Radians(std::f64::consts::PI).to_degrees(), Degrees(180.0));
        assert_eq!(Degrees(180.0).to_radians(), Radians(std::f64::consts::PI));
-       assert!((Point2D::from(Degrees(90.0)) - point!(0.0, 1.0)).length() < 0.001);
-       assert!((Point2D::from(Radians(std::f64::consts::FRAC_PI_2)) - point!(0.0, 1.0)).length() < 0.001);
+       assert!((Point::from(Degrees(90.0)) - point!(0.0, 1.0)).length() < 0.001);
+       assert!((Point::from(Radians(std::f64::consts::FRAC_PI_2)) - point!(0.0, 1.0)).length() < 0.001);
     }
 
     #[test]
index dc8d63b..3f5ba26 100644 (file)
@@ -1,5 +1,5 @@
 mod geometry;
-pub use common::geometry::Point2D;
+pub use common::geometry::Point;
 pub use common::geometry::Rect;
 pub use common::geometry::Radians;
 pub use common::geometry::Degrees;
index b4d2c01..9298ece 100644 (file)
@@ -1,5 +1,5 @@
 use boll::*;
-use common::{Point2D, Rect};
+use common::{Point, Rect};
 use core::controller::ControllerManager;
 use core::render::Renderer;
 use point; // defined in common, but loaded from main...
index 1eeb13c..030d962 100644 (file)
@@ -1,4 +1,4 @@
-use common::Point2D;
+use common::Point;
 use {hashmap, point};
 use common::Radians;
 use sdl2::HapticSubsystem;
@@ -88,11 +88,11 @@ impl Stick {
     #[inline(always)] #[allow(dead_code)] pub fn left(&self) -> bool { self.x < -0.99 }
     #[inline(always)] #[allow(dead_code)] pub fn right(&self) -> bool { self.x > 0.99 }
 
-    pub fn to_axis_point(&self) -> Point2D<f64> {
+    pub fn to_axis_point(&self) -> Point<f64> {
        point!(self.x as f64, self.y as f64)
     }
 
-    pub fn to_point(&self) -> Point2D<f64> {
+    pub fn to_point(&self) -> Point<f64> {
        let p = point!(self.x as f64, self.y as f64);
        if p.length() > 1.0 {
            p.normalized()
@@ -102,7 +102,7 @@ impl Stick {
     }
 }
 
-impl From<&Stick> for Point2D<f64> {
+impl From<&Stick> for Point<f64> {
     fn from(item: &Stick) -> Self {
        Self {
            x: item.x as f64,
index 36e6ed5..eabbdf9 100644 (file)
@@ -1,6 +1,6 @@
 use ActiveState;
 use AppState;
-use common::{Point2D, Radians};
+use common::{Point, Radians};
 use core::app::StateChange;
 use core::controller::Controller;
 use core::controller::ControllerManager;
@@ -145,8 +145,8 @@ pub trait Drawable {}
 
 pub struct Character {
     ctrl: Rc<RefCell<Controller>>,
-    pos: Point2D<f64>,
-    vel: Point2D<f64>,
+    pos: Point<f64>,
+    vel: Point<f64>,
 }
 
 impl Character {
@@ -237,7 +237,7 @@ impl Object for Character {
        renderer.draw_line(pos, p, (0, 255, 0));
        draw_cross(renderer, p);
        // // circle values
-       // let p = (self.pos + Point2D::from(ctrl.aim.a) * l).to_i32().into();
+       // let p = (self.pos + Point::from(ctrl.aim.a) * l).to_i32().into();
        // renderer.draw_line(pos, p, (0, 0, 255));
        // draw_cross(renderer, p);
     }
@@ -251,8 +251,8 @@ fn draw_cross(renderer: &mut Renderer, p: (i32, i32)) {
 ////////// BOLL ////////////////////////////////////////////////////////////////
 
 pub struct Boll {
-    pos: Point2D<f64>,
-    vel: Point2D<f64>,
+    pos: Point<f64>,
+    vel: Point<f64>,
     bounces: u8,
 }
 
@@ -274,7 +274,7 @@ impl Object for Boll {
            let mut rng = rand::thread_rng();
            let a = Radians(self.vel.to_radians().0 + Normal::new(0.0, 0.75).sample(&mut rng));
            objects.push(Box::new(Boll {
-               vel: Point2D::from(a) * Normal::new(1.0, 0.25).sample(&mut rng) * self.vel.length(),
+               vel: Point::from(a) * Normal::new(1.0, 0.25).sample(&mut rng) * self.vel.length(),
                ..*self
            }));
        }
index 3dcc3a7..d2a389e 100644 (file)
@@ -1,5 +1,5 @@
 use {point, time_scope};
-use common::Point2D;
+use common::Point;
 use super::{Grid, Level};
 use noise::{NoiseFn, OpenSimplex, Seedable};
 use rand::Rng;
@@ -220,7 +220,7 @@ impl LevelGenerator {
        }
     }
 
-    fn find_walls(&self, grid: &Grid) -> Vec<Vec<Point2D<isize>>> {
+    fn find_walls(&self, grid: &Grid) -> Vec<Vec<Point<isize>>> {
        let mut walls = vec!();
        for r in self.find_regions(&grid) {
            if r.value {
@@ -256,7 +256,7 @@ impl Region {
        (min.0, min.1, 1 + max.0 - min.0, 1 + max.1 - min.1)
     }
 
-    pub fn outline(&self, scale: usize) -> Vec<Point2D<isize>> {
+    pub fn outline(&self, scale: usize) -> Vec<Point<isize>> {
        let rect = self.enclosing_rect();
        let (ox, oy, w, h) = rect;
        let grid = self.grid(&rect);
@@ -313,7 +313,7 @@ impl Region {
        grid
     }
 
-    fn find_first_point_of_outline(&self, rect: &(usize, usize, usize, usize), grid: &Vec<Vec<bool>>) -> Point2D<isize> {
+    fn find_first_point_of_outline(&self, rect: &(usize, usize, usize, usize), grid: &Vec<Vec<bool>>) -> Point<isize> {
        let (ox, oy, w, h) = rect;
        let is_outer_wall = (ox, oy) == (&0, &0); // we know this is always the outer wall of the level
        for x in 0..*w {
@@ -329,7 +329,7 @@ impl Region {
        panic!("no wall found!");
     }
 
-    fn find_next_point_of_outline(&self, grid: &Vec<Vec<bool>>, p: &mut Point2D<isize>, directions: &mut Vec<(isize, isize)>) {
+    fn find_next_point_of_outline(&self, grid: &Vec<Vec<bool>>, p: &mut Point<isize>, directions: &mut Vec<(isize, isize)>) {
        directions.rotate_left(2);
        loop {
            let d = directions[0];
@@ -341,7 +341,7 @@ impl Region {
        }
     }
 
-    fn check(&self, p: Point2D<isize>, grid: &Vec<Vec<bool>>) -> bool {
+    fn check(&self, p: Point<isize>, grid: &Vec<Vec<bool>>) -> bool {
        if p.x < 0 || p.x >= grid.len() as isize || p.y < 0 || p.y >= grid[0].len() as isize {
            false
        } else {
index b70cdb8..78fb199 100644 (file)
@@ -1,4 +1,4 @@
-use common::Point2D;
+use common::Point;
 use core::render::Renderer;
 use sprites::SpriteManager;
 
@@ -10,13 +10,13 @@ pub use self::lvlgen::LevelGenerator;
 
 #[derive(Default)]
 pub struct Level {
-    pub gravity: Point2D<f64>,
+    pub gravity: Point<f64>,
     pub grid: Grid,
-    walls: Vec<Vec<Point2D<isize>>>,
+    walls: Vec<Vec<Point<isize>>>,
 }
 
 impl Level {
-    // pub fn new(gravity: Point2D<f64>) -> Self {
+    // pub fn new(gravity: Point<f64>) -> Self {
     //         let seed = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as u32;
     //         let mut lvl = Level { gravity, grid: Grid::generate(seed, 10), iterations: 10, walls: vec!() };
     //         lvl.filter_regions();