Added boid generation and borders physics
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
117
Cargo.lock
generated
Normal file
117
Cargo.lock
generated
Normal file
@@ -0,0 +1,117 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "Rusty_Boids"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rand",
|
||||
"sdl2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.125"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.6.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sdl2"
|
||||
version = "0.35.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7959277b623f1fb9e04aea73686c3ca52f01b2145f8ea16f4ff30d8b7623b1a"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"lazy_static",
|
||||
"libc",
|
||||
"sdl2-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sdl2-sys"
|
||||
version = "0.35.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e3586be2cf6c0a8099a79a12b4084357aa9b3e0b0d7980e3b67aaf7a9d55f9f0"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"version-compare",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "version-compare"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fe88247b92c1df6b6de80ddc290f3976dbdf2f5f5d3fd049a9fb598c6dd5ca73"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.10.2+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
|
||||
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "Rusty_Boids"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
sdl2 = "0.35.2"
|
||||
rand = "0.8.5"
|
||||
122
src/main.rs
Normal file
122
src/main.rs
Normal file
@@ -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