Fixed a bug where sometimes hitting the ceiling would crash the app

This commit is contained in:
2022-05-19 11:14:37 +02:00
parent 2063995dc5
commit b67f911bac

View File

@@ -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");
}
}