New boll - CircleBoll
authorTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 10 Feb 2019 12:30:04 +0000 (13:30 +0100)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 10 Feb 2019 12:30:04 +0000 (13:30 +0100)
src/boll.rs
src/main.rs

index f936fca..35dd97e 100644 (file)
@@ -6,6 +6,7 @@ use sdl2::video::Window;
 
 use {SCREEN_HEIGHT, SCREEN_WIDTH};
 use common::Point2D;
+use sdl2::gfx::primitives::DrawRenderer;
 
 pub trait Boll {
     fn update(&mut self);
@@ -51,3 +52,35 @@ impl Boll for SquareBoll {
         canvas.fill_rect(r).unwrap();
     }
 }
+
+
+pub struct CircleBoll {
+    pub boll: SquareBoll,
+}
+
+impl CircleBoll {
+    pub fn new(pos: Point2D<f64>, vel: Point2D<f64>) -> CircleBoll {
+        CircleBoll {
+            boll: SquareBoll {
+                pos,
+                vel,
+            }
+        }
+    }
+}
+
+impl Boll for CircleBoll {
+    fn update(&mut self) {
+        self.boll.update();
+    }
+
+    fn draw(&mut self, canvas: &mut Canvas<Window>, size: u32) {
+        let val = 255 - std::cmp::min(255, (self.boll.vel.length() * 20.0) as u8);
+        canvas.filled_circle(self.boll.pos.x as i16, self.boll.pos.y as i16, size as i16, Color::RGBA(
+            val,
+            val,
+            val,
+            128,
+        )).unwrap();
+    }
+}
index 0cb2ecf..d99da89 100644 (file)
@@ -22,7 +22,7 @@ use sdl2::video::Window;
 use sdl2::video::WindowContext;
 use time::PreciseTime;
 
-use boll::{Boll, SquareBoll};
+use boll::{Boll, SquareBoll, CircleBoll};
 use common::Point2D;
 
 #[macro_use] mod common;
@@ -125,10 +125,12 @@ fn main() {
                 }
                 Event::KeyDown { keycode: Some(Keycode::KpPlus), .. } => { boll_size = std::cmp::min(boll_size + 1, 32) }
                 Event::KeyDown { keycode: Some(Keycode::KpMinus), .. } => { boll_size = std::cmp::max(boll_size - 1, 1) }
-                Event::MouseMotion { x, y, .. } => { bolls.push(Box::new(SquareBoll {
-                    pos: point!(x as f64, y as f64),
-                    vel: point!(0.0, 0.0),
-                })) }
+                Event::MouseMotion { x, y, .. } => {
+                    bolls.push(Box::new(CircleBoll::new(
+                        point!(x as f64, y as f64),
+                        point!(0.0, 0.0),
+                    )))
+                }
                 _ => {}
             }
         }