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;
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
}
}
self.objects.push(object);
}
}
-
-////////// OBJECT //////////////////////////////////////////////////////////////
-
-type Objects = Vec<Box<dyn 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 }
-use self::ObjectState::*;
-
-
-pub trait Physical {}
-pub trait Drawable {}
-
-////////// CHARACTER ///////////////////////////////////////////////////////////
-
-pub struct Character {
- ctrl: Rc<RefCell<Controller>>,
- pos: Point<f64>,
- vel: Point<f64>,
- standing_on: Option<Wall>,
-}
-
-impl Character {
- pub fn new(ctrl: Rc<RefCell<Controller>>) -> 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::<f64>()) + 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<f64>,
- vel: Point<f64>,
- 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();
- }
-}
--- /dev/null
+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<f64>,
+ vel: Point<f64>,
+ bounces: u8,
+}
+
+impl Boll {
+ pub fn new(pos: Point<f64>, vel: Point<f64>, 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();
+ }
+}
--- /dev/null
+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<RefCell<Controller>>,
+ pos: Point<f64>,
+ vel: Point<f64>,
+ standing_on: Option<Wall>,
+}
+
+impl Character {
+ pub fn new(ctrl: Rc<RefCell<Controller>>) -> 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::<f64>()) + 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();
+}