From dc6765af994118e6e56915c5ffda69ca1d124e56 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Fri, 25 Jan 2019 17:00:43 -0800 Subject: [PATCH] . --- src/input.rs | 34 +++++++++ src/main.rs | 198 ++++---------------------------------------------- src/player.rs | 64 ++++++++++++++++ src/timer.rs | 30 ++++++++ src/util.rs | 67 +++++++++++++++++ 5 files changed, 209 insertions(+), 184 deletions(-) create mode 100644 src/input.rs create mode 100644 src/player.rs create mode 100644 src/timer.rs create mode 100644 src/util.rs diff --git a/src/input.rs b/src/input.rs new file mode 100644 index 00000000..0f1daf81 --- /dev/null +++ b/src/input.rs @@ -0,0 +1,34 @@ +use std::collections::HashSet; +use sfml::window::{Key, Event}; + + +pub struct Input { + held_keys: HashSet +} + +impl Input { + pub fn new() -> Input { + + let mut container = HashSet::new(); + + Input { + held_keys: container, + } + } + + pub fn is_held(&self, key: Key) -> bool{ + self.held_keys.contains(&key) + } + + pub fn ingest(&mut self, event: &Event) { + match event { + Event::KeyPressed { code, .. } => { + self.held_keys.insert(code.clone()); + } + Event::KeyReleased { code, .. } => { + self.held_keys.remove(code); + } + _ => {} + } + } +} diff --git a/src/main.rs b/src/main.rs index 833afc22..37bdabb0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,196 +6,26 @@ extern crate quick_xml; extern crate sfml; extern crate cgmath; -use simple_stopwatch::Stopwatch; -use quick_xml::events::Event as xmlEvent; -use quick_xml::Reader; -use sfml::graphics::{ - CircleShape, Color, Drawable, - RenderStates, - RenderTarget, RenderWindow, Shape, - Transformable, -}; -use sfml::window::{Event, Key, Style}; -use std::borrow::Cow; -use std::collections::HashMap; -use std::str::FromStr; -use sfml::graphics::{ Texture, Sprite, IntRect}; -use cgmath::{Vector2 }; -use std::collections::HashSet; - -fn read_spritesheet(filename: String) -> HashMap> { - let mut reader = Reader::from_file(filename).unwrap(); - reader.trim_text(true); - - let mut buf = Vec::new(); - - let mut t : HashMap> = HashMap::new(); - - loop { - match reader.read_event(&mut buf) { - Ok(xmlEvent::Start(ref e)) => { - match e.name() { - b"TextureAtlas" => println!("attributes values: {:?}", - e.attributes().map(|a| a.unwrap().value).collect::>()), - _ => (), - } - }, - Ok(xmlEvent::Empty(d)) => { - - let mut map_entry: HashMap = HashMap::new(); - - //println!("{:?}", d.name()); - - let mut name: String = String::new(); - - for i in d.attributes() { - - let attr = i.expect("Couldn't grab attribute"); - - let key = String::from_utf8_lossy(attr.key); - - if key == "name" { - - let value = match attr.value { - Cow::Borrowed(r) => String::from_utf8_lossy(&r), - Cow::Owned(_) => break - }; - name = value.to_lowercase() - } else { - - let value = match attr.value { - Cow::Borrowed(r) => String::from_utf8_lossy(&r), - Cow::Owned(_) => break - }; - - map_entry.insert(String::from(key), FromStr::from_str(&value[..]).expect("")); - } - } - - t.insert(name,map_entry); - }, - Ok(xmlEvent::Eof) => break, - _ => (), - } - - // if we don't keep a borrow elsewhere, we can clear the buffer to keep memory usage low - buf.clear(); - } - - return t; -} - -struct Timer { - stopwatch: Stopwatch, - lap: f32 -} - -impl Timer { - - fn new() -> Timer { - - let started = Stopwatch::start_new(); - let mut time_now = started.ms(); - - Timer { - stopwatch: started, - lap: time_now - } - } +mod timer; +mod player; +mod input; +mod util; - fn elap_time(&mut self) -> f32 { - self.stopwatch.ms()/1000.0 - } - - fn frame_time(&mut self) -> f32 { - - let now = self.stopwatch.ms(); - let elapsed = now - self.lap; - self.lap = now; - - return elapsed - } -} - - -struct Player<'s> { - head: CircleShape<'s>, - delta: Vector2, - pos: Vector2, -} - -impl<'s> Player<'s> { - pub fn impulse(&mut self, delta_v: &Vector2) { - self.delta.x += delta_v.x; - self.delta.y += delta_v.y; - } +use crate::player::Player; +use crate::timer::Timer; +use crate::input::Input; - pub fn update(&mut self, delta_t: f32) { - self.pos.x += self.delta.x * delta_t * 1.0; - self.pos.y += self.delta.y * delta_t * 1.0; - - self.delta *= 0.999; - - self.head.set_position((self.pos.x, self.pos.y)); - } - - pub fn new() -> Self { - let mut delta = Vector2::new(0.0, 0.0); - let mut pos = Vector2::new(0.0, 0.0); - - let mut head = CircleShape::new(10.0, 10); - head.set_position((delta.x, delta.y)); - head.set_fill_color(&Color::RED); - - Self { head, delta, pos } - } -} - -impl<'s> Drawable for Player<'s> { - fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>( - &'a self, - render_target: &mut RenderTarget, - _: RenderStates<'texture, 'shader, 'shader_texture>, - ) { - render_target.draw(&self.head); - } -} - -struct Input { - held_keys: HashSet -} - -impl Input { - pub fn new() -> Input { - - let mut container = HashSet::new(); - - Input { - held_keys: container, - } - } - - pub fn is_held(&self, key: Key) -> bool{ - self.held_keys.contains(&key) - } - - pub fn ingest(&mut self, event: &Event) { - match event { - Event::KeyPressed { code, .. } => { - self.held_keys.insert(code.clone()); - } - Event::KeyReleased { code, .. } => { - self.held_keys.remove(code); - } - _ => {} - } - } -} +use sfml::graphics::{ + Color, RenderTarget, RenderWindow, + Texture, Sprite, IntRect +}; +use sfml::window::{ Event, Key, Style}; +use sfml::system::Vector2; fn main() { - let spritesheet_desc = read_spritesheet(String::from("spritesheet_complete.xml")); + 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"); diff --git a/src/player.rs b/src/player.rs new file mode 100644 index 00000000..2e02a9cd --- /dev/null +++ b/src/player.rs @@ -0,0 +1,64 @@ +use sfml::system::Vector2; +use sfml::graphics::{CircleShape, + Color, + Drawable, + RenderStates, + RenderTarget}; +use sfml::graphics::Transformable; +use sfml::graphics::Shape; +use sfml::graphics::RectangleShape; +use sfml::graphics::Rect; + +pub struct Player<'s> { + head: CircleShape<'s>, + delta: Vector2, + pos: Vector2, +} + +impl<'s> Player<'s> { + pub fn impulse(&mut self, delta_v: &Vector2) { + self.delta.x += delta_v.x; + self.delta.y += delta_v.y; + } + + pub fn collision(&mut self, objects: Vec>) { + + for i in objects { + + match self.head.local_bounds().intersection(&i) { + Some(r) => println!("{:?}", r), + None => continue + } + } + } + + pub fn update(&mut self, delta_t: f32) { + self.pos.x += self.delta.x * delta_t * 1.0; + self.pos.y += self.delta.y * delta_t * 1.0; + + self.delta *= 0.999; + + self.head.set_position((self.pos.x, self.pos.y)); + } + + pub fn new() -> Self { + let mut delta = Vector2::new(0.0, 0.0); + let mut pos = Vector2::new(0.0, 0.0); + + let mut head = CircleShape::new(10.0, 10); + head.set_position((delta.x, delta.y)); + head.set_fill_color(&Color::RED); + + Self { head, delta, pos } + } +} + +impl<'s> Drawable for Player<'s> { + fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>( + &'a self, + render_target: &mut RenderTarget, + _: RenderStates<'texture, 'shader, 'shader_texture>, + ) { + render_target.draw(&self.head); + } +} \ No newline at end of file diff --git a/src/timer.rs b/src/timer.rs new file mode 100644 index 00000000..f9d9ec72 --- /dev/null +++ b/src/timer.rs @@ -0,0 +1,30 @@ +use simple_stopwatch::Stopwatch; + +pub struct Timer { + stopwatch: Stopwatch, + lap: f32 +} + +impl Timer { + pub fn new() -> Timer { + let started = Stopwatch::start_new(); + let mut time_now = started.ms(); + + Timer { + stopwatch: started, + lap: time_now + } + } + + pub fn elap_time(&mut self) -> f32 { + self.stopwatch.ms() / 1000.0 + } + + pub fn frame_time(&mut self) -> f32 { + let now = self.stopwatch.ms(); + let elapsed = now - self.lap; + self.lap = now; + + return elapsed + } +} diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 00000000..9b0e13f1 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,67 @@ +use quick_xml::Reader; +use quick_xml::events::Event as xmlEvent; +use std::collections::HashMap; +use std::borrow::Cow; +use std::str::FromStr; + +pub fn read_spritesheet(filename: String) -> HashMap> { + let mut reader = Reader::from_file(filename).unwrap(); + reader.trim_text(true); + + let mut buf = Vec::new(); + + let mut t : HashMap> = HashMap::new(); + + loop { + match reader.read_event(&mut buf) { + Ok(xmlEvent::Start(ref e)) => { + match e.name() { + b"TextureAtlas" => println!("attributes values: {:?}", + e.attributes().map(|a| a.unwrap().value).collect::>()), + _ => (), + } + }, + Ok(xmlEvent::Empty(d)) => { + + let mut map_entry: HashMap = HashMap::new(); + + //println!("{:?}", d.name()); + + let mut name: String = String::new(); + + for i in d.attributes() { + + let attr = i.expect("Couldn't grab attribute"); + + let key = String::from_utf8_lossy(attr.key); + + if key == "name" { + + let value = match attr.value { + Cow::Borrowed(r) => String::from_utf8_lossy(&r), + Cow::Owned(_) => break + }; + name = value.to_lowercase() + } else { + + let value = match attr.value { + Cow::Borrowed(r) => String::from_utf8_lossy(&r), + Cow::Owned(_) => break + }; + + map_entry.insert(String::from(key), FromStr::from_str(&value[..]).expect("")); + } + } + + t.insert(name,map_entry); + }, + Ok(xmlEvent::Eof) => break, + _ => (), + } + + // if we don't keep a borrow elsewhere, we can clear the buffer to keep memory usage low + buf.clear(); + } + + return t; +} \ No newline at end of file