expect() with formatting -> unwrap_or_else()
[kaka/rust-sdl-test.git] / src / boll.rs
index f936fca..64be265 100644 (file)
@@ -6,10 +6,11 @@ use sdl2::video::Window;
 
 use {SCREEN_HEIGHT, SCREEN_WIDTH};
 use common::Point2D;
+use sdl2::gfx::primitives::DrawRenderer;
 
 pub trait Boll {
     fn update(&mut self);
-    fn draw(&mut self, canvas: &mut Canvas<Window>, size: u32);
+    fn draw(&self, canvas: &mut Canvas<Window>, size: u32);
 }
 
 pub struct SquareBoll {
@@ -40,7 +41,7 @@ impl Boll for SquareBoll {
         }
     }
 
-    fn draw(&mut self, canvas: &mut Canvas<Window>, size: u32) {
+    fn draw(&self, canvas: &mut Canvas<Window>, size: u32) {
         canvas.set_draw_color(Color::RGBA(
             255 - std::cmp::min(255, (self.vel.length() * 25.0) as u8),
             (255.0 * (self.pos.x / SCREEN_WIDTH as f64)) as u8,
@@ -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(&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();
+    }
+}