use sdl2::video::WindowContext;
use time::PreciseTime;
-use boll::Boll;
+use boll::{Boll, SquareBoll, CircleBoll};
use common::Point2D;
#[macro_use] mod common;
const FPS: u32 = 60;
const NS_PER_FRAME: u32 = 1_000_000_000 / FPS;
+type Bollar = Vec<Box<dyn Boll>>;
+
fn init() -> (Canvas<Window>, EventPump) {
let context = sdl2::init().unwrap();
let video = context.video().unwrap();
let mut frame_count: u64 = 0;
let mut fps_time = PreciseTime::now();
- let mut bolls: Vec<Boll> = Vec::new();
+ let mut bolls: Bollar = Bollar::new();
let mut boll_size = 1;
let texture_creator = canvas.texture_creator();
}
Event::KeyDown { keycode: Some(Keycode::KpPlus), .. } => { boll_size = std::cmp::min(boll_size + 1, 32) }
Event::KeyDown { keycode: Some(Keycode::KpMinus), .. } => { boll_size = std::cmp::max(boll_size - 1, 1) }
- Event::MouseMotion { x, y, .. } => { bolls.push(Boll {
- pos: point!(x as f64, y as f64),
- vel: point!(0.0, 0.0),
- }) }
+ Event::MouseMotion { x, y, .. } => {
+ bolls.push(Box::new(CircleBoll::new(
+ point!(x as f64, y as f64),
+ point!(0.0, 0.0),
+ )))
+ }
_ => {}
}
}
println!("number of bolls: {}", bolls.len());
}
-fn change_boll_count(mut bolls: &mut Vec<Boll>, delta: i32) {
+fn change_boll_count(mut bolls: &mut Bollar, delta: i32) {
if delta > 0 {
for _i in 0..delta {
add_boll(&mut bolls);
}
}
-fn add_boll(bolls: &mut Vec<Boll>) {
+fn add_boll(bolls: &mut Bollar) {
let mut rng = rand::thread_rng();
- bolls.push(Boll {
+ bolls.push(Box::new(SquareBoll {
pos: point!(rng.gen_range(0, SCREEN_WIDTH) as f64, rng.gen_range(0, SCREEN_HEIGHT) as f64),
vel: point!(rng.gen_range(-2.0, 2.0), rng.gen_range(-2.0, 2.0)),
- });
+ }));
}