diff --git a/src/engine/math/mod.rs b/src/engine/math/mod.rs new file mode 100644 index 0000000..4f233c9 --- /dev/null +++ b/src/engine/math/mod.rs @@ -0,0 +1,62 @@ +use std::ops::{ + Index, + IndexMut +}; + +/// A generic matrix type, useful for levels. +pub struct Matrix { + + /// The number of rows of the matrix. + rows: usize, + + /// The number of cols of the matrix. + cols: usize, + + /// The contained data in the matrix. + data: Vec, + +} + +impl Matrix where T: Clone { + /// Creates a matrix from an element and duplicates it. + pub fn from_size(rows: usize, cols: usize, element: T) -> Matrix { + Matrix { + rows: rows, + cols: cols, + data: vec![element; rows * cols], + } + } +} + +impl Matrix { + /// Converts an a pair corresponding to the row and columns into an integer. + pub fn to_usize(&self, (row, col): (usize, usize)) -> usize { + self.cols * col + row + } + + /// Returns the number of rows. + pub fn rows(&self) -> usize { + self.rows + } + + /// Returns the number of columns. + pub fn cols(&self) -> usize { + self.cols + } +} + +impl Index<(usize, usize)> for Matrix { + type Output = T; + + fn index(&self, index: (usize, usize)) -> &T { + let index = self.to_usize(index); + &self.data[index] + } +} + +impl IndexMut<(usize, usize)> for Matrix { + fn index_mut(&mut self, index: (usize, usize)) -> &mut T { + let index = self.to_usize(index); + &mut self.data[index] + } +} diff --git a/src/engine/mod.rs b/src/engine/mod.rs index a005066..fe62f62 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -10,3 +10,6 @@ pub mod controls; /// This module helps us dealing with textures. pub mod texture; + +/// This module contains the math tools that will be used. +pub mod math;