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