Added collisions with linear interpolation (only works when going left to right and is overall very bad)
This commit is contained in:
48
src/main.rs
48
src/main.rs
@@ -19,7 +19,7 @@ const WINDOW_HEIGHT:i32 = 500;
|
||||
const DEBUG_FX_SIZE_RATIO:i32 = 4;
|
||||
const DEFAULT_BALL_WIDTH:i32 = 20;
|
||||
const BALL_SPEED:i32 = 1;
|
||||
const MAX_SPEED:i32 = BALL_SPEED * 18;
|
||||
const MAX_SPEED:i32 = BALL_SPEED * 40;
|
||||
|
||||
const ARROW_PARTS:i32 = 10;
|
||||
const ARROW_WIDTH:i32 = 3;
|
||||
@@ -60,9 +60,11 @@ const DEFAULT_MAP:&[Tile] = &[
|
||||
SKY,SKY,SKY,SKY,SKY, SKY,SKY,ROCK,ROCK,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, ROCK,ROCK,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY,
|
||||
SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,ROCK,ROCK,ROCK,SKY, SKY,SKY,SKY,SKY,SKY,
|
||||
SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY,
|
||||
GRASS,GRASS,GRASS,GRASS,GRASS, GRASS,GRASS,GRASS,GRASS,GRASS, GRASS,GRASS,GRASS,GRASS,GRASS, GRASS,GRASS,GRASS,GRASS,GRASS, GRASS,GRASS,GRASS,GRASS,GRASS, GRASS,GRASS,GRASS,GRASS,GRASS, GRASS,GRASS,GRASS,GRASS,GRASS, GRASS,GRASS,GRASS,GRASS,GRASS, GRASS,GRASS,GRASS,GRASS,GRASS, GRASS,GRASS,GRASS,GRASS,GRASS,
|
||||
ROCK,ROCK,ROCK,ROCK,ROCK, ROCK,ROCK,ROCK,ROCK,ROCK, ROCK,ROCK,ROCK,ROCK,ROCK, ROCK,ROCK,ROCK,ROCK,ROCK, ROCK,ROCK,ROCK,ROCK,ROCK, ROCK,ROCK,ROCK,ROCK,ROCK, ROCK,ROCK,ROCK,ROCK,ROCK, ROCK,ROCK,ROCK,ROCK,ROCK, ROCK,ROCK,ROCK,ROCK,ROCK, ROCK,ROCK,ROCK,ROCK,ROCK,
|
||||
SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,ROCK,ROCK,ROCK,SKY, SKY,SKY,SKY,SKY,SKY,
|
||||
SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,SKY,SKY,SKY,SKY, SKY,ROCK,ROCK,ROCK,SKY, SKY,SKY,SKY,SKY,SKY,
|
||||
];
|
||||
const TILE_WIDTH:i32 = WINDOW_WIDTH / DEFAULT_MAP_WIDTH;
|
||||
const TILE_HEIGHT:i32 = WINDOW_HEIGHT / DEFAULT_MAP_HEIGHT;
|
||||
|
||||
#[derive(Clone)]
|
||||
enum Tile{
|
||||
@@ -114,11 +116,11 @@ impl Map{
|
||||
}
|
||||
}
|
||||
pub fn render(&mut self,ctx:&mut sdl2::render::Canvas<sdl2::video::Window>,debug:bool){
|
||||
let width = WINDOW_WIDTH / self.size.x;
|
||||
let height = WINDOW_HEIGHT / self.size.y;
|
||||
//let width = WINDOW_WIDTH / self.size.x;
|
||||
//let height = WINDOW_HEIGHT / self.size.y;
|
||||
for x in 0..self.size.x{
|
||||
for y in 0..self.size.y{
|
||||
let rect = Rect::new(x * width,y * height,width as u32,height as u32);
|
||||
let rect = Rect::new(x * TILE_WIDTH,y * TILE_HEIGHT,TILE_WIDTH as u32,TILE_HEIGHT as u32);
|
||||
let index = x + self.size.x * y;
|
||||
let debug_color:Color = Color::RGB(0xFF, 0xFF, 0xFF);
|
||||
let tile_bg:Color;
|
||||
@@ -223,13 +225,39 @@ impl Game{
|
||||
self.main_ball.charge = Point::new(0,0);
|
||||
}
|
||||
pub fn update(&mut self){
|
||||
let map = &mut self.map;
|
||||
let bloc = &mut self.main_ball;
|
||||
let mut posx = bloc.shape.x;
|
||||
let mut posy = bloc.shape.y;
|
||||
let width = bloc.shape.width() as i32;
|
||||
let height = bloc.shape.height() as i32;
|
||||
|
||||
if !bloc.grounded{
|
||||
if !bloc.grounded{
|
||||
//Check for collisions with some tiles
|
||||
let posx_2 = posx + bloc.charge.x;
|
||||
let posy_2 = posy + bloc.charge.y;
|
||||
|
||||
for _i in 1..1{
|
||||
println!("coucou");
|
||||
}
|
||||
|
||||
for tmp_x in posx..posx_2{
|
||||
let tmp_y;
|
||||
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;
|
||||
|
||||
match map.grid[tmp_index_1d as usize]{
|
||||
Tile::Rock =>{
|
||||
bloc.charge = Point::new(0, 0);
|
||||
}
|
||||
_=>{
|
||||
//nothing
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
posx += bloc.charge.x;
|
||||
posy += bloc.charge.y;
|
||||
//gravity
|
||||
@@ -253,8 +281,6 @@ impl Game{
|
||||
bloc.charge.x = -bloc.charge.x;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Uggly way to maintain the bloc in a normal speed
|
||||
if bloc.charge.x > MAX_SPEED as i32{
|
||||
bloc.charge.x = MAX_SPEED as i32;
|
||||
@@ -268,12 +294,8 @@ impl Game{
|
||||
if bloc.charge.y < -MAX_SPEED as i32{
|
||||
bloc.charge.y = -MAX_SPEED as i32;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bloc.shape.set_x(posx);
|
||||
bloc.shape.set_y(posy);
|
||||
|
||||
}
|
||||
}
|
||||
//https://stackoverflow.com/questions/65723827/sdl2-function-to-draw-a-filled-circle ## Translated to Rust
|
||||
|
||||
Reference in New Issue
Block a user