Added boid generation and borders physics
This commit is contained in:
+122
@@ -0,0 +1,122 @@
|
||||
use sdl2::pixels::Color;
|
||||
use sdl2::event::Event;
|
||||
use sdl2::keyboard::Keycode;
|
||||
use sdl2::keyboard::Scancode;
|
||||
use std::collections::HashSet;
|
||||
use std::time::Duration;
|
||||
use sdl2::rect::Point;
|
||||
use sdl2::rect::Rect;
|
||||
use rand::Rng;
|
||||
|
||||
const WINDOW_HEIGHT:i32 = 600;
|
||||
const WINDOW_WIDTH:i32 = 800;
|
||||
const BIRD_SPEED:i32 = 2;
|
||||
const MAX_BIRD_SPEED:i32 = 10;
|
||||
const BIRDS_COUNT:i32 = 50;
|
||||
const MIN_BIRD_SIZE:i32 = 20;
|
||||
const MAX_BIRD_SIZE:i32 = 50;
|
||||
|
||||
pub struct Bird{
|
||||
shape:Rect,
|
||||
velocity:Point,
|
||||
}
|
||||
|
||||
pub struct Simulation{
|
||||
birds:Vec<Bird>,
|
||||
}
|
||||
impl Simulation{
|
||||
pub fn render(&mut self,ctx:&mut sdl2::render::Canvas<sdl2::video::Window>){
|
||||
ctx.set_draw_color(Color::RGB(0x6D, 0x6D, 0x64));
|
||||
ctx.clear();
|
||||
ctx.set_draw_color(Color::RGB(0x33, 0x33, 0x33));
|
||||
for bird in self.birds.iter(){
|
||||
ctx.fill_rect(bird.shape).expect("Rusty Boids");
|
||||
}
|
||||
}
|
||||
pub fn update(&mut self){
|
||||
for bird in &mut self.birds{
|
||||
let mut posx = bird.shape.x;
|
||||
let mut posy = bird.shape.y;
|
||||
let width:i32 = bird.shape.width() as i32;
|
||||
let height:i32 = bird.shape.height() as i32;
|
||||
let left_wall = WINDOW_WIDTH - width;
|
||||
let bottom_wall = WINDOW_HEIGHT- height;
|
||||
|
||||
if posx + bird.velocity.x > WINDOW_WIDTH - width{
|
||||
let offset = left_wall - (posx + bird.velocity.x);
|
||||
posx = left_wall - offset;
|
||||
bird.velocity.x = -bird.velocity.x;
|
||||
}
|
||||
if posx + bird.velocity.x < 0{
|
||||
let offset = posx + bird.velocity.x;
|
||||
posx = 0 + offset;
|
||||
bird.velocity.x = -bird.velocity.x;
|
||||
}
|
||||
|
||||
if posy + bird.velocity.y > WINDOW_HEIGHT - height{
|
||||
let offset = bottom_wall - (posy + bird.velocity.y);
|
||||
posy = bottom_wall - offset;
|
||||
bird.velocity.y = -bird.velocity.y;
|
||||
}
|
||||
if posy + bird.velocity.y < 0{
|
||||
let offset = posy + bird.velocity.y;
|
||||
posy = 0 + offset;
|
||||
bird.velocity.y = -bird.velocity.y;
|
||||
}
|
||||
|
||||
posx += bird.velocity.x;
|
||||
posy += bird.velocity.y;
|
||||
bird.shape.set_x(posx);
|
||||
bird.shape.set_y(posy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main_loop(canvas: &mut sdl2::render::Canvas<sdl2::video::Window>,event_pump:&mut sdl2::EventPump,game:&mut Simulation){
|
||||
'running: loop {
|
||||
for event in event_pump.poll_iter() {
|
||||
match event {
|
||||
Event::Quit {..} |
|
||||
Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
|
||||
break 'running
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// MAIN LOOP
|
||||
game.update();
|
||||
game.render(canvas);
|
||||
// MAIN LOOP
|
||||
|
||||
canvas.present();
|
||||
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
|
||||
}
|
||||
}
|
||||
pub fn generate_birds() -> Vec<Bird>{
|
||||
let mut rng = rand::thread_rng();
|
||||
let mut birds = Vec::with_capacity(BIRDS_COUNT as usize);
|
||||
for bird in 0..BIRDS_COUNT{
|
||||
let size = rng.gen_range(MIN_BIRD_SIZE as u32..MAX_BIRD_SIZE as u32);
|
||||
let rectangle = Rect::new(rng.gen_range(0..WINDOW_WIDTH - MAX_BIRD_SIZE),rng.gen_range(0..WINDOW_HEIGHT - MAX_BIRD_SIZE),size, size);
|
||||
let velocity = Point::new(rng.gen_range(0..MAX_BIRD_SPEED), rng.gen_range(0..MAX_BIRD_SPEED));
|
||||
birds.push(Bird{shape:rectangle,velocity:velocity});
|
||||
}
|
||||
return birds;
|
||||
}
|
||||
pub fn main() {
|
||||
let sdl_context = sdl2::init().unwrap();
|
||||
let video_subsystem = sdl_context.video().unwrap();
|
||||
|
||||
let window = video_subsystem.window("Test SDL Rust", WINDOW_WIDTH as u32, WINDOW_HEIGHT as u32)
|
||||
.position_centered()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let mut canvas = window.into_canvas().build().unwrap();
|
||||
let mut event_pump = sdl_context.event_pump().unwrap();
|
||||
|
||||
let mut simulation = Simulation{birds:generate_birds()};
|
||||
|
||||
main_loop(&mut canvas,&mut event_pump,&mut simulation);
|
||||
}
|
||||
Reference in New Issue
Block a user