Added collisions with tiles but with a huge drawback... if you are going fast enough you just go trough all of them
This commit is contained in:
94
src/main.rs
94
src/main.rs
@@ -40,21 +40,21 @@ const DEFAULT_TEST_MAP:&[i32] = &[
|
||||
0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,0,0,0,0,0,0,0,0,0,
|
||||
|
||||
0,0,0,0,0,0,0,0,0,4, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 4,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,4, 1,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,1, 4,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,4, 1,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,1, 4,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,4, 1,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,1, 4,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,4, 1,0,0,0,0,0,0,0,0,6, 6,0,0,0,0,0,0,0,0,1, 4,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,1, 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, 1,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,
|
||||
|
||||
0,0,0,0,0,0,0,0,0,5, 5,5,5,5,5,0,0,0,0,0, 0,0,0,0,0,5,5,5,5,5, 5,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,1, 5,5,5,5,5,0,0,0,0,0, 0,0,0,0,0,5,5,5,5,5, 1,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,4, 4,4,4,4,4,0,0,0,0,0, 0,0,0,0,0,4,4,4,4,4, 4,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0, 3,3,3,3,3,0,0,0,0,0, 0,0,0,0,0,3,3,3,3,3, 0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0, 2,2,2,2,2,0,0,0,0,0, 0,0,0,0,0,2,2,2,2,2, 0,0,0,0,0,0,0,0,0,0,
|
||||
@@ -267,24 +267,66 @@ impl Game{
|
||||
if posy + height >= WINDOW_HEIGHT{
|
||||
//we hit rock bottom
|
||||
posy = WINDOW_HEIGHT - height;
|
||||
bloc.velocity.y = -(bloc.velocity.y as f32 / 100.0 * BOUNCE_RATIO) as i32;
|
||||
bloc.velocity.y = bounce(bloc.velocity.y);
|
||||
}
|
||||
if posy <= 0{
|
||||
//we hit top
|
||||
posy = 0;
|
||||
bloc.velocity.y = -(bloc.velocity.y as f32 / 100.0 * BOUNCE_RATIO) as i32;
|
||||
bloc.velocity.y = bounce(bloc.velocity.y);
|
||||
}
|
||||
if posx + width >= WINDOW_WIDTH{
|
||||
//we hit right
|
||||
posx = WINDOW_WIDTH - width;
|
||||
bloc.velocity.x = -(bloc.velocity.x as f32 / 100.0 * BOUNCE_RATIO) as i32;
|
||||
bloc.velocity.x = bounce(bloc.velocity.x);
|
||||
}
|
||||
if posx <= 0{
|
||||
//we hit left
|
||||
posx = 0;
|
||||
bloc.velocity.x = -(bloc.velocity.x as f32 / 100.0 * BOUNCE_RATIO) as i32;
|
||||
bloc.velocity.x = bounce(bloc.velocity.x);
|
||||
}
|
||||
|
||||
//Apply tiles effects
|
||||
let index_2d = Point::new(posx / (WINDOW_WIDTH / self.map.size.x),posy / (WINDOW_HEIGHT / self.map.size.y));
|
||||
//println!("[DEBUG] x:{} y:{}",index_2d.x,index_2d.y);
|
||||
let index_1d = index_2d.x + self.map.size.x * index_2d.y;
|
||||
//println!("[DEBUG] index:{}",index_1d);
|
||||
|
||||
let mut slow_amount = 0;
|
||||
//println!("[DEBUG] tile {}",self.map.grid[index_1d as usize]);
|
||||
match self.map.grid[index_1d as usize]{
|
||||
0 => {
|
||||
//Ground
|
||||
slow_amount = 1;
|
||||
}
|
||||
2 =>{
|
||||
//Water
|
||||
slow_amount = 10;
|
||||
}
|
||||
3 =>{
|
||||
//Sand
|
||||
slow_amount = 3;
|
||||
}
|
||||
4 =>{
|
||||
//Wet Sand
|
||||
slow_amount = 5;
|
||||
}
|
||||
5 =>{
|
||||
//Forest
|
||||
slow_amount = 5;
|
||||
}
|
||||
6 =>{
|
||||
//Hole
|
||||
|
||||
}
|
||||
_ => {
|
||||
//uuuuuuh... whatever I guess lets not do anything
|
||||
},
|
||||
}
|
||||
if self.ball_released{
|
||||
bloc.velocity.x = reduce_velocity(bloc.velocity.x, slow_amount);
|
||||
bloc.velocity.y = reduce_velocity(bloc.velocity.y, slow_amount);
|
||||
}
|
||||
|
||||
if bloc.velocity.x > MAX_SPEED as i32{
|
||||
bloc.velocity.x = MAX_SPEED as i32;
|
||||
}
|
||||
@@ -307,6 +349,26 @@ impl Game{
|
||||
|
||||
}
|
||||
}
|
||||
pub fn bounce(acceleration:i32)->i32{
|
||||
-(acceleration as f32 / 100.0 * BOUNCE_RATIO) as i32
|
||||
}
|
||||
pub fn reduce_velocity(original:i32,amount:i32) -> i32{
|
||||
if original > 0{
|
||||
if original - amount < 0{
|
||||
0
|
||||
}else{
|
||||
original - amount
|
||||
}
|
||||
}else if original < 0{
|
||||
if original + amount > 0{
|
||||
0
|
||||
}else{
|
||||
original + amount
|
||||
}
|
||||
}else{
|
||||
original
|
||||
}
|
||||
}
|
||||
//https://stackoverflow.com/questions/65723827/sdl2-function-to-draw-a-filled-circle ## Translated to Rust
|
||||
pub fn fill_circle(ctx:&mut sdl2::render::Canvas<sdl2::video::Window>, x:i32, y:i32, radius:i32, color:Color)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user