|
|
@ -1,19 +1,21 @@
|
|
|
|
extern crate quick_xml;
|
|
|
|
extern crate quick_xml;
|
|
|
|
extern crate sfml;
|
|
|
|
extern crate sfml;
|
|
|
|
|
|
|
|
extern crate cgmath;
|
|
|
|
|
|
|
|
|
|
|
|
use quick_xml::events::Event as xmlEvent;
|
|
|
|
use quick_xml::events::Event as xmlEvent;
|
|
|
|
use quick_xml::Reader;
|
|
|
|
use quick_xml::Reader;
|
|
|
|
use sfml::graphics::{
|
|
|
|
use sfml::graphics::{
|
|
|
|
CircleShape, Color, Drawable, RectangleShape, RenderStates, RenderTarget, RenderWindow, Shape,
|
|
|
|
CircleShape, Color, Drawable,
|
|
|
|
|
|
|
|
RectangleShape, RenderStates,
|
|
|
|
|
|
|
|
RenderTarget, RenderWindow, Shape,
|
|
|
|
Transformable,
|
|
|
|
Transformable,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
use sfml::window::{Event, Key, Style};
|
|
|
|
use sfml::window::{Event, Key, Style};
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::str::FromStr;
|
|
|
|
use std::str::FromStr;
|
|
|
|
use sfml::graphics::Texture;
|
|
|
|
use sfml::graphics::{ Texture, Sprite, IntRect};
|
|
|
|
use sfml::graphics::Sprite;
|
|
|
|
use cgmath::{InnerSpace, Vector2 };
|
|
|
|
use sfml::graphics::IntRect;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn read_spritesheet(filename: String) -> HashMap<String, HashMap<String, i32>> {
|
|
|
|
fn read_spritesheet(filename: String) -> HashMap<String, HashMap<String, i32>> {
|
|
|
|
let mut reader = Reader::from_file(filename).unwrap();
|
|
|
|
let mut reader = Reader::from_file(filename).unwrap();
|
|
|
@ -81,41 +83,54 @@ fn read_spritesheet(filename: String) -> HashMap<String, HashMap<String, i32>> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Our custom drawable type. It looks like a bullet.
|
|
|
|
/// Our custom drawable type. It looks like a bullet.
|
|
|
|
struct Bullet<'s> {
|
|
|
|
struct Player<'s> {
|
|
|
|
head: CircleShape<'s>,
|
|
|
|
head: CircleShape<'s>,
|
|
|
|
torso: RectangleShape<'s>,
|
|
|
|
delta: Vector2<f32>,
|
|
|
|
|
|
|
|
pos: Vector2<f32>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'s> Bullet<'s> {
|
|
|
|
impl<'s> Player<'s> {
|
|
|
|
|
|
|
|
pub fn impulse(&mut self, delta_v: &Vector2<f32>) {
|
|
|
|
|
|
|
|
self.delta.x = delta_v.x;
|
|
|
|
|
|
|
|
self.delta.y = delta_v.y;
|
|
|
|
|
|
|
|
self.delta.normalize();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn update(&mut self, delta_t: f32) {
|
|
|
|
|
|
|
|
self.pos.x += self.delta.x * delta_t;
|
|
|
|
|
|
|
|
self.pos.y += self.delta.y * delta_t;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.delta *= 0.95;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.head.set_position((delta.x, delta.y));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let mut head = CircleShape::new(50.0, 50);
|
|
|
|
let mut delta = Vector2::new(0.0, 0.0);
|
|
|
|
head.set_position((100.0, 100.0));
|
|
|
|
let mut pos = Vector2::new(0.0, 0.0);
|
|
|
|
head.set_fill_color(&Color::RED);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut torso = RectangleShape::with_size((100., 200.).into());
|
|
|
|
let mut head = CircleShape::new(10.0, 10);
|
|
|
|
torso.set_position((100.0, 150.0));
|
|
|
|
head.set_position((delta.x, delta.y));
|
|
|
|
torso.set_fill_color(&Color::BLUE);
|
|
|
|
head.set_fill_color(&Color::RED);
|
|
|
|
|
|
|
|
|
|
|
|
Self { head, torso }
|
|
|
|
Self { head, delta, pos }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Implement the Drawable trait for our custom drawable.
|
|
|
|
impl<'s> Drawable for Player<'s> {
|
|
|
|
impl<'s> Drawable for Bullet<'s> {
|
|
|
|
|
|
|
|
fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>(
|
|
|
|
fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>(
|
|
|
|
&'a self,
|
|
|
|
&'a self,
|
|
|
|
render_target: &mut RenderTarget,
|
|
|
|
render_target: &mut RenderTarget,
|
|
|
|
_: RenderStates<'texture, 'shader, 'shader_texture>,
|
|
|
|
_: RenderStates<'texture, 'shader, 'shader_texture>,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
render_target.draw(&self.head);
|
|
|
|
render_target.draw(&self.head);
|
|
|
|
render_target.draw(&self.torso)
|
|
|
|
// render_target.draw(&self.torso)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
fn main() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let spritesheet_desc = read_spritesheet(String::from("spritesheet_complete.xml"));
|
|
|
|
let spritesheet_desc = read_spritesheet(String::from("spritesheet_complete.xml"));
|
|
|
|
let spritesheet_text = Texture::from_file("spritesheet_complete.png")
|
|
|
|
let spritesheet_text = Texture::from_file("spritesheet_complete.png")
|
|
|
|
.expect("Couldn't load texture");
|
|
|
|
.expect("Couldn't load texture");
|
|
|
@ -140,20 +155,32 @@ fn main() {
|
|
|
|
&Default::default(),
|
|
|
|
&Default::default(),
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// let bullet = Bullet::new();
|
|
|
|
let mut player = Player::new();
|
|
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
loop {
|
|
|
|
while let Some(event) = window.poll_event() {
|
|
|
|
while let Some(event) = window.poll_event() {
|
|
|
|
match event {
|
|
|
|
match event {
|
|
|
|
Event::Closed | Event::KeyPressed {
|
|
|
|
Event::Closed => return,
|
|
|
|
code: Key::Escape, ..
|
|
|
|
Event::KeyPressed { code, .. } => {
|
|
|
|
} => return,
|
|
|
|
if code == Key::Escape {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
} else if code == Key::W {
|
|
|
|
|
|
|
|
player.impulse(&Vector2::new(0.0, -1.0));
|
|
|
|
|
|
|
|
} else if code == Key::A {
|
|
|
|
|
|
|
|
player.impulse(&Vector2::new(-1.0, 0.0));
|
|
|
|
|
|
|
|
} else if code == Key::S {
|
|
|
|
|
|
|
|
player.impulse(&Vector2::new(0.0, 1.0));
|
|
|
|
|
|
|
|
} else if code == Key::D {
|
|
|
|
|
|
|
|
player.impulse(&Vector2::new(1.0, 0.0));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
window.clear(&Color::BLACK);
|
|
|
|
window.clear(&Color::BLACK);
|
|
|
|
window.draw(&sprite);
|
|
|
|
window.draw(&sprite);
|
|
|
|
|
|
|
|
window.draw(&player);
|
|
|
|
window.display()
|
|
|
|
window.display()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|