improve object generation

master
CptCaptain 4 years ago
parent b0c869b53d
commit 8ae1659fff
  1. 46
      src/main.rs

@ -73,9 +73,9 @@ impl Object {
} }
} }
pub fn new_rand(color: [f32; 4]) -> Object { pub fn new_rand() -> Object {
Object::new( Object::new(
color, [1.0, 1.0, 0.0, 0.8],
rand::random::<f64>(), rand::random::<f64>(),
vec![rand::random::<f64>(); 2], vec![rand::random::<f64>(); 2],
vec![rand::random::<f64>(); 2], vec![rand::random::<f64>(); 2],
@ -84,7 +84,21 @@ impl Object {
) )
} }
fn get_pos(&mut self) -> Vec<f64> { fn randomize_color(&mut self) {
const RED: [f32; 4] = [1.0, 0.0, 0.0, 0.8];
const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0];
const FRUNK: [f32; 4] = [0.0, 0.0, 1.0, 0.8];
const GNORM: [f32; 4] = [1.0, 1.0, 0.0, 0.8];
const WHITE: [f32; 4] = [1.0, 1.0, 1.0, 0.8];
let colors = [RED, FRUNK, GNORM, WHITE, GREEN];
self.color = *colors.choose(&mut rand::thread_rng())
.expect("Color failure!");
}
fn get_pos(&self) -> Vec<f64> {
self.pos.clone() self.pos.clone()
} }
@ -222,19 +236,10 @@ fn main() {
println!("Hello there!"); println!("Hello there!");
let mut m_attract = false; let mut m_attract = false;
const RED: [f32; 4] = [1.0, 0.0, 0.0, 0.8];
const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0];
const FRUNK: [f32; 4] = [0.0, 0.0, 1.0, 0.8];
const GNORM: [f32; 4] = [1.0, 1.0, 0.0, 0.8];
const WHITE: [f32; 4] = [1.0, 1.0, 1.0, 0.8];
let colors = [RED, FRUNK, GNORM, WHITE, GREEN];
let opengl = OpenGL::V3_2; let opengl = OpenGL::V3_2;
let num_objs = 512; const NUM_OBJECTS: usize = 512;
dbg!(NUM_OBJECTS);
let mut window: Window = WindowSettings::new("Test", [200, 200]) let mut window: Window = WindowSettings::new("Test", [200, 200])
.opengl(opengl) .opengl(opengl)
@ -246,14 +251,11 @@ fn main() {
gl: GlGraphics::new(opengl), gl: GlGraphics::new(opengl),
}; };
let mut objects = Vec::new(); let mut objects = vec![
for _ in 0..num_objs { Object::new_rand();
objects.push(Object::new_rand( NUM_OBJECTS
*colors ];
.choose(&mut rand::thread_rng()) objects.iter_mut().for_each(|obj| obj.randomize_color());
.expect("Color failure!"),
))
}
let mut positions: Vec<Vec<f64>> = vec![vec![0.0; 2]; objects.len()]; let mut positions: Vec<Vec<f64>> = vec![vec![0.0; 2]; objects.len()];
let mut events = Events::new(EventSettings::new()); let mut events = Events::new(EventSettings::new());

Loading…
Cancel
Save