diff --git a/src/main.rs b/src/main.rs index 4b99366..e5974b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,16 +38,16 @@ struct Rect { impl Rect { pub fn rect_collides(&self, pos: (f64, f64), other: &Rect, other_pos: (f64, f64)) -> bool { pos.0 + self.width > other_pos.0 - && pos.0 <= other_pos.0 + other.width + && pos.0 < other_pos.0 + other.width && pos.1 + self.height > other_pos.1 - && pos.1 <= other_pos.1 + other.height + && pos.1 < other_pos.1 + other.height } pub fn point_collides(&self, pos: (f64, f64), point: (f64, f64)) -> bool { pos.0 + self.width < point.0 - && pos.0 >= point.0 + && pos.0 > point.0 && pos.1 + self.height < point.1 - && pos.1 >= point.1 + && pos.1 > point.1 } } // @@ -133,7 +133,6 @@ fn closest_surface_for_point_and_rectangle_and_your_mom( ] .into_iter() .filter_map(|(intersect, surface)| intersect.map(|intersect| (intersect, surface))) - .filter(|&((x, _), _)| x >= rect_pos.0 && x < rect_pos.0 + rect.width) .map(|(point, surface)| (point_distance(p0, point), surface)) .min_by(|(dist_a, _), (dist_b, _)| dist_a.total_cmp(&dist_b)); @@ -146,7 +145,6 @@ fn closest_surface_for_point_and_rectangle_and_your_mom( ] .into_iter() .filter_map(|(intersect, surface)| intersect.map(|intersect| (intersect, surface))) - .filter(|&((_, y), _)| y >= rect_pos.1 && y < rect_pos.1 + rect.height) .map(|(point, surface)| (point_distance(p0, point), surface)) .min_by(|(dist_a, _), (dist_b, _)| dist_a.total_cmp(&dist_b)); @@ -172,6 +170,7 @@ impl System for CollisionSystem { } let body = ctx.entity_component::(id).clone(); let rect = ctx.entity_component::(id).clone(); + let vel = body.vel.clone(); for other_id in query!(ctx, RigidBody, Rect, Collider) { if id == other_id { continue; @@ -179,10 +178,7 @@ impl System for CollisionSystem { let other_rect = ctx.entity_component::(other_id).clone(); let other_body = ctx.entity_component::(other_id).clone(); if rect.rect_collides(body.pos, &other_rect, other_body.pos) { - let last_pos = ( - body.pos.0 - body.vel.0 * delta, - body.pos.1 - body.vel.1 * delta, - ); + let last_pos = (body.pos.0 - vel.0 * delta, body.pos.1 - vel.1 * delta); let closest_surface = [ (last_pos.0, last_pos.1), (last_pos.0, last_pos.1 + rect.height), @@ -193,7 +189,7 @@ impl System for CollisionSystem { .map(|p0| { closest_surface_for_point_and_rectangle_and_your_mom( p0, - body.vel, + vel, other_body.pos, &other_rect, ) @@ -204,6 +200,7 @@ impl System for CollisionSystem { .ok_or_else(|| "we already checked if collision happens")?; let body = ctx.entity_component::(id); + println!("closest: {closest_surface:?} | vel: {:?}", vel); match closest_surface { Surface::Top => { body.vel.1 = 0.0; @@ -303,10 +300,11 @@ struct PlayerMovement; struct PlayerMovementSystem; impl System for PlayerMovementSystem { - fn on_update(&self, ctx: &mut engine::Context, _delta: f64) -> Result<(), engine::Error> { + fn on_update(&self, ctx: &mut engine::Context, delta: f64) -> Result<(), engine::Error> { for id in query!(ctx, PlayerMovement, RigidBody) { let d_down = ctx.key_pressed(engine::Keycode::D); let a_down = ctx.key_pressed(engine::Keycode::A); + let w_down = ctx.key_pressed(engine::Keycode::W); let body = ctx.entity_component::(id); body.vel.0 = if d_down && !a_down { 400.0 @@ -315,6 +313,9 @@ impl System for PlayerMovementSystem { } else { 0.0 }; + if w_down { + body.vel.1 -= 1000.0 * delta; + } } Ok(()) } @@ -332,6 +333,7 @@ fn main() { context.add_system(CloudSystem); let player = context.load_sprite("textures/player.png").unwrap(); let background = context.load_sprite("textures/literally_dprk.png").unwrap(); + let nope = context.load_sprite("textures/nuh-uh.png").unwrap(); spawn!( &mut context, @@ -345,7 +347,6 @@ fn main() { RigidBody { pos: (400.0, 400.0), gravity: true, - vel: (0.0, -600.0), ..Default::default() }, Rect { @@ -372,15 +373,42 @@ fn main() { spawn!( &mut context, RigidBody { - pos: (872.0, 360.0), + pos: (500.0, 200.0), ..Default::default() }, Rect { - width: 388.0, - height: 48.0 + width: 32.0, + height: 32.0 }, Collider { resolve: false }, + Sprite { sprite: nope }, ); + spawn!( + &mut context, + RigidBody { + pos: (872.0, 450.0), + ..Default::default() + }, + Rect { + width: 32.0, + height: 32.0 + }, + Collider { resolve: false }, + Sprite { sprite: nope }, + ); + // spawn!( + // &mut context, + // RigidBody { + // pos: (872.0, 360.0), + // ..Default::default() + // }, + // Rect { + // width: 388.0, + // height: 48.0 + // }, + // Collider { resolve: false }, + // ); + game.run(); }