Fixed warning - irrefutable let pattern
[kaka/rust-sdl-test.git] / src / boll.rs
CommitLineData
6566d7e5 1use core::render::Renderer;
296187ca 2use sdl2::rect::Rect;
296187ca 3
e570927a 4use common::Point;
6c5dd5cf 5use sdl2::gfx::primitives::DrawRenderer;
6ba7aef1 6use {SCREEN_HEIGHT, SCREEN_WIDTH};
296187ca 7
c315bb31
TW
8pub trait Boll {
9 fn update(&mut self);
6566d7e5 10 fn draw(&self, renderer: &mut Renderer, size: u32);
c315bb31
TW
11}
12
13pub struct SquareBoll {
e570927a
TW
14 pub pos: Point<f64>,
15 pub vel: Point<f64>,
296187ca
TW
16}
17
c315bb31
TW
18impl Boll for SquareBoll {
19 fn update(&mut self) {
296187ca
TW
20 self.vel.y += 0.1;
21 self.pos += self.vel;
22
23 if self.pos.x < 0.0 {
24 self.pos.x = -self.pos.x;
25 self.vel.x = -self.vel.x;
26 }
27 if self.pos.x > SCREEN_WIDTH as f64 {
28 self.pos.x = SCREEN_WIDTH as f64 - (self.pos.x - SCREEN_WIDTH as f64);
29 self.vel.x = -self.vel.x;
30 }
31 if self.pos.y < 0.0 {
32 self.pos.y = -self.pos.y;
33 self.vel.y = -self.vel.y;
34 }
35 if self.pos.y > SCREEN_HEIGHT as f64 {
36 self.pos.y = SCREEN_HEIGHT as f64 - (self.pos.y - SCREEN_HEIGHT as f64);
37 self.vel.y = -self.vel.y;
38 }
39 }
40
6566d7e5
TW
41 fn draw(&self, renderer: &mut Renderer, size: u32) {
42 renderer.canvas().set_draw_color((
296187ca
TW
43 255 - std::cmp::min(255, (self.vel.length() * 25.0) as u8),
44 (255.0 * (self.pos.x / SCREEN_WIDTH as f64)) as u8,
6ba7aef1
TW
45 (255.0 * (self.pos.y / SCREEN_HEIGHT as f64)) as u8,
46 128,
296187ca
TW
47 ));
48 let mut r = Rect::new(0, 0, size, size);
6566d7e5
TW
49 r.center_on((self.pos.x as i32, self.pos.y as i32));
50 renderer.canvas().fill_rect(r).unwrap();
296187ca
TW
51 }
52}
6c5dd5cf 53
6c5dd5cf
TW
54pub struct CircleBoll {
55 pub boll: SquareBoll,
56}
57
58impl CircleBoll {
e570927a 59 pub fn new(pos: Point<f64>, vel: Point<f64>) -> CircleBoll {
6c5dd5cf 60 CircleBoll {
6ba7aef1 61 boll: SquareBoll { pos, vel },
6c5dd5cf
TW
62 }
63 }
64}
65
66impl Boll for CircleBoll {
67 fn update(&mut self) {
68 self.boll.update();
69 }
70
6566d7e5 71 fn draw(&self, renderer: &mut Renderer, size: u32) {
6c5dd5cf 72 let val = 255 - std::cmp::min(255, (self.boll.vel.length() * 20.0) as u8);
6566d7e5
TW
73 renderer
74 .canvas()
6ba7aef1
TW
75 .filled_circle(
76 self.boll.pos.x as i16,
77 self.boll.pos.y as i16,
78 size as i16,
6566d7e5 79 (val, val, val, 128),
6ba7aef1
TW
80 )
81 .unwrap();
6c5dd5cf
TW
82 }
83}