From 21dc0b81edf0af8645f594b1d09374c814eeeeb0 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Wed, 31 Jan 2018 13:00:00 +0700 Subject: [PATCH] going to just put all the loading stuff into the EntState --- src/loader.rs | 11 ++++--- src/main.rs | 85 +++++++++++++++++++++++++++++---------------------- 2 files changed, 54 insertions(+), 42 deletions(-) diff --git a/src/loader.rs b/src/loader.rs index 77befeb6..83e2bdb4 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -7,6 +7,7 @@ use std::io::BufReader; use std::io::BufRead; use sfml::graphics::Sprite; use sfml::graphics::Transformable; +use std::cell::RefCell; pub struct Loader { @@ -24,7 +25,7 @@ impl Loader { } } - pub fn read_static_entities<'a>(&'a self, filename: String, entities: &EntState<'a>) { + pub fn read_static_entities<'a>(&'a self, filename: String, entities: &mut EntState<'a>) { let file = File::open(filename).expect("Could not open file"); @@ -63,7 +64,7 @@ impl Loader { sprite.set_texture_rect(&util::grab_sheet_rec(String::from("blockBrown.png"), &self.spritesheet_desc)); sprite.set_position((x as f32 * w, y as f32 * h)); - entities.static_entities.borrow_mut().push(sprite); + entities.static_entities.push(RefCell::new(sprite)); } _ => { panic!("ahhhhhh"); @@ -76,7 +77,7 @@ impl Loader { } } - pub fn read_dynamic_entities<'a>(&'a self, filename: String, entities: &EntState<'a>) { + pub fn read_dynamic_entities<'a>(&'a self, filename: String, entities: &mut EntState<'a>) { let file = File::open(filename).expect("Could not open file"); @@ -96,7 +97,7 @@ impl Loader { sprite.set_texture_rect(&util::grab_sheet_rec(String::from("enemyFloating_1.png"), &self.spritesheet_desc)); sprite.set_position((x, y)); - entities.dynamic_entities.borrow_mut().push(sprite); + entities.dynamic_entities.push(RefCell::new(sprite)); } "player" => { let mut sprite = Sprite::new(); @@ -104,7 +105,7 @@ impl Loader { sprite.set_texture_rect(&util::grab_sheet_rec(String::from("playerBlue_up3.png"), &self.spritesheet_desc)); sprite.set_position((x, y)); - entities.dynamic_entities.borrow_mut().push(sprite); + entities.dynamic_entities.push(RefCell::new(sprite)); } _ => { // Do nothing diff --git a/src/main.rs b/src/main.rs index fd53d366..7c6256e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,50 +37,61 @@ use ncollide2d::bounding_volume; pub struct EntState<'a> { dynamic_bvh : Option, AABB>>, - dynamic_entities: Rc >>>, + dynamic_entities: Vec >>, static_bvh : Option, AABB>>, - static_entities : Rc >>>, + static_entities : Vec >>, player : Player<'a>, } +impl<'a> EntState<'a> { -fn main() { + pub fn gen_bvt(&'a mut self) { + + let mut dynamic_sprites: Vec<(&'a Sprite, AABB)> = Vec::new(); + { + for i in self.dynamic_entities { + let bounds = i.borrow().global_bounds(); + let pos = i.borrow().position(); + let volume = bounding_volume::AABB::new(na::Point2::new(pos.x as f64, pos.y as f64), + na::Point2::new((pos.x + bounds.width) as f64, (pos.y + bounds.width) as f64)); + + dynamic_sprites.push((&i.borrow(), volume)); + } + } + self.dynamic_bvh = Some(BVT::new_balanced(dynamic_sprites)); + +// let mut static_sprites: Vec<(&Sprite, AABB)> = Vec::new(); +// { +// for i in self.static_entities { +// let bounds = i.local_bounds(); +// let pos = i.position(); +// let volume = bounding_volume::AABB::new(na::Point2::new(pos.x as f64, pos.y as f64), +// na::Point2::new((pos.x + bounds.width) as f64, (pos.y + bounds.width) as f64)); +// +// static_sprites.push((i, volume)); +// } +// } +// self.static_bvh = Some(BVT::new_balanced(static_sprites)); + } + +} +fn main() { let loader = Loader::new(); let mut state = EntState { - dynamic_bvh: None, - dynamic_entities: Rc::new(RefCell::new(Vec::new())), - static_bvh: None, - static_entities: Rc::new(RefCell::new(Vec::new())), + dynamic_bvh: Option::None, + dynamic_entities: Vec::new(), + static_bvh: Option::None, + static_entities: Vec::new(), player: Player::new(), }; - loader.read_static_entities(String::from("static_entities.txt"), &state); - loader.read_dynamic_entities(String::from("dynamic_entities.txt"), &state); - - let mut dynamic_sprites: Vec<(&Sprite, AABB)> = Vec::new(); - let dyna = state.dynamic_entities.borrow(); - for i in dyna.iter() { - let bounds = &i.local_bounds(); - let pos = &i.position(); - let volume = bounding_volume::AABB::new(na::Point2::new(pos.x as f64, pos.y as f64), - na::Point2::new((pos.x + bounds.width) as f64, (pos.y + bounds.width) as f64)); - - dynamic_sprites.push((i, volume)); - } - state.dynamic_bvh = Some(BVT::new_balanced(dynamic_sprites)); + loader.read_static_entities(String::from("static_entities.txt"), &mut state); + loader.read_dynamic_entities(String::from("dynamic_entities.txt"), &mut state); + state.gen_bvt(); - let mut static_sprites: Vec<(&Sprite, AABB)> = Vec::new(); - for i in state.static_entities.borrow_mut().iter() { - let bounds = &i.local_bounds(); - let pos = &i.position(); - let volume = bounding_volume::AABB::new(na::Point2::new(pos.x as f64, pos.y as f64), - na::Point2::new((pos.x + bounds.width) as f64, (pos.y + bounds.width) as f64)); - static_sprites.push((i, volume)); - } - state.static_bvh = Some(BVT::new_balanced(static_sprites)); let mut window = RenderWindow::new( (512, 512), @@ -161,13 +172,13 @@ fn main() { window.draw(&player); //window.draw(&collision_sprite); - for ent in state.static_entities.borrow().iter() { - window.draw(ent); - } - - for ent in state.dynamic_entities.borrow().iter() { - window.draw(ent); - } +// for ent in state.static_entities.get_mut().iter() { +// window.draw(ent); +// } +// +// for ent in state.dynamic_entities.get_mut().iter() { +// window.draw(ent); +// } window.display();