X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fboll.rs;h=64be265cf6fd92798d418d65500cd7c96c191490;hb=fea68d56aa10f9e91f922323676ef7ff468340f6;hp=b273f1d3145a8d6a5bf44b39e0ffed86f5f00369;hpb=296187ca027364405cf2fd95e727d5d1eaaec4fc;p=kaka%2Frust-sdl-test.git diff --git a/src/boll.rs b/src/boll.rs index b273f1d..64be265 100644 --- a/src/boll.rs +++ b/src/boll.rs @@ -6,14 +6,20 @@ use sdl2::video::Window; use {SCREEN_HEIGHT, SCREEN_WIDTH}; use common::Point2D; +use sdl2::gfx::primitives::DrawRenderer; -pub struct Boll { +pub trait Boll { + fn update(&mut self); + fn draw(&self, canvas: &mut Canvas, size: u32); +} + +pub struct SquareBoll { pub pos: Point2D, pub vel: Point2D, } -impl Boll { - pub fn update(&mut self) { +impl Boll for SquareBoll { + fn update(&mut self) { self.vel.y += 0.1; self.pos += self.vel; @@ -35,7 +41,7 @@ impl Boll { } } - pub fn draw(&mut self,canvas: &mut Canvas, size: u32) { + fn draw(&self, canvas: &mut Canvas, 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, @@ -46,3 +52,35 @@ impl Boll { canvas.fill_rect(r).unwrap(); } } + + +pub struct CircleBoll { + pub boll: SquareBoll, +} + +impl CircleBoll { + pub fn new(pos: Point2D, vel: Point2D) -> CircleBoll { + CircleBoll { + boll: SquareBoll { + pos, + vel, + } + } + } +} + +impl Boll for CircleBoll { + fn update(&mut self) { + self.boll.update(); + } + + fn draw(&self, canvas: &mut Canvas, 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(); + } +}