From e570927ad1703298a2c85599c7e25475c60b33d4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Sat, 30 Jan 2021 16:21:56 +0100 Subject: [PATCH] Point2D -> Point --- src/boll.rs | 8 ++++---- src/common/geometry.rs | 52 ++++++++++++++++++++++++------------------------ src/common/mod.rs | 2 +- src/core/app.rs | 2 +- src/core/controller.rs | 8 ++++---- src/core/game.rs | 14 ++++++------- src/core/level/lvlgen.rs | 12 +++++------ src/core/level/mod.rs | 8 ++++---- 8 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/boll.rs b/src/boll.rs index abf6c87..5d09563 100644 --- a/src/boll.rs +++ b/src/boll.rs @@ -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, - pub vel: Point2D, + pub pos: Point, + pub vel: Point, } impl Boll for SquareBoll { @@ -56,7 +56,7 @@ pub struct CircleBoll { } impl CircleBoll { - pub fn new(pos: Point2D, vel: Point2D) -> CircleBoll { + pub fn new(pos: Point, vel: Point) -> CircleBoll { CircleBoll { boll: SquareBoll { pos, vel }, } diff --git a/src/common/geometry.rs b/src/common/geometry.rs index 1cf205e..02f93b1 100644 --- a/src/common/geometry.rs +++ b/src/common/geometry.rs @@ -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 { +pub struct Point { pub x: T, pub y: T, } -impl Point2D { +impl Point { pub fn length(&self) -> f64 { ((self.x * self.x) + (self.y * self.y)).sqrt() } @@ -34,8 +34,8 @@ impl Point2D { self.to_radians().to_degrees() } - pub fn to_i32(self) -> Point2D { - Point2D { + pub fn to_i32(self) -> Point { + Point { x: self.x as i32, y: self.y as i32, } @@ -44,7 +44,7 @@ impl Point2D { 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> $trait<$Rhs> for Point2D { + impl> $trait<$Rhs> for Point { type Output = Self; fn $fn(self, $rhs: $Rhs) -> Self { @@ -55,7 +55,7 @@ macro_rules! point_op { } } - impl + Copy> $trait_assign<$Rhs> for Point2D { + impl + Copy> $trait_assign<$Rhs> for Point { 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 => rhs.x, rhs.y); -point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point2D => rhs.x, rhs.y); -point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point2D => rhs.x, rhs.y); -point_op!(/, Div(div), DivAssign(div_assign), rhs = Point2D => rhs.x, rhs.y); +point_op!(+, Add(add), AddAssign(add_assign), rhs = Point => rhs.x, rhs.y); +point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point => rhs.x, rhs.y); +point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point => rhs.x, rhs.y); +point_op!(/, Div(div), DivAssign(div_assign), rhs = Point => 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 + Copy> Mul for Point2D { +impl + Copy> Mul for Point { type Output = Self; fn mul(self, rhs: T) -> Self { @@ -87,7 +87,7 @@ impl + Copy> Mul for Point2D { } } -impl + Copy> MulAssign for Point2D { +impl + Copy> MulAssign for Point { fn mul_assign(&mut self, rhs: T) { *self = Self { x: self.x * rhs, @@ -97,7 +97,7 @@ impl + Copy> MulAssign for Point2D { } ////////// divide point with scalar //////////////////////////////////////////// -impl + Copy> Div for Point2D { +impl + Copy> Div for Point { type Output = Self; fn div(self, rhs: T) -> Self { @@ -108,7 +108,7 @@ impl + Copy> Div for Point2D { } } -impl + Copy> DivAssign for Point2D { +impl + Copy> DivAssign for Point { fn div_assign(&mut self, rhs: T) { *self = Self { x: self.x / rhs, @@ -117,7 +117,7 @@ impl + Copy> DivAssign for Point2D { } } -impl> Neg for Point2D { +impl> Neg for Point { type Output = Self; fn neg(self) -> Self { @@ -128,34 +128,34 @@ impl> Neg for Point2D { } } -impl From<(T, T)> for Point2D { +impl From<(T, T)> for Point { fn from(item: (T, T)) -> Self { - Point2D { + Point { x: item.0, y: item.1, } } } -impl From> for (T, T) { - fn from(item: Point2D) -> Self { +impl From> for (T, T) { + fn from(item: Point) -> Self { (item.x, item.y) } } -impl From for Point2D { +impl From for Point { fn from(item: Degrees) -> Self { let r = item.0.to_radians(); - Point2D { + Point { x: r.cos(), y: r.sin(), } } } -impl From for Point2D { +impl From for Point { 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] diff --git a/src/common/mod.rs b/src/common/mod.rs index dc8d63b..3f5ba26 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -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; diff --git a/src/core/app.rs b/src/core/app.rs index b4d2c01..9298ece 100644 --- a/src/core/app.rs +++ b/src/core/app.rs @@ -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... diff --git a/src/core/controller.rs b/src/core/controller.rs index 1eeb13c..030d962 100644 --- a/src/core/controller.rs +++ b/src/core/controller.rs @@ -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 { + pub fn to_axis_point(&self) -> Point { point!(self.x as f64, self.y as f64) } - pub fn to_point(&self) -> Point2D { + pub fn to_point(&self) -> Point { 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 { +impl From<&Stick> for Point { fn from(item: &Stick) -> Self { Self { x: item.x as f64, diff --git a/src/core/game.rs b/src/core/game.rs index 36e6ed5..eabbdf9 100644 --- a/src/core/game.rs +++ b/src/core/game.rs @@ -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>, - pos: Point2D, - vel: Point2D, + pos: Point, + vel: Point, } 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, - vel: Point2D, + pos: Point, + vel: Point, 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 })); } diff --git a/src/core/level/lvlgen.rs b/src/core/level/lvlgen.rs index 3dcc3a7..d2a389e 100644 --- a/src/core/level/lvlgen.rs +++ b/src/core/level/lvlgen.rs @@ -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>> { + fn find_walls(&self, grid: &Grid) -> Vec>> { 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> { + pub fn outline(&self, scale: usize) -> Vec> { 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>) -> Point2D { + fn find_first_point_of_outline(&self, rect: &(usize, usize, usize, usize), grid: &Vec>) -> Point { 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>, p: &mut Point2D, directions: &mut Vec<(isize, isize)>) { + fn find_next_point_of_outline(&self, grid: &Vec>, p: &mut Point, 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, grid: &Vec>) -> bool { + fn check(&self, p: Point, grid: &Vec>) -> bool { if p.x < 0 || p.x >= grid.len() as isize || p.y < 0 || p.y >= grid[0].len() as isize { false } else { diff --git a/src/core/level/mod.rs b/src/core/level/mod.rs index b70cdb8..78fb199 100644 --- a/src/core/level/mod.rs +++ b/src/core/level/mod.rs @@ -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, + pub gravity: Point, pub grid: Grid, - walls: Vec>>, + walls: Vec>>, } impl Level { - // pub fn new(gravity: Point2D) -> Self { + // pub fn new(gravity: Point) -> 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(); -- 2.11.0