the bloc is now bouncing on the walls with a specified bouncing ratio

This commit is contained in:
2022-05-11 14:42:59 +02:00
parent 683d27f357
commit 430f3d6dbf

View File

@@ -1,14 +1,19 @@
use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::event::Event; use sdl2::event::Event;
use sdl2::keyboard::Keycode; use sdl2::keyboard::Keycode;
use sdl2::keyboard::Scancode;
use std::collections::HashSet;
use std::time::Duration; use std::time::Duration;
use sdl2::rect::Point; use sdl2::rect::Point;
use sdl2::rect::Rect; use sdl2::rect::Rect;
const WINDOW_HEIGHT:i32 = 600; const WINDOW_HEIGHT:i32 = 800;
const WINDOW_WIDTH:i32 = 800; const WINDOW_WIDTH:i32 = 1000;
const BLOC_SPEED:f32 = 3.0; const BLOC_SPEED:f32 = 3.0;
const MAX_SPEED:f32 = 20.0; const MAX_SPEED:f32 = 20.0;
const GRAVITY:f32 = 10.0;
const BOUNCE_RATIO:f32 = 110.0;
pub struct Game{ pub struct Game{
main_bloc:Entity, main_bloc:Entity,
@@ -57,62 +62,50 @@ impl Game{
} }
} }
pub fn update(&mut self){ pub fn update(&mut self){
let mut posx = self.main_bloc.shape.x; let bloc = &mut self.main_bloc;
let mut posy = self.main_bloc.shape.y; let mut posx = bloc.shape.x;
posx += self.main_bloc.velocity.x; let mut posy = bloc.shape.y;
posy += self.main_bloc.velocity.y; let width = bloc.shape.width() as i32;
let bloc_width:i32 = self.main_bloc.shape.width() as i32; let height = bloc.shape.height() as i32;
let bloc_height:i32 = self.main_bloc.shape.height() as i32;
if posx + bloc_width > WINDOW_WIDTH{ if posy + height >= WINDOW_HEIGHT{
posx = WINDOW_WIDTH - bloc_width as i32; //we hit rock bottom
self.main_bloc.velocity.x = 0; posy = WINDOW_HEIGHT - height;
}else{ bloc.velocity.y = -(bloc.velocity.y as f32 / 100.0 * BOUNCE_RATIO) as i32;
if posx < 0{
posx = 0;
self.main_bloc.velocity.x = 0;
}
} }
if posy + bloc_height > WINDOW_HEIGHT{ if posy <= 0{
posy = WINDOW_HEIGHT - bloc_height as i32; //we hit top
self.main_bloc.velocity.y = 0; posy = 0;
}else{ bloc.velocity.y = -(bloc.velocity.y as f32 / 100.0 * BOUNCE_RATIO) as i32;
if posy < 0{ }
posy = 0; if posx + width >= WINDOW_WIDTH{
self.main_bloc.velocity.y = 0; //we hit right
} posx = WINDOW_WIDTH - width;
bloc.velocity.x = -(bloc.velocity.x as f32 / 100.0 * BOUNCE_RATIO) as i32;
}
if posx <= 0{
//we hit left
posx = 0;
bloc.velocity.x = -(bloc.velocity.x as f32 / 100.0 * BOUNCE_RATIO) as i32;
} }
if self.main_bloc.velocity.x > 0{ if bloc.velocity.x > MAX_SPEED as i32{
if self.main_bloc.velocity.x < BLOC_SPEED as i32{ bloc.velocity.x = MAX_SPEED as i32;
self.main_bloc.velocity.x = 0; }
}else{ if bloc.velocity.x < -MAX_SPEED as i32{
self.main_bloc.velocity.x -= (BLOC_SPEED / 2.0) as i32; bloc.velocity.x = -MAX_SPEED as i32;
} }
}else{ if bloc.velocity.y > MAX_SPEED as i32{
if self.main_bloc.velocity.x > -BLOC_SPEED as i32{ bloc.velocity.y = MAX_SPEED as i32;
self.main_bloc.velocity.x = 0; }
}else{ if bloc.velocity.y < -MAX_SPEED as i32{
self.main_bloc.velocity.x += (BLOC_SPEED / 2.0) as i32; bloc.velocity.y = -MAX_SPEED as i32;
}
} }
if self.main_bloc.velocity.y > 0{ posx += bloc.velocity.x;
if self.main_bloc.velocity.y < BLOC_SPEED as i32{ posy += bloc.velocity.y;
self.main_bloc.velocity.y = 0; bloc.shape.set_x(posx);
}else{ bloc.shape.set_y(posy);
self.main_bloc.velocity.y -= (BLOC_SPEED / 2.0) as i32;
}
}else{
if self.main_bloc.velocity.y > -BLOC_SPEED as i32{
self.main_bloc.velocity.y = 0;
}else{
self.main_bloc.velocity.y += (BLOC_SPEED / 2.0) as i32;
}
}
self.main_bloc.shape.set_x(posx);
self.main_bloc.shape.set_y(posy);
} }
} }
@@ -123,7 +116,6 @@ pub struct Entity{
pub fn main_loop(canvas: &mut sdl2::render::Canvas<sdl2::video::Window>,event_pump:&mut sdl2::EventPump,game:&mut Game){ pub fn main_loop(canvas: &mut sdl2::render::Canvas<sdl2::video::Window>,event_pump:&mut sdl2::EventPump,game:&mut Game){
'running: loop { 'running: loop {
canvas.clear();
for event in event_pump.poll_iter() { for event in event_pump.poll_iter() {
match event { match event {
Event::Quit {..} | Event::Quit {..} |