let mut new_pos = Point2::new(curr_pos.x + x_offset, curr_pos.y);
let collision_on_x = collide_with_world(&new_pos, &world);
if !collision_on_x {
transform.translate_x(x_offset);
}
new_pos.y = curr_pos.y + y_offset;
let collision_on_y = collide_with_world(&new_pos, &arena&world);
if !collision_on_y {
transform.translate_y(y_offset);
}
fn collide_with_world(new_pos: &Point2<f32>, world: &World) -> bool {
// TODO make these sizes i16 instead so we can grow in any direction!
let mut left_tile: u16 = (new_pos.x / arena::TILE_SIZE_IN_PIXELS as f32) as u16;
// -1 so that we do not collide until we are exactly at the thing
let mut right_tile: u16 = ((new_pos.x + PLAYER_WIDTH - 1.0) / arena::TILE_SIZE_IN_PIXELS as f32) as u16;
let mut top_tile: u16 = ((new_pos.y + PLAYER_HEIGHT - 1.0) / arena::TILE_SIZE_IN_PIXELS as f32) as u16;
let mut bottom_tile: u16 = (new_pos.y / arena::TILE_SIZE_IN_PIXELS as f32) as u16;
println!("new_pos: {:?}, left {}, right {}, top {}, bottom {}", (new_pos.x, new_pos.y), left_tile, right_tile, top_tile, bottom_tile);
// Useless as long as we use unsigned coordinates
//if left_tile < 0 {
// left_tile = 0;
//}
if right_tile > arena::ARENA_WIDTH_IN_TILESWORLD_WIDTH_IN_TILES {
right_tile = arena::ARENA_WIDTH_IN_TILES;WORLD_WIDTH_IN_TILES;
}
// Useless as long as we use unsigned coordinates
//if bottom_tile < 0 {
// bottom_tile = 0;
//}
if top_tile > arena::ARENA_HEIGHT_IN_TILESWORLD_HEIGHT_IN_TILES {
top_tile = arena::ARENA_HEIGHT_IN_TILES;WORLD_HEIGHT_IN_TILES;
}
let mut any_collision = false;
for i in left_tile..=right_tile {
for j in bottom_tile..=top_tile {
let tile = arenaworld.get_tile(i, j);
println!("tile at {:?} is {:?}", (i, j), tile);
match tile {
Some(tile) => {
if tile == WALL {
println!("hit a wall at {:?}!", (i, j));
any_collision = true;
}
}
None => (),
}
}
}
return any_collision;
}