diff options
| author | Arne Dußin | 2021-05-07 18:06:02 +0200 |
|---|---|---|
| committer | Arne Dußin | 2021-05-07 18:06:02 +0200 |
| commit | 6de8cfc84edbc80196ad144f2886031a898f5ed7 (patch) | |
| tree | b51d5f147dacce69bbb70bf363067a2528a2601f | |
| parent | f3178df0a92fb3b87087e78cad7b9313f947be6a (diff) | |
| download | pmd_coop-6de8cfc84edbc80196ad144f2886031a898f5ed7.tar.gz pmd_coop-6de8cfc84edbc80196ad144f2886031a898f5ed7.zip | |
| -rw-r--r-- | Cargo.lock | 135 | ||||
| -rw-r--r-- | Cargo.toml | 6 | ||||
| -rw-r--r-- | src/components.rs | 17 | ||||
| -rw-r--r-- | src/main.rs | 20 | ||||
| -rw-r--r-- | src/math/mod.rs | 93 | ||||
| -rw-r--r-- | src/math/rect.rs | 203 | ||||
| -rw-r--r-- | src/math/vec2.rs | 231 | ||||
| -rw-r--r-- | src/systems/draw.rs | 40 | ||||
| -rw-r--r-- | src/systems/input.rs | 88 | ||||
| -rw-r--r-- | src/systems/mod.rs | 3 | ||||
| -rw-r--r-- | src/systems/movement.rs | 43 |
11 files changed, 860 insertions, 19 deletions
@@ -9,6 +9,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8fd72866655d1904d6b0997d0b07ba561047d070fbe29de039031c641b61217" [[package]] +name = "approx" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f2a05fd1bd10b2527e20a2cd32d8873d115b8b39fe219ee25f42a8aca6ba278" +dependencies = [ + "num-traits", +] + +[[package]] name = "arrayvec" version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -118,6 +127,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" [[package]] +name = "float-cmp" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1267f4ac4f343772758f7b1bdcbe767c218bbab93bb432acbf5162bbf85a6c4" +dependencies = [ + "num-traits", +] + +[[package]] name = "hashbrown" version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -168,6 +186,15 @@ dependencies = [ ] [[package]] +name = "matrixmultiply" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a8a15b776d9dfaecd44b03c5828c2199cddff5247215858aac14624f8d6b741" +dependencies = [ + "rawpointer", +] + +[[package]] name = "maybe-uninit" version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -195,6 +222,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a785740271256c230f57462d3b83e52f998433a7062fc18f96d5999474a9f915" [[package]] +name = "nalgebra" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "476d1d59fe02fe54c86356e91650cd892f392782a1cb9fc524ec84f7aa9e1d06" +dependencies = [ + "approx", + "matrixmultiply", + "num-complex", + "num-rational", + "num-traits", + "simba", + "typenum", +] + +[[package]] name = "nom" version = "5.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -205,6 +247,45 @@ dependencies = [ ] [[package]] +name = "num-complex" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "747d632c0c558b87dbabbe6a82f3b4ae03720d0646ac5b7b4dae89394be5f2c5" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + +[[package]] name = "num_cpus" version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -215,11 +296,21 @@ dependencies = [ ] [[package]] +name = "paste" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf547ad0c65e31259204bd90935776d1c693cec2f4ff7abb7a1bbbd40dfe58" + +[[package]] name = "pmd_coop" version = "0.1.0" dependencies = [ + "float-cmp", "lazy_static", + "nalgebra", + "num-traits", "sdl2", + "serde", "specs", "specs-derive", ] @@ -243,6 +334,12 @@ dependencies = [ ] [[package]] +name = "rawpointer" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" + +[[package]] name = "rayon" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -297,6 +394,26 @@ dependencies = [ ] [[package]] +name = "serde" +version = "1.0.125" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.125" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b093b7a2bb58203b5da3056c05b4ec1fed827dcfdb37347a8841695263b3d06d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] name = "shred" version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -317,6 +434,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5752e017e03af9d735b4b069f53b7a7fd90fefafa04d8bd0c25581b0bff437f" [[package]] +name = "simba" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5132a955559188f3d13c9ba831e77c802ddc8782783f050ed0c52f5988b95f4c" +dependencies = [ + "approx", + "num-complex", + "num-traits", + "paste", +] + +[[package]] name = "smallvec" version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -376,6 +505,12 @@ dependencies = [ ] [[package]] +name = "typenum" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06" + +[[package]] name = "unicode-xid" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -6,9 +6,13 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +lazy_static = "*" +float-cmp = "*" +nalgebra = "*" +num-traits = "*" +serde = { version = "*", features = ["derive"] } specs = "^0.16" specs-derive = "*" -lazy_static = "*" [dependencies.sdl2] version = "^0.34" diff --git a/src/components.rs b/src/components.rs index c92ca03..9b51bc6 100644 --- a/src/components.rs +++ b/src/components.rs @@ -1,15 +1,22 @@ +use sdl2::rect::Rect; use specs::prelude::*; use specs_derive::*; +use crate::math::Vec2; + #[derive(Component, Debug, Clone, Copy)] -pub struct Pos -{ - pub x: f64, - pub y: f64, -} +pub struct Pos(pub Vec2<f64>); + +#[derive(Component, Debug, Clone, Copy)] +pub struct Velocity(pub Vec2<f64>); #[derive(Component)] pub struct StaticDrawable { pub texture_name: String, + pub source_rect: Rect, } + +#[derive(Component, Default)] +#[storage(NullStorage)] +pub struct Player; diff --git a/src/main.rs b/src/main.rs index 60186b3..8bc4faf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,16 @@ +#![allow(dead_code)] + pub mod components; +pub mod math; pub mod systems; pub mod texture_manager; -use components::{Pos, StaticDrawable}; +use components::*; +use math::Vec2; +use sdl2::rect::Rect; use specs::prelude::*; use specs::{DispatcherBuilder, World}; -use systems::{SysDraw, SysInput}; +use systems::{SysDraw, SysInput, SysMovement}; fn main() { @@ -23,23 +28,30 @@ fn main() let mut world = World::new(); world.register::<Pos>(); world.register::<StaticDrawable>(); + world.register::<Velocity>(); + world.register::<Player>(); world .create_entity() - .with(Pos { x: 0., y: 0. }) + .with(Pos(Vec2::new(0., 0.))) + .with(Velocity(Vec2::new(0., 0.))) .with(StaticDrawable { texture_name: "portraits.png".to_string(), + source_rect: Rect::new(0, 0, 40, 40), }) + .with(Player) .build(); let sys_input = SysInput::new(&sdl); let sys_draw = SysDraw::new(window); + let sys_move = SysMovement::new(); let mut dispatcher = DispatcherBuilder::new() + .with(sys_move, "move", &[]) .with_thread_local(sys_input) .with_thread_local(sys_draw) .build(); loop { - dispatcher.dispatch(&mut world); + dispatcher.dispatch(&world); } } diff --git a/src/math/mod.rs b/src/math/mod.rs new file mode 100644 index 0000000..8b22972 --- /dev/null +++ b/src/math/mod.rs @@ -0,0 +1,93 @@ +//! Useful mathematical operations in graphical contexts. + +pub mod rect; +pub mod vec2; + +use std::cmp::Ordering; + +use nalgebra::RealField; +use num_traits::Pow; + +pub use self::rect::*; +pub use self::vec2::*; + +/// Round a floating point number to the nearest step given by the step +/// argument. For instance, if the step is 0.5, then all numbers from 0.0 to +/// 0.24999... will be 0., while all numbers from 0.25 to 0.74999... will be 0.5 +/// and so on. +pub fn round<T>(num: T, step: T) -> T +where + T: RealField, +{ + // Only positive steps will be accepted. + assert!(step > T::zero()); + + let lower_bound = (num / step).floor() * step; + let upper_bound = lower_bound + step; + + // Compare the distances and prefer the smaller. If they are the same, prefer + // the upper bound. + if (num - lower_bound) < (upper_bound - num) { + lower_bound + } + else { + upper_bound + } +} + +/// Like round, but instead of rounding to a certain fraction, rounds to the nth +/// decimal place instead of taking a granularity. +pub fn round_nth_decimal<T>(num: T, decimal_place: u16) -> T +where + T: RealField + Pow<u16, Output = T>, +{ + round(num, nalgebra::convert::<f64, T>(0.1).pow(decimal_place)) +} + +/// Works like `std::cmp::max`, however also allows partial comparisons. It is +/// specifically designed so functions that should be able to use f32 and f64 +/// work, eventhough these do not implement Ord. The downside of this function +/// however is, that its behaviour is undefined when `f32::NaN` for instance +/// were to be passed. +pub(crate) fn partial_max<T>(a: T, b: T) -> T +where + T: PartialOrd, +{ + match a.partial_cmp(&b) { + Some(Ordering::Greater) => a, + _ => b, + } +} +/// Like `partial_max`, but for minimum values. Comes with the same downside, +/// too. +pub(crate) fn partial_min<T>(a: T, b: T) -> T +where + T: PartialOrd, +{ + match a.partial_cmp(&b) { + Some(Ordering::Less) => a, + _ => b, + } +} + +#[cfg(test)] +mod test +{ + #[test] + fn partial_max() + { + assert_eq!(super::partial_max(0., 0.), 0.); + assert_eq!(super::partial_max(-1., 1.), 1.); + assert_eq!(super::partial_max(-2., -1.), -1.); + assert_eq!(super::partial_max(2., 1.), 2.); + } + + #[test] + fn partial_min() + { + assert_eq!(super::partial_min(0., 0.), 0.); + assert_eq!(super::partial_min(-1., 1.), -1.); + assert_eq!(super::partial_min(-2., -1.), -2.); + assert_eq!(super::partial_min(2., 1.), 1.); + } +} diff --git a/src/math/rect.rs b/src/math/rect.rs new file mode 100644 index 0000000..55e0b1c --- /dev/null +++ b/src/math/rect.rs @@ -0,0 +1,203 @@ +//! Rectangles where the sides are parallel to the x and y-axes. + +use std::ops::{Add, AddAssign, Sub}; + +//use alga::general::{Additive, Identity}; +use nalgebra::Scalar; +use num_traits::sign::{self, Signed}; +use num_traits::Zero; +use serde::{Deserialize, Serialize}; + +use super::Vec2; + +/// Represents a Rectangle with the value type T. +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct Rect<T: Scalar + Copy> +{ + /// The x coordinate, or leftmost coordinate of the Rect. + pub x: T, + /// The y coordinate, or rightmost coordinate of the Rect. + pub y: T, + /// The width of the Rect. + pub w: T, + /// The height of the Rect. + pub h: T, +} + +impl<T: Scalar + Copy> Rect<T> +{ + /// Create a new Rectangle from the internal values, where it might be nicer + /// to use a function instead of setting the values directly. + pub fn new(x: T, y: T, w: T, h: T) -> Self { Self { x, y, w, h } } + + /// Create a Rectangle from a slice. Indices are [x, y, w, h]. + pub fn from_slice(slice: [T; 4]) -> Rect<T> + where + T: Copy, + { + Rect { + x: slice[0], + y: slice[1], + w: slice[2], + h: slice[3], + } + } + + /// Move by the Vec provided. + pub fn translate(&mut self, by: Vec2<T>) + where + T: AddAssign, + { + self.x += by.x; + self.y += by.y; + } + + /// Set the posiotien of the rectangle to the given one without changing its + /// size + pub fn set_pos(&mut self, pos: Vec2<T>) + { + self.x = pos.x; + self.y = pos.y; + } + + /// Test if two rectangles intersect. + pub fn intersect<'a>(this: &'a Rect<T>, other: &'a Rect<T>) -> bool + where + T: Add<Output = T> + PartialOrd + Copy, + { + !(this.x > other.x + other.w + || this.x + this.w < other.x + || this.y > other.y + other.h + || this.y + this.h < other.y) + } + + /// Function to calculate the bounding rectangle that is between two + /// vectors. The order of the vectors is irrelevent for this. As long as + /// they are diagonally opposite of each other, this function will work. + pub fn bounding_rect(pos1: Vec2<T>, pos2: Vec2<T>) -> Self + where + T: PartialOrd + Sub<Output = T>, + { + let vertices = [pos1, pos2]; + + Self::bounding_rect_n(&vertices) + } + + /// Function to calculate the bounding rectangle of n vertices provided. The + /// order of them is not relevant and a point that is contained by the + /// vertices will not change the result. + /// + /// # Panics + /// If there is not at least one vertex in the vertices slice, the function + /// will panic, since it is impossible to calculate any bounds in such a + /// case. + pub fn bounding_rect_n(vertices: &[Vec2<T>]) -> Self + where + T: PartialOrd + Sub<Output = T>, + { + if vertices.is_empty() { + panic!("Cannot create bounding rectangle without any vertices"); + } + + let mut min = vertices[0]; + let mut max = vertices[0]; + + for vertex in vertices.iter().skip(1) { + min.x = super::partial_min(min.x, vertex.x); + min.y = super::partial_min(min.y, vertex.y); + max.x = super::partial_max(max.x, vertex.x); + max.y = super::partial_max(max.y, vertex.y); + } + + Self { + x: min.x, + y: min.y, + w: max.x - min.x, + h: max.y - min.y, + } + } + + /// Get the shortest way that must be applied to this Rect to clear out of + /// another Rect of the same type so that they would not intersect any more. + pub fn shortest_way_out(&self, of: &Rect<T>) -> Vec2<T> + where + T: Add<Output = T> + Sub<Output = T> + Signed + PartialOrd, + { + // Check upwards + let mut move_y = of.y - self.y - self.h; + // Check downwards + let move_down = of.y + of.h - self.y; + if move_down < -move_y { + move_y = move_down; + } + + // Check left + let mut move_x = of.x - self.x - self.w; + // Check right + let move_right = of.x + of.w - self.x; + if move_right < -move_x { + move_x = move_right; + } + + if move_x.abs() < move_y.abs() { + Vec2::new(move_x, T::zero()) + } + else { + Vec2::new(T::zero(), move_y) + } + } + + /// Calculate the area of the rectangle. + pub fn area(&self) -> T + where + T: Signed, + { + sign::abs(self.w) * sign::abs(self.h) + } + + fn contains_point(&self, point: &Vec2<T>) -> bool + where + T: Add<Output = T> + PartialOrd, + { + point.x >= self.x + && point.x <= self.x + self.w + && point.y >= self.y + && point.y <= self.y + self.h + } + + fn contains_rect(&self, rect: &Rect<T>) -> bool + where + T: Add<Output = T> + Sub<Output = T> + Zero + PartialOrd, + { + /* True, if the rightmost x-coordinate of the called rectangle is further + * right or the same as the rightmost coordinate of the given rect. + */ + let x_exceeds = self.x + self.w - rect.x - rect.w >= T::zero(); + // The same for the y-coordinates + let y_exceeds = self.y + self.h - rect.y - rect.h >= T::zero(); + + x_exceeds && y_exceeds && self.x <= rect.x && self.y <= rect.y + } + + fn is_inside_rect(&self, rect: &Rect<T>) -> bool + where + T: Add<Output = T> + Sub<Output = T> + Zero + PartialOrd, + { + rect.contains_rect(&self) + } +} + +#[cfg(test)] +mod test +{ + use super::*; + + #[test] + fn test_intersect() + { + let a = Rect::from_slice([0, 0, 4, 4]); + let b = Rect::from_slice([-1, -1, 1, 1]); + + assert!(Rect::intersect(&a, &b)); + } +} diff --git a/src/math/vec2.rs b/src/math/vec2.rs new file mode 100644 index 0000000..5ae0024 --- /dev/null +++ b/src/math/vec2.rs @@ -0,0 +1,231 @@ +//! Two-dimensional vectors and useful operations on them. + +use std::cmp::Ordering; +use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign}; +use std::{fmt, mem}; + +use nalgebra::{RealField, Scalar}; +use num_traits::One; +use serde::{Deserialize, Serialize}; + +use crate::math::Rect; + +/// Describes a vector, which may be a point or a directed length, depending on +/// the interpretation. +#[allow(missing_docs)] +#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, Eq, Hash)] +pub struct Vec2<T: Scalar + Copy> +{ + pub x: T, + pub y: T, +} + +impl<T: Scalar + Copy> Vec2<T> +{ + /// Create a new vector from its internal values. + pub fn new(x: T, y: T) -> Self { Self { x, y } } + + /// Finds the euclidian length and returns it. + pub fn length(&self) -> T + where + T: RealField, + { + (self.x * self.x + self.y * self.y).sqrt() + } + + /// Consumes the vector and returns a vector that is rotated by Pi/2 in + /// clockwise direction. This is a special case of rotation and the + /// function is faster than using the nonspecific rotation function. + pub fn rotated_90_clockwise(mut self) -> Vec2<T> + where + T: One + Neg<Output = T> + MulAssign, + { + mem::swap(&mut self.x, &mut self.y); + self.y *= -T::one(); + self + } + + /// Consumes the vector and returns a vector that is rotated by Pi/2 in + /// counterclockwise direction. This is a special case of rotation and + /// the function is faster than using the nonspecific rotation function. + pub fn rotated_90_counterclockwise(mut self) -> Vec2<T> + where + T: One + Neg<Output = T> + MulAssign, + { + mem::swap(&mut self.x, &mut self.y); + self.x *= -T::one(); + self + } +} + +// Begin mathematical operators ----------------------------------------------- + +// Addition +impl<T: Scalar + Add<Output = T> + Copy> Add for Vec2<T> +{ + type Output = Self; + + fn add(self, rhs: Self) -> Self { Vec2::new(self.x + rhs.x, self.y + rhs.y) } +} + +impl<T: Scalar + Add<Output = T> + Copy> Add<(T, T)> for Vec2<T> +{ + type Output = Self; + + fn add(self, (x, y): (T, T)) -> Self { Vec2::new(self.x + x, self.y + y) } +} + +impl<T: Scalar + Add<Output = T> + Copy> Add<T> for Vec2<T> +{ + type Output = Self; + + fn add(self, rhs: T) -> Self { Vec2::new(self.x + rhs, self.y + rhs) } +} + +impl<T: Scalar + AddAssign + Copy> AddAssign for Vec2<T> +{ + fn add_assign(&mut self, rhs: Self) + { + self.x += rhs.x; + self.y += rhs.y; + } +} + +impl<T: Scalar + AddAssign + Copy> AddAssign<(T, T)> for Vec2<T> +{ + fn add_assign(&mut self, (x, y): (T, T)) + { + self.x += x; + self.y += y; + } +} + +// Subtraction +impl<T: Scalar + Sub<Output = T> + Copy> Sub for Vec2<T> +{ + type Output = Self; + + fn sub(self, rhs: Self) -> Self { Vec2::new(self.x - rhs.x, self.y - rhs.y) } +} + +impl<T: Scalar + Sub<Output = T> + Copy> Sub<(T, T)> for Vec2<T> +{ + type Output = Self; + + fn sub(self, (x, y): (T, T)) -> Self { Vec2::new(self.x - x, self.y - y) } +} + +impl<T: Scalar + Sub<Output = T> + Copy> Sub<T> for Vec2<T> +{ + type Output = Self; + + fn sub(self, rhs: T) -> Self { Vec2::new(self.x - rhs, self.y - rhs) } +} + +impl<T: Scalar + SubAssign + Copy> SubAssign for Vec2<T> +{ + fn sub_assign(&mut self, rhs: Self) + { + self.x -= rhs.x; + self.y -= rhs.y; + } +} + +impl<T: Scalar + SubAssign + Copy> SubAssign<(T, T)> for Vec2<T> +{ + fn sub_assign(&mut self, (x, y): (T, T)) + { + self.x -= x; + self.y -= y; + } +} + +// Scalar multiplication +impl<T: Scalar + Add<Output = T> + Mul<Output = T> + Copy> Mul for Vec2<T> +{ + type Output = T; + + fn mul(self, rhs: Self) -> T { self.x * rhs.x + self.y * rhs.y } +} + +impl<T: Scalar + Mul<Output = T> + Copy> Mul<T> for Vec2<T> +{ + type Output = Self; + + fn mul(self, rhs: T) -> Self { Vec2::new(self.x * rhs, self.y * rhs) } +} + +impl<T: Scalar + Div<Output = T> + Copy> Div<T> for Vec2<T> +{ + type Output = Self; + + fn div(self, rhs: T) -> Self { Vec2::new(self.x / rhs, self.y / rhs) } +} + +// End of mathematical operators ---------------------------------------------- + +// By default, the coordinates are first compared by their y-coordinates, then +// their x-coordinates +impl<T: fmt::Debug> PartialOrd for Vec2<T> +where + T: PartialOrd + Copy + 'static, +{ + fn partial_cmp(&self, other: &Self) -> Option<Ordering> + { + match self.y.partial_cmp(&other.y) { + Some(Ordering::Equal) | None => self.x.partial_cmp(&other.x), + y_order => y_order, + } + } +} + +impl<T: fmt::Debug> Ord for Vec2<T> +where + T: Ord + Copy + 'static, +{ + fn cmp(&self, other: &Self) -> Ordering + { + match self.y.cmp(&other.y) { + Ordering::Equal => self.x.cmp(&other.x), + y_order => y_order, + } + } +} + +// Helper function to determine the absolute positive difference between two +// Values, which don't have to be signed. +fn difference_abs<T>(a: T, b: T) -> T +where + T: Sub<Output = T> + PartialOrd, +{ + if a > b { + a - b + } + else { + b - a + } +} + +// Helper function that removes all points inside the vector that are not +// contained inside the optional limit Rect +fn retain_inside_limits<T: 'static>(items: Vec<Vec2<T>>, limits: Option<Rect<T>>) -> Vec<Vec2<T>> +where + T: PartialOrd + std::fmt::Debug + Copy + Add<Output = T>, +{ + // Fast return in case there are no limits + if limits.is_none() { + return items; + } + let limits = limits.unwrap(); + + // Retain only items that are within the bounds of the limits rect + items + .into_iter() + .filter(|v| { + v.x >= limits.x + && v.x <= limits.x + limits.w + && v.y >= limits.y + && v.y <= limits.y + limits.h + }) + .collect() +} diff --git a/src/systems/draw.rs b/src/systems/draw.rs index 1d5ee6b..fd0185e 100644 --- a/src/systems/draw.rs +++ b/src/systems/draw.rs @@ -1,4 +1,8 @@ +use std::thread; +use std::time::{Duration, Instant}; + use sdl2::pixels::Color; +use sdl2::rect::Rect; use sdl2::render::WindowCanvas; use sdl2::video::{Window, WindowContext}; use specs::prelude::*; @@ -6,8 +10,14 @@ use specs::prelude::*; use crate::components::{Pos, StaticDrawable}; use crate::texture_manager::TextureManager; +/// The minimum time in micro a frame should take to limit the number of +/// frames drawn to the screen. 5000 microseconds for a maximum of 200 frames +/// per second. +pub const FRAME_TIME_MIN_US: u64 = (1_000_000. / 200.) as u64; + pub struct SysDraw { + last_frame_time: Instant, canvas: WindowCanvas, texture_manager: TextureManager<WindowContext>, } @@ -25,10 +35,20 @@ impl SysDraw let texture_manager = TextureManager::new(canvas.texture_creator()); Self { + last_frame_time: Instant::now(), canvas, texture_manager, } } + + pub fn delay_for_min_frametime(&mut self) + { + let frame_duration_us = (Instant::now() - self.last_frame_time).as_micros() as u64; + if frame_duration_us < FRAME_TIME_MIN_US { + thread::sleep(Duration::from_micros(FRAME_TIME_MIN_US - frame_duration_us)); + } + self.last_frame_time = Instant::now(); + } } impl<'a> System<'a> for SysDraw @@ -39,20 +59,30 @@ impl<'a> System<'a> for SysDraw { self.canvas.clear(); - // XXX: This is so slow.. yaaaawn. Replace with ids and texture manager - // lifetime? - for (_pos, drawable) in (&pos, &drawable).join() { + /* XXX: This is so slow.. yaaaawn. Replace with ids and texture manager + * lifetime? */ + for (pos, drawable) in (&pos, &drawable).join() { let texture = self .texture_manager .get(&drawable.texture_name) .expect("Unable to load texture"); - // let texture_info = texture.query(); self.canvas - .copy(&texture, None, None) + .copy( + &texture, + Some(drawable.source_rect), + Rect::new( + pos.0.x as i32, + pos.0.y as i32, + drawable.source_rect.width(), + drawable.source_rect.height(), + ), + ) .expect("Unable to draw texture"); } + self.delay_for_min_frametime(); + self.canvas.present(); } } diff --git a/src/systems/input.rs b/src/systems/input.rs index 96ac3b8..13942e0 100644 --- a/src/systems/input.rs +++ b/src/systems/input.rs @@ -1,10 +1,21 @@ use sdl2::event::{Event, WindowEvent}; +use sdl2::keyboard::Scancode; use sdl2::{EventPump, Sdl}; use specs::prelude::*; +use crate::components::{Player, Velocity}; + +pub const PLAYER_SPEED: f64 = 150.; + +pub const ACTION_KEY_UP: u8 = 0b0001; +pub const ACTION_KEY_LEFT: u8 = 0b0010; +pub const ACTION_KEY_DOWN: u8 = 0b0100; +pub const ACTION_KEY_RIGHT: u8 = 0b1000; + pub struct SysInput { - event_pump: EventPump, + pressed_action_keys: u8, + event_pump: EventPump, } impl SysInput @@ -12,17 +23,19 @@ impl SysInput pub fn new(sdl: &Sdl) -> Self { Self { - event_pump: sdl.event_pump().unwrap(), + pressed_action_keys: 0b0, + event_pump: sdl.event_pump().unwrap(), } } } impl<'a> System<'a> for SysInput { - type SystemData = (); + type SystemData = (WriteStorage<'a, Velocity>, ReadStorage<'a, Player>); - fn run(&mut self, (): Self::SystemData) + fn run(&mut self, (mut vel, player): Self::SystemData) { + let mut player_movement_changed = false; for event in self.event_pump.poll_iter() { match event { Event::Quit { .. } => { @@ -34,8 +47,75 @@ impl<'a> System<'a> for SysInput } => { println!("Window resized to {}x{}", x, y); }, + Event::KeyDown { scancode, .. } => match scancode { + Some(Scancode::W) => { + self.pressed_action_keys |= ACTION_KEY_UP; + player_movement_changed = true; + }, + Some(Scancode::A) => { + self.pressed_action_keys |= ACTION_KEY_LEFT; + player_movement_changed = true; + }, + Some(Scancode::S) => { + self.pressed_action_keys |= ACTION_KEY_DOWN; + player_movement_changed = true; + }, + Some(Scancode::D) => { + self.pressed_action_keys |= ACTION_KEY_RIGHT; + player_movement_changed = true; + }, + key => { + println!("keydown received: {:?}", key) + }, + }, + Event::KeyUp { scancode, .. } => match scancode { + Some(Scancode::W) => { + self.pressed_action_keys &= !ACTION_KEY_UP; + player_movement_changed = true; + }, + Some(Scancode::A) => { + self.pressed_action_keys &= !ACTION_KEY_LEFT; + player_movement_changed = true; + }, + Some(Scancode::S) => { + self.pressed_action_keys &= !ACTION_KEY_DOWN; + player_movement_changed = true; + }, + Some(Scancode::D) => { + self.pressed_action_keys &= !ACTION_KEY_RIGHT; + player_movement_changed = true; + }, + _ => {}, + }, _ => {}, } } + + /* Update player movement */ + if player_movement_changed { + for (vel, _player) in (&mut vel, &player).join() { + /* Vertical movement */ + if (self.pressed_action_keys & ACTION_KEY_UP) != 0 { + vel.0.y = -PLAYER_SPEED; + } + else if (self.pressed_action_keys & ACTION_KEY_DOWN) != 0 { + vel.0.y = PLAYER_SPEED; + } + else { + vel.0.y = 0.; + } + + /* Horizontal movement */ + if (self.pressed_action_keys & ACTION_KEY_LEFT) != 0 { + vel.0.x = -PLAYER_SPEED; + } + else if (self.pressed_action_keys & ACTION_KEY_RIGHT) != 0 { + vel.0.x = PLAYER_SPEED; + } + else { + vel.0.x = 0.; + } + } + } } } diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 2c2af6c..5101a83 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -3,3 +3,6 @@ pub use self::draw::*; pub mod input; pub use self::input::*; + +pub mod movement; +pub use self::movement::*; diff --git a/src/systems/movement.rs b/src/systems/movement.rs new file mode 100644 index 0000000..61c572a --- /dev/null +++ b/src/systems/movement.rs @@ -0,0 +1,43 @@ +use std::time::{Duration, Instant}; + +use specs::prelude::*; + +use crate::components::{Pos, Velocity}; + +pub struct SysMovement +{ + delta_time: Duration, + last_run: Instant, +} + +impl SysMovement +{ + pub fn new() -> Self + { + Self { + delta_time: Duration::ZERO, + last_run: Instant::now(), + } + } +} + +impl Default for SysMovement +{ + fn default() -> Self { Self::new() } +} + +impl<'a> System<'a> for SysMovement +{ + type SystemData = (WriteStorage<'a, Pos>, ReadStorage<'a, Velocity>); + + fn run(&mut self, (mut pos, vel): Self::SystemData) + { + let now = Instant::now(); + self.delta_time = now - self.last_run; + self.last_run = now; + + for (pos, vel) in (&mut pos, &vel).join() { + pos.0 += vel.0 * self.delta_time.as_secs_f64(); + } + } +} |
