Animationen für die Pixeldecke
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.2 KiB

  1. use std::ops::Add;
  2. #[macro_export]
  3. macro_rules! v2 {
  4. ( $x:expr, $y:expr ) => {
  5. Vec2::new($x, $y)
  6. };
  7. }
  8. #[derive(Clone, Copy, Debug)]
  9. pub struct Vec2 {
  10. pub x: i32,
  11. pub y: i32,
  12. }
  13. impl Vec2 {
  14. pub fn new(x: i32, y: i32) -> Vec2 {
  15. Vec2 { x, y }
  16. }
  17. pub fn directions() -> Vec<Vec2> {
  18. vec![v2!(1, 0), v2!(0, 1), v2!(-1, 0), v2!(0, -1)]
  19. }
  20. }
  21. impl Add for Vec2 {
  22. type Output = Vec2;
  23. fn add(self, other: Vec2) -> Vec2 {
  24. let x = self.x + other.x;
  25. let y = self.y + other.y;
  26. Vec2 { x, y }
  27. }
  28. }
  29. impl<'a> Add<&'a Vec2> for Vec2 {
  30. type Output = Vec2;
  31. fn add(self, other: &'a Vec2) -> Vec2 {
  32. let x = self.x + other.x;
  33. let y = self.y + other.y;
  34. Vec2 { x, y }
  35. }
  36. }
  37. impl<'a> Add<Vec2> for &'a Vec2 {
  38. type Output = Vec2;
  39. fn add(self, other: Vec2) -> Vec2 {
  40. let x = self.x + other.x;
  41. let y = self.y + other.y;
  42. Vec2 { x, y }
  43. }
  44. }
  45. impl<'a> Add<&'a Vec2> for &'a Vec2 {
  46. type Output = Vec2;
  47. fn add(self, other: &'a Vec2) -> Vec2 {
  48. let x = self.x + other.x;
  49. let y = self.y + other.y;
  50. Vec2 { x, y }
  51. }
  52. }