From 3fd8761bc3123ed0de1c5416c7efbbb931257248 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Sun, 24 Jan 2021 20:53:49 +0100 Subject: [PATCH] Playing around with a bunch of things - player/level interaction - controls - level generation --- src/core/controller.rs | 10 ++++----- src/core/game.rs | 61 ++++++++++++++++++++++++++++++-------------------- src/core/level.rs | 20 ++++++----------- 3 files changed, 49 insertions(+), 42 deletions(-) diff --git a/src/core/controller.rs b/src/core/controller.rs index 23af1b2..1eeb13c 100644 --- a/src/core/controller.rs +++ b/src/core/controller.rs @@ -83,10 +83,10 @@ impl Stick { self.a = Radians(self.y.atan2(self.x) as f64); } - #[inline(always)] #[allow(dead_code)] fn up(&self) -> bool { self.y > 0.99 } - #[inline(always)] #[allow(dead_code)] fn down(&self) -> bool { self.y < -0.99 } - #[inline(always)] #[allow(dead_code)] fn left(&self) -> bool { self.x < -0.99 } - #[inline(always)] #[allow(dead_code)] fn right(&self) -> bool { self.x > 0.99 } + #[inline(always)] #[allow(dead_code)] pub fn up(&self) -> bool { self.y < -0.99 } + #[inline(always)] #[allow(dead_code)] pub fn down(&self) -> bool { self.y > 0.99 } + #[inline(always)] #[allow(dead_code)] pub fn left(&self) -> bool { self.x < -0.99 } + #[inline(always)] #[allow(dead_code)] pub fn right(&self) -> bool { self.x > 0.99 } pub fn to_axis_point(&self) -> Point2D { point!(self.x as f64, self.y as f64) @@ -216,7 +216,7 @@ fn get_action_mapping() -> HashMap { ActionControls::MovementY => DeviceControls::AxisLY, ActionControls::AimX => DeviceControls::AxisRX, ActionControls::AimY => DeviceControls::AxisRY, - ActionControls::Jump => DeviceControls::ButtonL1, + ActionControls::Jump => DeviceControls::ButtonA, ActionControls::Shoot => DeviceControls::ButtonR1, ActionControls::Start => DeviceControls::ButtonStart ) 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); diff --git a/src/core/level.rs b/src/core/level.rs index 65e74ff..087af3a 100644 --- a/src/core/level.rs +++ b/src/core/level.rs @@ -1,5 +1,6 @@ use common::Point2D; use core::render::Renderer; +use noise::{NoiseFn, OpenSimplex, Seedable}; use rand::Rng; use sprites::SpriteManager; @@ -8,14 +9,13 @@ use sprites::SpriteManager; #[derive(Default)] pub struct Level { pub gravity: Point2D, - pub ground: f64, // just to have something pub grid: Grid, iterations: u8, } impl Level { - pub fn new(gravity: Point2D, ground: f64) -> Self { - Level { gravity, ground, grid: Grid::generate(10), iterations: 10 } + pub fn new(gravity: Point2D) -> Self { + Level { gravity, grid: Grid::generate(10), iterations: 10 } } pub fn regenerate(&mut self) { @@ -39,8 +39,6 @@ impl Level { } pub fn render(&mut self, renderer: &mut Renderer, _sprites: &SpriteManager) { - let w = renderer.viewport().0 as i32; - renderer.canvas().set_draw_color((64, 64, 64)); let size = self.grid.cell_size; for x in 0..self.grid.width { @@ -50,12 +48,6 @@ impl Level { } } } - - for i in 1..11 { - let y = (i * i - 1) as i32 + self.ground as i32; - renderer.canvas().set_draw_color((255 - i * 20, 255 - i * 20, 0)); - renderer.canvas().draw_line((0, y), (w, y)).unwrap(); - } } } @@ -100,7 +92,6 @@ impl Grid { #[allow(dead_code)] fn simplex_noise(&mut self) { - use noise::{NoiseFn, OpenSimplex, Seedable}; let noise = OpenSimplex::new().set_seed(std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as u32); self.set_each(|x, y| noise.get([x as f64 / 12.0, y as f64 / 12.0]) > 0.055, 1); } @@ -108,7 +99,10 @@ impl Grid { #[allow(dead_code)] fn random_noise(&mut self) { let mut rng = rand::thread_rng(); - self.set_each(|_x, _y| rng.gen_range(0, 100) > 55, 1); + let noise = OpenSimplex::new().set_seed(std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as u32); + self.set_each(|_x, _y| rng.gen_range(0, 100) > (45 + (150.0 * noise.get([_x as f64 / 40.0, _y as f64 / 10.0])) as usize), 1); // more horizontal platforms + // let w = self.width as f64; + // self.set_each(|_x, _y| rng.gen_range(0, 100) > (45 + ((15 * _x) as f64 / w) as usize), 1); // opens up to the right } #[allow(dead_code)] -- 2.11.0