From b67f911bac1b7ab69a2d3cbb6f5a70d2887d6ac3 Mon Sep 17 00:00:00 2001 From: maxluli Date: Thu, 19 May 2022 11:14:37 +0200 Subject: [PATCH] Fixed a bug where sometimes hitting the ceiling would crash the app --- src/main.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index f2d6d0a..176aa51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -234,9 +234,15 @@ impl Game{ if !bloc.grounded{ //Check for collisions with some tiles - let posx_2 = posx + bloc.charge.x; - let posy_2 = posy + bloc.charge.y; + let mut posx_2 = posx + bloc.charge.x; + let mut posy_2 = posy + bloc.charge.y; + if posx_2 < 0{ + posx_2 = 0; + } + if posy_2 < 0{ + posy_2 = 0; + } let mut range = posx..posx_2; if posx < posx_2{ @@ -247,26 +253,30 @@ impl Game{ //there is not velocity in the x axis range = posx..posx+1; } - + let mut old_position:Point = Point::new(posx,posy); + let mut tmp_index_2d:Point = Point::new(-1,-1); + let mut tmp_index_1d = -1; for tmp_x in range{ let tmp_y; - //tmp_y = posy + (tmp_x-posx)*((posy_2-posy) / (posx_2-posx)); if posx_2-posx == 0{ tmp_y = posy + (tmp_x-posx)*1; }else{ tmp_y = posy + (tmp_x-posx)*((posy_2-posy) / (posx_2-posx)); } //check the tile in the new Point - let tmp_index_2d = Point::new(tmp_x / (WINDOW_WIDTH / map.size.x),tmp_y / (WINDOW_HEIGHT / map.size.y)); - let tmp_index_1d = tmp_index_2d.x + map.size.x * tmp_index_2d.y; + if tmp_index_1d != -1{ + //old_position = tmp_index_2d; + } + tmp_index_2d = Point::new(tmp_x / (WINDOW_WIDTH / map.size.x),tmp_y / (WINDOW_HEIGHT / map.size.y)); + tmp_index_1d = tmp_index_2d.x + map.size.x * tmp_index_2d.y; match map.grid[tmp_index_1d as usize]{ Tile::Rock =>{ + bloc.shape.set_x(old_position.x); + bloc.shape.set_y(old_position.y); bloc.charge = Point::new(0, 0); - println!("rock"); } _=>{ - println!("Not rock"); } }