From 45256f1a0e809c0a18e9b639aa6f76df06864b17 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Wed, 30 Jan 2019 17:48:11 -0800 Subject: [PATCH] . --- src/loader.rs | 39 ++++++++++++++++++++++++++++++++------- src/main.rs | 41 +++++++---------------------------------- src/util.rs | 7 ------- 3 files changed, 39 insertions(+), 48 deletions(-) diff --git a/src/loader.rs b/src/loader.rs index 95b764c5..77befeb6 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -24,7 +24,6 @@ impl Loader { } } - pub fn read_static_entities<'a>(&'a self, filename: String, entities: &EntState<'a>) { let file = File::open(filename).expect("Could not open file"); @@ -33,7 +32,7 @@ impl Loader { let mut w: f32 = 0.0; let mut h: f32 = 0.0; - let mut x: i32 = 0; + let mut x: i32; let mut y: i32 = 0; @@ -77,14 +76,40 @@ impl Loader { } } - pub fn read_dynamic_entities(filename: String, entities: &EntState) { + pub fn read_dynamic_entities<'a>(&'a self, filename: String, entities: &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 mut sprite1 = Sprite::new(); - entities.dynamic_entities.borrow_mut().push(sprite1); - } + 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.borrow_mut().push(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.borrow_mut().push(sprite); + } + _ => { + // Do nothing + } + } + } + } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 58b70930..3c8b2953 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,11 +22,11 @@ extern crate ncollide2d; use sfml::graphics::{ Color, RenderTarget, RenderWindow, - Texture, Sprite, Transformable + Sprite, Transformable }; use sfml::window::{ Event, Key, Style}; use sfml::system::Vector2 as sfVec2; -use ncollide2d::bounding_volume::{self, AABB, BoundingVolumeInterferencesCollector}; +use ncollide2d::bounding_volume::{AABB, BoundingVolumeInterferencesCollector}; use ncollide2d::partitioning::BVT; use sfml::graphics::RectangleShape; use std::{thread, time}; @@ -42,18 +42,15 @@ pub struct EntState<'a> { fn main() { - - let loader = Loader::new(); let mut state = EntState { dynamic_entities: Rc::new(RefCell::new(Vec::new())), static_entities: Rc::new(RefCell::new(Vec::new())) }; - { - let mut state_borrow = &state; - loader.read_static_entities(String::from("static_entities.txt"), state_borrow); - } + loader.read_static_entities(String::from("static_entities.txt"), &state); + loader.read_dynamic_entities(String::from("dynamic_entities.txt"), &state); + let static_sprites: Vec<(&Sprite, AABB)> = vec![ // ( @@ -64,33 +61,11 @@ fn main() { // 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)); - let mut window = RenderWindow::new( (512, 512), "Custom drawable", @@ -179,10 +154,8 @@ fn main() { window.draw(ent); } - if interferences.len() == 0 { - // window.draw(&block_sprite); -// window.draw(&block_sprite2); -// window.draw(&block_sprite3); + for ent in state.dynamic_entities.borrow().iter() { + window.draw(ent); } window.display(); diff --git a/src/util.rs b/src/util.rs index 98dc5224..b37c9aad 100644 --- a/src/util.rs +++ b/src/util.rs @@ -4,11 +4,6 @@ use std::collections::HashMap; use std::borrow::Cow; use std::str::FromStr; 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> { @@ -30,8 +25,6 @@ pub fn read_spritesheet(filename: String) -> HashMap = HashMap::new(); - //println!("{:?}", d.name()); - let mut name: String = String::new(); for i in d.attributes() {