From 070fdb387386e64e3e31343cda7da8b2037d6e51 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Thu, 31 Jan 2019 23:27:33 -0800 Subject: [PATCH] Apparently *self referencial structs* are a big no no in Rust, bummer --- src/collision.rs | 52 +++++++++++++++++++ src/entstate.rs | 131 +++++++++++++++++++++++++++++++++++++++++++++++ src/loader.rs | 116 ----------------------------------------- src/main.rs | 77 +++++----------------------- src/resources.rs | 18 +++++++ 5 files changed, 215 insertions(+), 179 deletions(-) create mode 100644 src/collision.rs create mode 100644 src/entstate.rs delete mode 100644 src/loader.rs create mode 100644 src/resources.rs diff --git a/src/collision.rs b/src/collision.rs new file mode 100644 index 00000000..b034a6ed --- /dev/null +++ b/src/collision.rs @@ -0,0 +1,52 @@ +use crate::entstate::EntState; +use sfml::graphics::Sprite; +use sfml::graphics::Transformable; +use ncollide2d::partitioning::BVT; +use ncollide2d::bounding_volume::AABB; +use ncollide2d::bounding_volume; +use std::cell::Ref; + +pub struct Collision<'a> { + dynamic_bvh : Option, AABB>>, + static_bvh : Option, AABB>>, +} + + +impl<'a> Collision<'a> { + pub fn new() -> Collision<'a> { + Collision { + dynamic_bvh: Option::None, + static_bvh: Option::None, + } + } + + pub fn gen_bvt(mut self, entity_state: &'a EntState<'a>) { + + let mut dynamic_sprites: Box, AABB)>> = Box::new(Vec::new()); + { + let vec = entity_state.dynamic_entities.borrow(); + for i in vec.iter() { + let bounds = i.global_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)); + } + } + 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)); + } +} \ No newline at end of file diff --git a/src/entstate.rs b/src/entstate.rs new file mode 100644 index 00000000..1d07113c --- /dev/null +++ b/src/entstate.rs @@ -0,0 +1,131 @@ +use std::collections::HashMap; +use sfml::graphics::Texture; +use crate::util; +use std::fs::File; +use std::io::BufReader; +use std::io::BufRead; +use sfml::graphics::Sprite; +use sfml::graphics::Transformable; +use std::cell::RefCell; +use crate::player::Player; +use ncollide2d::partitioning::BVT; +use ncollide2d::bounding_volume::AABB; +use crate::resources::Resources; +use ncollide2d::bounding_volume; +use std::rc::Rc; + + +pub struct EntState<'a> { + pub dynamic_entities: Rc >>>, + pub static_entities : Rc >>>, + pub player : Player<'a>, +} + +impl<'a> EntState<'a> { + + pub fn new() -> EntState<'a> { + EntState{ + dynamic_entities: Rc::new(RefCell::new(Vec::new())), + static_entities: Rc::new(RefCell::new(Vec::new())), + player: Player::new(), + } + } + + + + pub fn read_static_entities(mut self, filename: String, resources: &'a Resources) { + + let mut sprite : Sprite = Sprite::new(); + sprite.set_texture(&resources.spritesheet_text, false); + sprite.set_texture_rect(&util::grab_sheet_rec(String::from("blockBrown.png"), &resources.spritesheet_desc)); + sprite.set_position((0.0,0.0)); + + //self.static_entities.get_mut().push(sprite); + +// let file = File::open(filename).expect("Could not open file"); +// +// let mut first_line: bool = true; +// let mut w: f32 = 0.0; +// let mut h: f32 = 0.0; +// +// let mut x: i32; +// let mut y: i32 = 0; +// +// +// for line in BufReader::new(file).lines() { +// +// if first_line { +// +// first_line = !first_line; +// let val = line.unwrap(); +// let arr : Vec<&str> = val.split_whitespace().collect(); +// +// w = arr.get(0).unwrap().parse::().unwrap(); +// h = arr.get(1).unwrap().parse::().unwrap(); +// +// } else { +// +// x = 0; +// +// let val = line.unwrap(); +// for i in val.split_whitespace() { +// match i.parse::().unwrap() { +// 0 => { +// // Do nothing for now +// } +// 1 => { +// let mut sprite : Sprite<'a> = Sprite::new(); +// sprite.set_texture(&self.spritesheet_text, false); +// 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)); +// +// self.static_entities.push(RefCell::new(sprite)); +// } +// _ => { +// panic!("ahhhhhh"); +// } +// } +// x += 1; +// } +// y += 1; +// } +// } + } + +// pub fn read_dynamic_entities(&'a mut self, filename: String) { +// +// let file = File::open(filename).expect("Could not open file"); +// +// for line in BufReader::new(file).lines() { +// +// let val = line.unwrap(); +// let arr : Vec<&str> = val.split_whitespace().collect(); +// +// let e = arr.get(0).unwrap(); +// let x = arr.get(1).unwrap().parse::().unwrap(); +// let y = arr.get(2).unwrap().parse::().unwrap(); +// +// match *e { +// "enemy" => { +// let mut sprite = Sprite::new(); +// sprite.set_texture(&self.spritesheet_text, false); +// sprite.set_texture_rect(&util::grab_sheet_rec(String::from("enemyFloating_1.png"), &self.spritesheet_desc)); +// sprite.set_position((x, y)); +// +// self.dynamic_entities.push(RefCell::new(sprite)); +// } +// "player" => { +// let mut sprite = Sprite::new(); +// sprite.set_texture(&self.spritesheet_text, false); +// sprite.set_texture_rect(&util::grab_sheet_rec(String::from("playerBlue_up3.png"), &self.spritesheet_desc)); +// sprite.set_position((x, y)); +// +// self.dynamic_entities.push(RefCell::new(sprite)); +// } +// _ => { +// // Do nothing +// } +// } +// } +// } +} \ No newline at end of file diff --git a/src/loader.rs b/src/loader.rs deleted file mode 100644 index 83e2bdb4..00000000 --- a/src/loader.rs +++ /dev/null @@ -1,116 +0,0 @@ -use std::collections::HashMap; -use sfml::graphics::Texture; -use crate::util; -use crate::EntState; -use std::fs::File; -use std::io::BufReader; -use std::io::BufRead; -use sfml::graphics::Sprite; -use sfml::graphics::Transformable; -use std::cell::RefCell; - -pub struct Loader { - - spritesheet_desc : HashMap>, - spritesheet_text : Texture - -} - -impl Loader { - - pub fn new() -> Loader { - Loader{ - spritesheet_desc: util::read_spritesheet(String::from("spritesheet_complete.xml")), - spritesheet_text: Texture::from_file("spritesheet_complete.png").expect("Couldn't load texture") - } - } - - pub fn read_static_entities<'a>(&'a self, filename: String, entities: &mut EntState<'a>) { - - let file = File::open(filename).expect("Could not open file"); - - let mut first_line: bool = true; - let mut w: f32 = 0.0; - let mut h: f32 = 0.0; - - let mut x: i32; - let mut y: i32 = 0; - - - for line in BufReader::new(file).lines() { - - if first_line { - - first_line = !first_line; - let val = line.unwrap(); - let arr : Vec<&str> = val.split_whitespace().collect(); - - w = arr.get(0).unwrap().parse::().unwrap(); - h = arr.get(1).unwrap().parse::().unwrap(); - - } else { - - x = 0; - - let val = line.unwrap(); - for i in val.split_whitespace() { - match i.parse::().unwrap() { - 0 => { - // Do nothing for now - } - 1 => { - let mut sprite = Sprite::new(); - sprite.set_texture(&self.spritesheet_text, false); - 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.push(RefCell::new(sprite)); - } - _ => { - panic!("ahhhhhh"); - } - } - x += 1; - } - y += 1; - } - } - } - - pub fn read_dynamic_entities<'a>(&'a self, filename: String, entities: &mut EntState<'a>) { - - let file = File::open(filename).expect("Could not open file"); - - for line in BufReader::new(file).lines() { - - let val = line.unwrap(); - let arr : Vec<&str> = val.split_whitespace().collect(); - - let e = arr.get(0).unwrap(); - let x = arr.get(1).unwrap().parse::().unwrap(); - let y = arr.get(2).unwrap().parse::().unwrap(); - - match *e { - "enemy" => { - let mut sprite = Sprite::new(); - sprite.set_texture(&self.spritesheet_text, false); - sprite.set_texture_rect(&util::grab_sheet_rec(String::from("enemyFloating_1.png"), &self.spritesheet_desc)); - sprite.set_position((x, y)); - - entities.dynamic_entities.push(RefCell::new(sprite)); - } - "player" => { - let mut sprite = Sprite::new(); - sprite.set_texture(&self.spritesheet_text, false); - sprite.set_texture_rect(&util::grab_sheet_rec(String::from("playerBlue_up3.png"), &self.spritesheet_desc)); - sprite.set_position((x, y)); - - entities.dynamic_entities.push(RefCell::new(sprite)); - } - _ => { - // Do nothing - } - } - } - } -} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7c6256e7..851fa683 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,87 +10,38 @@ mod timer; mod player; mod input; mod util; -mod loader; +mod entstate; +mod resources; +mod collision; use crate::player::Player; use crate::timer::Timer; use crate::input::Input; -use crate::loader::Loader; +use crate::entstate::EntState; +use crate::resources::Resources; +use crate::collision::Collision; extern crate nalgebra as na; extern crate ncollide2d; use sfml::graphics::{ Color, RenderTarget, RenderWindow, - Sprite, Transformable }; use sfml::window::{ Event, Key, Style}; use sfml::system::Vector2 as sfVec2; -use ncollide2d::bounding_volume::{AABB, BoundingVolumeInterferencesCollector}; -use ncollide2d::partitioning::BVT; -use sfml::graphics::RectangleShape; -use std::{thread, time}; -use std::cell::RefCell; -use std::rc::Rc; -use ncollide2d::bounding_volume; -pub struct EntState<'a> { - dynamic_bvh : Option, AABB>>, - dynamic_entities: Vec >>, - - static_bvh : Option, AABB>>, - static_entities : Vec >>, - player : Player<'a>, -} - -impl<'a> EntState<'a> { - - 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)); +fn main() { -// 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)); - } + let mut resources = Resources::new(); + let mut collision = Collision::new(); + let mut state = EntState::new(); -} + state.read_static_entities(String::from("static_entities.txt"), &resources); + //state.read_static_entities(String::from("static_entities.txt"), &resources); + //state.read_dynamic_entities(String::from("dynamic_entities.txt")); -fn main() { - let loader = Loader::new(); - let mut state = EntState { - 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"), &mut state); - loader.read_dynamic_entities(String::from("dynamic_entities.txt"), &mut state); - - state.gen_bvt(); + //state.gen_bvt(); let mut window = RenderWindow::new( diff --git a/src/resources.rs b/src/resources.rs new file mode 100644 index 00000000..3b86abcc --- /dev/null +++ b/src/resources.rs @@ -0,0 +1,18 @@ +use crate::util; +use sfml::graphics::Texture; +use std::collections::HashMap; + +pub struct Resources { + pub spritesheet_desc : HashMap>, + pub spritesheet_text : Texture, +} + +impl Resources { + pub fn new() -> Resources { + Resources { + + spritesheet_desc: util::read_spritesheet(String::from("spritesheet_complete.xml")), + spritesheet_text: Texture::from_file("spritesheet_complete.png").expect("Couldn't load texture") + } + } +} \ No newline at end of file