diff --git a/dynamic_entities.txt b/dynamic_entities.txt new file mode 100644 index 00000000..8bb1381d --- /dev/null +++ b/dynamic_entities.txt @@ -0,0 +1,4 @@ +player 200 200 +enemy 100 100 +enemy 150 100 +enemy 200 100 \ No newline at end of file diff --git a/src/loader.rs b/src/loader.rs new file mode 100644 index 00000000..bf3dae56 --- /dev/null +++ b/src/loader.rs @@ -0,0 +1,88 @@ +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; + +pub struct Loader<'a> { + + spritesheet_desc : HashMap>, + spritesheet_text : Texture + +} + +impl<'a> Loader<'a> { + + pub fn new() -> Loader<'a> { + 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(&self, filename: String, entities: &EntState) { + + 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 = 0; + 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 { + + y += 1; + x = 0; + + let val = line.unwrap(); + for i in val.split_whitespace() { + + x += 1; + match i.parse::().unwrap() { + 0 => { + 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.dynamic_entities.borrow_mut().push(sprite); + } + _ => { + panic!("ahhhhhh"); + } + } + } + } + } + } + + pub fn read_dynamic_entities(filename: String, entities: &EntState) { + let file = File::open(filename).expect("Could not open file"); + for line in BufReader::new(file).lines() { + + } + + let mut sprite1 = Sprite::new(); + entities.dynamic_entities.borrow_mut().push(sprite1); + } + +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1bb2ee5e..03692745 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,10 +10,12 @@ mod timer; mod player; mod input; mod util; +mod loader; use crate::player::Player; use crate::timer::Timer; use crate::input::Input; +use crate::loader::Loader; extern crate nalgebra as na; extern crate ncollide2d; @@ -31,50 +33,36 @@ use std::{thread, time}; use std::cell::RefCell; use std::rc::Rc; -pub struct tSprite { - pub value1: i32, - value2: f64 -} -impl tSprite { - pub fn new() -> tSprite { - tSprite { value1: 0, value2: 0.0 } - } +pub struct EntState<'a> { + dynamic_entities: Rc >>>, + static_entities : Rc >>>, } -fn main() { - // Load the spritesheet - let spritesheet_desc = util::read_spritesheet(String::from("spritesheet_complete.xml")); - let spritesheet_text = Texture::from_file("spritesheet_complete.png") - .expect("Couldn't load texture"); - - let shared_container: Rc>> = Rc::new(RefCell::new(Vec::new())); +fn main() { - shared_container.borrow_mut().push(tSprite::new()); + let loader = Loader::new(); - let mut sprite_vec = shared_container.borrow_mut(); + let mut state = EntState { + dynamic_entities: Rc::new(RefCell::new(Vec::new())), + static_entities : Rc::new(RefCell::new(Vec::new())) + }; - sprite_vec.last().unwrap().value1 = 0; + loader.read_static_entities(String::from("static_entities.txt"), &state); - // sp.set_position((0.0,0.0)); - //println!("{:?}", sp); + state.dynamic_entities.borrow_mut().push(Sprite::new()); // sp.set_texture(&spritesheet_text, false); // sp.set_texture_rect(&util::grab_sheet_rec(String::from("blockBrown.png"), &spritesheet_desc)); // sp.set_position((64.0, 64.0)); return; - let mut block_sprite2 = Sprite::new(); - block_sprite2.set_texture(&spritesheet_text, false); - block_sprite2.set_texture_rect(&util::grab_sheet_rec(String::from("blockBrown.png"), &spritesheet_desc)); - block_sprite2.set_position((128.0, 64.0)); - - let mut block_sprite3 = Sprite::new(); - block_sprite3.set_texture(&spritesheet_text, false); - block_sprite3.set_texture_rect(&util::grab_sheet_rec(String::from("blockBrown.png"), &spritesheet_desc)); - block_sprite3.set_position((192.0, 64.0)); +// let mut block_sprite2 = Sprite::new(); +// block_sprite2.set_texture(&spritesheet_text, false); +// block_sprite2.set_texture_rect(&util::grab_sheet_rec(String::from("blockBrown.png"), &spritesheet_desc)); +// block_sprite2.set_position((128.0, 64.0)); let static_sprites: Vec<(&Sprite, AABB)> = vec![ // ( @@ -86,31 +74,31 @@ fn main() { // na::Point2::new((pos.x + bounds.width) as f64, (pos.y + bounds.width) as f64)) // }, // ), - ( - &block_sprite2, - { - let bounds = &block_sprite2.local_bounds(); - let pos = &block_sprite2.position(); - 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)) - }, - ), - ( - &block_sprite3, - { - let bounds = &block_sprite3.local_bounds(); - let pos = &block_sprite3.position(); - 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)) - }, - ), +// ( +// &block_sprite2, +// { +// let bounds = &block_sprite2.local_bounds(); +// let pos = &block_sprite2.position(); +// 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)) +// }, +// ), +// ( +// &block_sprite3, +// { +// let bounds = &block_sprite3.local_bounds(); +// let pos = &block_sprite3.position(); +// 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)) +// }, +// ), ]; let bvt = BVT::new_balanced(static_sprites); let mut sprite = Sprite::new(); - sprite.set_texture(&spritesheet_text, false); - sprite.set_texture_rect(&util::grab_sheet_rec(String::from("playerBlue_stand.png"), &spritesheet_desc)); +// sprite.set_texture(&spritesheet_text, false); +// sprite.set_texture_rect(&util::grab_sheet_rec(String::from("playerBlue_stand.png"), &spritesheet_desc)); let mut window = RenderWindow::new( (500, 500), @@ -197,11 +185,12 @@ fn main() { if interferences.len() == 0 { // window.draw(&block_sprite); - window.draw(&block_sprite2); - window.draw(&block_sprite3); +// window.draw(&block_sprite2); +// window.draw(&block_sprite3); } window.display(); } + } \ No newline at end of file diff --git a/src/util.rs b/src/util.rs index d03a7c46..98dc5224 100644 --- a/src/util.rs +++ b/src/util.rs @@ -7,6 +7,8 @@ use sfml::graphics::IntRect; use std::fs::File; use std::io::BufRead; use std::io::BufReader; +use crate::EntState; +use sfml::graphics::Sprite; pub fn read_spritesheet(filename: String) -> HashMap> { @@ -79,10 +81,3 @@ pub fn grab_sheet_rec(spritename: String, spritesheet: &HashMap