From 3583c453df58103da76156b8971d36cb4cab35b3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Sun, 17 Jan 2021 10:40:20 +0100 Subject: [PATCH] Added simple bolls mario can shoot --- src/core/game.rs | 87 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 77 insertions(+), 10 deletions(-) diff --git a/src/core/game.rs b/src/core/game.rs index f4a16f0..bf6d0ba 100644 --- a/src/core/game.rs +++ b/src/core/game.rs @@ -53,7 +53,7 @@ impl AppState for GameState { #[derive(Default)] pub struct World { level: Level, - objects: Vec>, + objects: Objects, } impl World { @@ -68,8 +68,16 @@ 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); } } @@ -106,11 +114,18 @@ impl Level { ////////// OBJECT ////////////////////////////////////////////////////////////// +type Objects = Vec>; + pub trait Object { - fn update(&mut self, lvl: &Level, dt: Nanoseconds); - fn render(&mut self, canvas: &mut Canvas, _sprites: &mut SpriteManager); + fn update(&mut self, objects: &mut Objects, lvl: &Level, dt: Nanoseconds) -> ObjectState; + fn render(&self, _canvas: &mut Canvas, _sprites: &mut SpriteManager) {} } +#[derive(PartialEq)] +pub enum ObjectState { Alive, Dead } +use self::ObjectState::*; + + pub trait Physical {} pub trait Drawable {} @@ -133,19 +148,32 @@ impl Character { } 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::()) + point!(normal.sample(&mut rand::thread_rng()), normal.sample(&mut rand::thread_rng())) + self.vel, + bounces: 2, + })); } } @@ -154,11 +182,50 @@ impl Object for Character { v if v > 0.9 => { self.vel.x += 0.5 } _ => {} } + + Alive } - fn render(&mut self, canvas: &mut Canvas, sprites: &mut SpriteManager) { + fn render(&self, canvas: &mut Canvas, 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, + vel: Point2D, + 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, _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(); } } -- 2.11.0