From: Tomas Wenström Date: Wed, 24 Feb 2021 18:29:53 +0000 (+0100) Subject: Moved out objects from game module X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=5d7eff9e9c0f585e9c4d57907f2e72003d39c757;p=kaka%2Frust-sdl-test.git Moved out objects from game module --- diff --git a/src/core/game.rs b/src/core/game.rs index d9df53a..c356402 100644 --- a/src/core/game.rs +++ b/src/core/game.rs @@ -1,17 +1,13 @@ use AppState; use core::app::StateChange; -use core::controller::Controller; use core::controller::ControllerManager; -use core::level::{Level, LevelGenerator, Wall, IntersectResult::Intersection}; +use core::level::{Level, LevelGenerator}; +use core::object::{Object, Objects, ObjectState}; +use core::object::character::Character; use core::render::Renderer; -use geometry::{Point, ToAngle}; -use point; use sdl2::event::Event; use sdl2::keyboard::Keycode; -use sdl2::rect::Rect; use sprites::SpriteManager; -use std::cell::RefCell; -use std::rc::Rc; use teststate::TestState; use time::Duration; @@ -114,7 +110,7 @@ impl World { 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 { + if self.objects[i].update(&mut breeding_ground, &self.level, dt) == ObjectState::Dead { self.objects.remove(i); // swap_remove is more efficient, but changes the order of the array } } @@ -137,171 +133,3 @@ impl World { self.objects.push(object); } } - -////////// OBJECT ////////////////////////////////////////////////////////////// - -type Objects = Vec>; - -pub trait Object { - fn update(&mut self, objects: &mut Objects, lvl: &Level, dt: Duration) -> ObjectState; - fn render(&self, _renderer: &mut Renderer, _sprites: &SpriteManager) {} -} - -#[derive(PartialEq)] -pub enum ObjectState { Alive, Dead } -use self::ObjectState::*; - - -pub trait Physical {} -pub trait Drawable {} - -////////// CHARACTER /////////////////////////////////////////////////////////// - -pub struct Character { - ctrl: Rc>, - pos: Point, - vel: Point, - standing_on: Option, -} - -impl Character { - pub fn new(ctrl: Rc>) -> Self { - Character { - ctrl, - pos: point!(300.0, 300.0), - vel: point!(0.0, 0.0), - standing_on: None, - } - } -} - -impl Object for Character { - fn update(&mut self, objects: &mut Objects, lvl: &Level, dt: Duration) -> ObjectState { - let ctrl = self.ctrl.borrow(); - - match &self.standing_on { - Some(wall) => { - if ctrl.jump.is_pressed && !ctrl.jump.was_pressed { - if ctrl.mov.to_point().length() < 0.1 { - self.vel = wall.normal().into(); - } else { - self.vel = ctrl.mov.to_point(); - } - self.vel *= 5.0; - self.pos += self.vel * 0.1; - self.standing_on = None; - } else { - self.vel *= 0.9; - } - }, - None => { - self.vel += lvl.gravity; - self.pos += self.vel; - - match ctrl.mov.x { - v if v < -0.9 && self.vel.x > -5.0 => { self.vel.x -= 0.5 } - v if v > 0.9 && self.vel.x < 5.0 => { self.vel.x += 0.5 } - _ => {} - } - - if let Intersection(wall, pos) = lvl.intersect_walls(self.pos - self.vel, self.pos) { - self.standing_on = Some(wall); - self.pos = pos; - self.vel = point!(0.0, 0.0); - } - } - } - - if ctrl.shoot.is_pressed { - use rand::distributions::{Distribution, Normal}; - let normal = Normal::new(0.0, 0.1); - let direction = if ctrl.aim.to_point().length() > 0.1 { ctrl.aim.to_point() } else { ctrl.mov.to_point() }; - for _i in 0..100 { - objects.push(Box::new(Boll { - pos: self.pos + point!(0.0, -16.0), // half the height of mario - vel: direction * (10.0 + rand::random::()) + point!(normal.sample(&mut rand::thread_rng()), normal.sample(&mut rand::thread_rng())) + self.vel, - bounces: 2, - })); - } - ctrl.rumble(1.0, dt); - self.vel -= direction * 0.1; - } - - Alive - } - - fn render(&self, renderer: &mut Renderer, sprites: &SpriteManager) { - let block = sprites.get("mario"); - let size = 32; - renderer.blit(block, None, Rect::new(self.pos.x as i32 - size as i32 / 2, self.pos.y as i32 - size as i32, size, size)); - - let ctrl = &self.ctrl.borrow(); - let l = 300.0; - let pos = (self.pos.x as i32, self.pos.y as i32); - // // axis values - // let p = (self.pos + ctrl.aim.to_axis_point() * l).to_i32().into(); - // renderer.draw_line(pos, p, (0, 255, 0)); - // draw_cross(renderer, p); - // values limited to unit vector - let p = (self.pos + ctrl.aim.to_point() * l).to_i32().into(); - renderer.draw_line(pos, p, (255, 0, 0)); - draw_cross(renderer, p); - let p = (self.pos + ctrl.mov.to_point() * l).to_i32().into(); - renderer.draw_line(pos, p, (0, 255, 0)); - draw_cross(renderer, p); - // // circle values - // let p = (self.pos + Point::from(ctrl.aim.a) * l).to_i32().into(); - // renderer.draw_line(pos, p, (0, 0, 255)); - // draw_cross(renderer, p); - } -} - -fn draw_cross(renderer: &mut Renderer, p: (i32, i32)) { - renderer.canvas().draw_line((p.0 - 5, p.1), (p.0 + 5, p.1)).unwrap(); - renderer.canvas().draw_line((p.0, p.1 - 5), (p.0, p.1 + 5)).unwrap(); -} - -////////// BOLL //////////////////////////////////////////////////////////////// - -pub struct Boll { - pos: Point, - vel: Point, - bounces: u8, -} - -impl Object for Boll { - fn update(&mut self, objects: &mut Objects, lvl: &Level, _dt: Duration) -> ObjectState { - self.vel += lvl.gravity; - self.pos += self.vel; - - if let Intersection(wall, pos) = lvl.intersect_walls(self.pos - self.vel, self.pos) { - if self.bounces == 0 { - return Dead - } - self.bounces -= 1; - let mut a = wall.normal().mirror(self.vel.to_angle()); // TODO interpolera normalen mellan närliggande väggdelar? bollarna studsar väldigt "kantigt" nu - self.pos = pos + Point::from(wall.normal()) * 0.1; // fÃ¥r bollen att inte Ã¥ka igenom väggen av misstag p.g.a nedan slumpvinkel - self.vel = Point::from(a) * self.vel.length() * 0.35; - - // create another boll - use rand::distributions::{Distribution, Normal}; - let mut rng = rand::thread_rng(); - a += Normal::new(0.0, 0.1).sample(&mut rng).radians(); // TODO slumpen kan ge en vinkel som är under tangenten. vinkel-metoder pÃ¥ väggen istället kanske? - use rand::Rng; - objects.push(Box::new(Boll { - vel: Point::from(a) * Normal::new(1.0, 0.25).sample(&mut rng) * self.vel.length() * rng.gen_range(0.25, 1.0), - ..*self - })); - } - - Alive - } - - fn render(&self, renderer: &mut Renderer, _sprites: &SpriteManager) { - let block = _sprites.get("block"); - let size = 4 + self.bounces * 6; - renderer.blit(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)); - // renderer.canvas().set_draw_color((0, self.bounces * 100, 255)); - // renderer.canvas().draw_point((self.pos.x as i32, self.pos.y as i32)).unwrap(); - } -} diff --git a/src/core/mod.rs b/src/core/mod.rs index 9e25ba4..c37e19b 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -1,5 +1,6 @@ pub mod app; pub mod controller; pub mod game; -pub mod render; pub mod level; +pub mod object; +pub mod render; diff --git a/src/core/object/boll.rs b/src/core/object/boll.rs new file mode 100644 index 0000000..adae989 --- /dev/null +++ b/src/core/object/boll.rs @@ -0,0 +1,59 @@ +use core::level::{Level, IntersectResult::Intersection}; +use core::object::{Object, Objects, ObjectState}; +use core::render::Renderer; +use geometry::{Point, ToAngle}; +use sdl2::rect::Rect; +use sprites::SpriteManager; +use time::Duration; + +////////// BOLL //////////////////////////////////////////////////////////////// + +pub struct Boll { + pos: Point, + vel: Point, + bounces: u8, +} + +impl Boll { + pub fn new(pos: Point, vel: Point, bounces: u8) -> Self { + Boll { pos, vel, bounces } + } +} + +impl Object for Boll { + + fn update(&mut self, objects: &mut Objects, lvl: &Level, _dt: Duration) -> ObjectState { + self.vel += lvl.gravity; + self.pos += self.vel; + + if let Intersection(wall, pos) = lvl.intersect_walls(self.pos - self.vel, self.pos) { + if self.bounces == 0 { + return ObjectState::Dead + } + self.bounces -= 1; + let mut a = wall.normal().mirror(self.vel.to_angle()); // TODO interpolera normalen mellan närliggande väggdelar? bollarna studsar väldigt "kantigt" nu + self.pos = pos + Point::from(wall.normal()) * 0.1; // fÃ¥r bollen att inte Ã¥ka igenom väggen av misstag p.g.a nedan slumpvinkel + self.vel = Point::from(a) * self.vel.length() * 0.35; + + // create another boll + use rand::distributions::{Distribution, Normal}; + let mut rng = rand::thread_rng(); + a += Normal::new(0.0, 0.1).sample(&mut rng).radians(); // TODO slumpen kan ge en vinkel som är under tangenten. vinkel-metoder pÃ¥ väggen istället kanske? + use rand::Rng; + objects.push(Box::new(Boll { + vel: Point::from(a) * Normal::new(1.0, 0.25).sample(&mut rng) * self.vel.length() * rng.gen_range(0.25, 1.0), + ..*self + })); + } + + ObjectState::Alive + } + + fn render(&self, renderer: &mut Renderer, _sprites: &SpriteManager) { + let block = _sprites.get("block"); + let size = 4 + self.bounces * 6; + renderer.blit(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)); + // renderer.canvas().set_draw_color((0, self.bounces * 100, 255)); + // renderer.canvas().draw_point((self.pos.x as i32, self.pos.y as i32)).unwrap(); + } +} diff --git a/src/core/object/character.rs b/src/core/object/character.rs new file mode 100644 index 0000000..8632ee2 --- /dev/null +++ b/src/core/object/character.rs @@ -0,0 +1,118 @@ +use core::controller::Controller; +use core::object::boll::Boll; +use core::level::{Level, Wall, IntersectResult::Intersection}; +use core::object::{Object, Objects, ObjectState}; +use core::render::Renderer; +use geometry::Point; +use point; +use sdl2::rect::Rect; +use sprites::SpriteManager; +use std::cell::RefCell; +use std::rc::Rc; +use time::Duration; + +////////// CHARACTER /////////////////////////////////////////////////////////// + +pub struct Character { + ctrl: Rc>, + pos: Point, + vel: Point, + standing_on: Option, +} + +impl Character { + pub fn new(ctrl: Rc>) -> Self { + Character { + ctrl, + pos: point!(300.0, 300.0), + vel: point!(0.0, 0.0), + standing_on: None, + } + } +} + +impl Object for Character { + fn update(&mut self, objects: &mut Objects, lvl: &Level, dt: Duration) -> ObjectState { + let ctrl = self.ctrl.borrow(); + + match &self.standing_on { + Some(wall) => { + if ctrl.jump.is_pressed && !ctrl.jump.was_pressed { + if ctrl.mov.to_point().length() < 0.1 { + self.vel = wall.normal().into(); + } else { + self.vel = ctrl.mov.to_point(); + } + self.vel *= 5.0; + self.pos += self.vel * 0.1; + self.standing_on = None; + } else { + self.vel *= 0.9; + } + }, + None => { + self.vel += lvl.gravity; + self.pos += self.vel; + + match ctrl.mov.x { + v if v < -0.9 && self.vel.x > -5.0 => { self.vel.x -= 0.5 } + v if v > 0.9 && self.vel.x < 5.0 => { self.vel.x += 0.5 } + _ => {} + } + + if let Intersection(wall, pos) = lvl.intersect_walls(self.pos - self.vel, self.pos) { + self.standing_on = Some(wall); + self.pos = pos; + self.vel = point!(0.0, 0.0); + } + } + } + + if ctrl.shoot.is_pressed { + use rand::distributions::{Distribution, Normal}; + let normal = Normal::new(0.0, 0.1); + let direction = if ctrl.aim.to_point().length() > 0.1 { ctrl.aim.to_point() } else { ctrl.mov.to_point() }; + for _i in 0..100 { + objects.push(Box::new(Boll::new( + self.pos + point!(0.0, -16.0), // half the height of mario + direction * (10.0 + rand::random::()) + point!(normal.sample(&mut rand::thread_rng()), normal.sample(&mut rand::thread_rng())) + self.vel, + 2, + ))); + } + ctrl.rumble(1.0, dt); + self.vel -= direction * 0.1; + } + + ObjectState::Alive + } + + fn render(&self, renderer: &mut Renderer, sprites: &SpriteManager) { + let block = sprites.get("mario"); + let size = 32; + renderer.blit(block, None, Rect::new(self.pos.x as i32 - size as i32 / 2, self.pos.y as i32 - size as i32, size, size)); + + let ctrl = &self.ctrl.borrow(); + let l = 300.0; + let pos = (self.pos.x as i32, self.pos.y as i32); + // // axis values + // let p = (self.pos + ctrl.aim.to_axis_point() * l).to_i32().into(); + // renderer.draw_line(pos, p, (0, 255, 0)); + // draw_cross(renderer, p); + // values limited to unit vector + let p = (self.pos + ctrl.aim.to_point() * l).to_i32().into(); + renderer.draw_line(pos, p, (255, 0, 0)); + draw_cross(renderer, p); + let p = (self.pos + ctrl.mov.to_point() * l).to_i32().into(); + renderer.draw_line(pos, p, (0, 255, 0)); + draw_cross(renderer, p); + // // circle values + // let p = (self.pos + Point::from(ctrl.aim.a) * l).to_i32().into(); + // renderer.draw_line(pos, p, (0, 0, 255)); + // draw_cross(renderer, p); + } +} + +fn draw_cross(renderer: &mut Renderer, p: (i32, i32)) { + renderer.canvas().draw_line((p.0 - 5, p.1), (p.0 + 5, p.1)).unwrap(); + renderer.canvas().draw_line((p.0, p.1 - 5), (p.0, p.1 + 5)).unwrap(); +} diff --git a/src/core/object/mod.rs b/src/core/object/mod.rs new file mode 100644 index 0000000..2ecf042 --- /dev/null +++ b/src/core/object/mod.rs @@ -0,0 +1,22 @@ +use core::level::Level; +use core::render::Renderer; +use sprites::SpriteManager; +use time::Duration; + +pub mod boll; +pub mod character; + +pub type Objects = Vec>; + +////////// OBJECT ////////////////////////////////////////////////////////////// + +pub trait Object { + fn update(&mut self, objects: &mut Objects, lvl: &Level, dt: Duration) -> ObjectState; + fn render(&self, _renderer: &mut Renderer, _sprites: &SpriteManager) {} +} + +#[derive(PartialEq)] +pub enum ObjectState { Alive, Dead } + +pub trait Physical {} +pub trait Drawable {}