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<f64> {
point!(self.x as f64, self.y as f64)
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
)
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())));
}
}
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()
}
}
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) {
pub fn new(ctrl: Rc<RefCell<Controller>>) -> Self {
Character {
ctrl,
- pos: point!(100.0, 100.0),
+ pos: point!(300.0, 300.0),
vel: point!(0.0, 0.0),
}
}
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::<f64>()) + point!(normal.sample(&mut rand::thread_rng()), normal.sample(&mut rand::thread_rng())) + self.vel,
+ 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;
}
if ctrl.start.is_pressed && !ctrl.start.was_pressed {
}
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 }
_ => {}
}
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);
}
}
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);
use common::Point2D;
use core::render::Renderer;
+use noise::{NoiseFn, OpenSimplex, Seedable};
use rand::Rng;
use sprites::SpriteManager;
#[derive(Default)]
pub struct Level {
pub gravity: Point2D<f64>,
- pub ground: f64, // just to have something
pub grid: Grid,
iterations: u8,
}
impl Level {
- pub fn new(gravity: Point2D<f64>, ground: f64) -> Self {
- Level { gravity, ground, grid: Grid::generate(10), iterations: 10 }
+ pub fn new(gravity: Point2D<f64>) -> Self {
+ Level { gravity, grid: Grid::generate(10), iterations: 10 }
}
pub fn regenerate(&mut self) {
}
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 {
}
}
}
-
- 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();
- }
}
}
#[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);
}
#[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)]