Removed old code
[kaka/rust-sdl-test.git] / src / boll.rs
... / ...
CommitLineData
1use core::render::Renderer;
2use sdl2::rect::Rect;
3
4use common::Point;
5use sdl2::gfx::primitives::DrawRenderer;
6use {SCREEN_HEIGHT, SCREEN_WIDTH};
7
8pub trait Boll {
9 fn update(&mut self);
10 fn draw(&self, renderer: &mut Renderer, size: u32);
11}
12
13pub struct SquareBoll {
14 pub pos: Point<f64>,
15 pub vel: Point<f64>,
16}
17
18impl Boll for SquareBoll {
19 fn update(&mut self) {
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
41 fn draw(&self, renderer: &mut Renderer, size: u32) {
42 renderer.canvas().set_draw_color((
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,
45 (255.0 * (self.pos.y / SCREEN_HEIGHT as f64)) as u8,
46 128,
47 ));
48 let mut r = Rect::new(0, 0, size, size);
49 r.center_on((self.pos.x as i32, self.pos.y as i32));
50 renderer.canvas().fill_rect(r).unwrap();
51 }
52}
53
54pub struct CircleBoll {
55 pub boll: SquareBoll,
56}
57
58impl CircleBoll {
59 pub fn new(pos: Point<f64>, vel: Point<f64>) -> CircleBoll {
60 CircleBoll {
61 boll: SquareBoll { pos, vel },
62 }
63 }
64}
65
66impl Boll for CircleBoll {
67 fn update(&mut self) {
68 self.boll.update();
69 }
70
71 fn draw(&self, renderer: &mut Renderer, size: u32) {
72 let val = 255 - std::cmp::min(255, (self.boll.vel.length() * 20.0) as u8);
73 renderer
74 .canvas()
75 .filled_circle(
76 self.boll.pos.x as i16,
77 self.boll.pos.y as i16,
78 size as i16,
79 (val, val, val, 128),
80 )
81 .unwrap();
82 }
83}