#[derive(Default)]
pub struct World {
level: Level,
- objects: Vec<Box<dyn Object>>,
+ objects: Objects,
}
impl World {
}
pub fn update(&mut self, dt: Nanoseconds) {
- for o in &mut self.objects {
- o.update(&self.level, dt);
+ let mut breeding_ground = vec!();
+
+ for i in (0..self.objects.len()).rev() {
+ if self.objects[i].update(&mut breeding_ground, &self.level, dt) == Dead {
+ self.objects.remove(i); // swap_remove is more efficient, but changes the order of the array
+ }
+ }
+
+ for o in breeding_ground {
+ self.add(o);
}
}
////////// OBJECT //////////////////////////////////////////////////////////////
+type Objects = Vec<Box<dyn Object>>;
+
pub trait Object {
- fn update(&mut self, lvl: &Level, dt: Nanoseconds);
- fn render(&mut self, canvas: &mut Canvas<Window>, _sprites: &mut SpriteManager);
+ fn update(&mut self, objects: &mut Objects, lvl: &Level, dt: Nanoseconds) -> ObjectState;
+ fn render(&self, _canvas: &mut Canvas<Window>, _sprites: &mut SpriteManager) {}
}
+#[derive(PartialEq)]
+pub enum ObjectState { Alive, Dead }
+use self::ObjectState::*;
+
+
pub trait Physical {}
pub trait Drawable {}
}
impl Object for Character {
- fn update(&mut self, lvl: &Level, _dt: Nanoseconds) {
+ fn update(&mut self, objects: &mut Objects, lvl: &Level, _dt: Nanoseconds) -> ObjectState {
self.vel += lvl.gravity;
self.pos += self.vel;
let ctrl = &self.ctrl.borrow().ctrl;
+ let right_stick = point!(ctrl.axis(Axis::RightX) as f64 / 32768.0, ctrl.axis(Axis::RightY) as f64 / 32768.0);
if self.pos.y >= lvl.ground {
self.pos.y = lvl.ground;
self.vel.y = 0.0;
self.vel.x *= 0.9;
- if ctrl.button(Button::A) {
- self.vel.y = -5.0;
+ if ctrl.button(Button::LeftShoulder) {
+ self.vel = right_stick * 5.0;
+ }
+ }
+
+ if ctrl.button(Button::RightShoulder) {
+ use rand::distributions::{Distribution, Normal};
+ let normal = Normal::new(0.0, 0.1);
+ for _i in 0..100 {
+ objects.push(Box::new(Boll {
+ pos: self.pos,
+ vel: right_stick * (3.0 + rand::random::<f64>()) + point!(normal.sample(&mut rand::thread_rng()), normal.sample(&mut rand::thread_rng())) + self.vel,
+ bounces: 2,
+ }));
}
}
v if v > 0.9 => { self.vel.x += 0.5 }
_ => {}
}
+
+ Alive
}
- fn render(&mut self, canvas: &mut Canvas<Window>, sprites: &mut SpriteManager) {
+ fn render(&self, canvas: &mut Canvas<Window>, sprites: &mut SpriteManager) {
let block = sprites.get("mario");
let size = 32;
- canvas.copy(block, None, Rect::new(self.pos.x as i32, self.pos.y as i32 - size as i32, size, size)).unwrap();
+ canvas.copy(block, None, Rect::new(self.pos.x as i32 - size as i32 / 2, self.pos.y as i32 - size as i32, size, size)).unwrap();
+ let ctrl = &self.ctrl.borrow().ctrl;
+ let (x, y) = (ctrl.axis(Axis::RightX) as f64 / 32768.0, ctrl.axis(Axis::RightY) as f64 / 32768.0);
+ canvas.set_draw_color((0, 255, 0));
+ canvas.draw_line((self.pos.x as i32, self.pos.y as i32), ((self.pos.x + x * size as f64) as i32, (self.pos.y + y * size as f64) as i32)).unwrap();
+ }
+}
+
+pub struct Boll {
+ pos: Point2D<f64>,
+ vel: Point2D<f64>,
+ bounces: u8,
+}
+
+impl Object for Boll {
+ fn update(&mut self, _objects: &mut Objects, lvl: &Level, _dt: Nanoseconds) -> ObjectState {
+ self.vel += lvl.gravity;
+ self.pos += self.vel;
+
+ if self.pos.y >= lvl.ground {
+ if self.bounces == 0 {
+ return Dead
+ } else {
+ self.bounces -= 1;
+ self.pos.y = lvl.ground;
+ self.vel.y = -self.vel.y;
+ }
+ }
+
+ Alive
+ }
+
+ fn render(&self, canvas: &mut Canvas<Window>, _sprites: &mut SpriteManager) {
+ let block = _sprites.get("block");
+ let size = 4 + self.bounces * 6;
+ canvas.copy(block, None, Rect::new(self.pos.x as i32 - size as i32 / 2, self.pos.y as i32 - size as i32 / 2, size as u32, size as u32)).unwrap();
+ // canvas.set_draw_color((0, self.bounces * 100, 255));
+ // canvas.draw_point((self.pos.x as i32, self.pos.y as i32)).unwrap();
}
}