X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fcore%2Fgame.rs;h=429a2ee67a864a7c69cbf038a3c123f169dae972;hb=3fd8761bc3123ed0de1c5416c7efbbb931257248;hp=3e017b168e401759a68b4397d4ee0eeddf50af33;hpb=c80ba7f72596437b28540f463b952e6dcbce6295;p=kaka%2Frust-sdl-test.git diff --git a/src/core/game.rs b/src/core/game.rs index 3e017b1..429a2ee 100644 --- a/src/core/game.rs +++ b/src/core/game.rs @@ -31,8 +31,8 @@ impl GameState { impl AppState for GameState { fn enter(&mut self, ctrl_man: &ControllerManager) { - if let Some(ctrl) = ctrl_man.controllers.get(&0) { - self.world.add(Box::new(Character::new(ctrl.clone()))); + for (_k, v) in ctrl_man.controllers.iter() { + self.world.add(Box::new(Character::new(v.clone()))); } } @@ -76,7 +76,7 @@ pub struct World { impl World { pub fn new() -> Self { World { - level: Level::new(point!(0.0, 0.1), 600.0), + level: Level::new(point!(0.0, 0.1)), ..Default::default() } } @@ -93,6 +93,8 @@ impl World { for o in breeding_ground { self.add(o); } + + println!("\x1b[Kobject count: {}\x1b[1A", self.objects.len()); // clear line, print, move cursor up } pub fn render(&mut self, renderer: &mut Renderer, sprites: &SpriteManager) { @@ -136,7 +138,7 @@ impl Character { pub fn new(ctrl: Rc>) -> Self { Character { ctrl, - pos: point!(100.0, 100.0), + pos: point!(300.0, 300.0), vel: point!(0.0, 0.0), } } @@ -144,32 +146,40 @@ impl Character { impl Object for Character { fn update(&mut self, objects: &mut Objects, lvl: &Level, dt: Duration) -> ObjectState { - self.vel += lvl.gravity; - self.pos += self.vel; - let ctrl = self.ctrl.borrow(); - if self.pos.y >= lvl.ground { - self.pos.y = lvl.ground; - self.vel.y = 0.0; - self.vel.x *= 0.9; + let x = (self.pos.x / lvl.grid.cell_size as f64).min(lvl.grid.width as f64 - 1.0).max(0.0) as usize; + let y = (self.pos.y / lvl.grid.cell_size as f64).min(lvl.grid.height as f64 - 1.0).max(0.0) as usize; + if lvl.grid.cells[x][y] { + self.vel += lvl.gravity; + if self.vel.y > 0.0 && !(ctrl.mov.down() && ctrl.jump.is_pressed) { + self.vel.y = 0.0; + self.vel.x *= 0.9; + } - if ctrl.jump.is_pressed { - self.vel = ctrl.aim.to_point() * 5.0; + if !ctrl.mov.down() { + if ctrl.jump.is_pressed && !ctrl.jump.was_pressed { + self.vel.y = -5.0; + } } + } else { + self.vel += lvl.gravity; } + self.pos += self.vel; 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, - vel: ctrl.aim.to_point() * (3.0 + rand::random::()) + point!(normal.sample(&mut rand::thread_rng()), normal.sample(&mut rand::thread_rng())) + self.vel, + 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; } if ctrl.start.is_pressed && !ctrl.start.was_pressed { @@ -185,8 +195,8 @@ impl Object for Character { } match ctrl.mov.x { - v if v < -0.9 => { self.vel.x -= 0.5 } - v if v > 0.9 => { self.vel.x += 0.5 } + 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 } _ => {} } @@ -201,18 +211,21 @@ impl Object for Character { 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); + // // 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); - // circle values - let p = (self.pos + Point2D::from(ctrl.aim.a) * l).to_i32().into(); - renderer.draw_line(pos, p, (0, 0, 255)); + 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 + Point2D::from(ctrl.aim.a) * l).to_i32().into(); + // renderer.draw_line(pos, p, (0, 0, 255)); + // draw_cross(renderer, p); } } @@ -240,7 +253,7 @@ impl Object for Boll { if self.bounces == 0 { return Dead } - self.vel = -self.vel; + self.vel *= -0.5; self.bounces -= 1; use rand::distributions::{Distribution, Normal}; let normal = Normal::new(0.5, 0.4);