83 lines
1.9 KiB
Rust
83 lines
1.9 KiB
Rust
|
use std::time::Duration;
|
||
|
|
||
|
use sfml::system::Vector2;
|
||
|
use sfml::window::Event;
|
||
|
use sfml::graphics::{
|
||
|
Drawable,
|
||
|
RenderStates,
|
||
|
RenderTarget,
|
||
|
Sprite,
|
||
|
Color,
|
||
|
};
|
||
|
|
||
|
use engine::scene::Updatable;
|
||
|
use engine::controls::Controls;
|
||
|
|
||
|
/// A character, enemy or controllable.
|
||
|
pub struct Character {
|
||
|
|
||
|
/// The position of the character.
|
||
|
position: Vector2<f32>,
|
||
|
|
||
|
/// The speed of the character.
|
||
|
speed: Vector2<f32>,
|
||
|
|
||
|
/// If the player is controlling a character.
|
||
|
controls: Option<Controls>,
|
||
|
}
|
||
|
|
||
|
impl Character {
|
||
|
/// Creates a character in (0, 0).
|
||
|
pub fn new() -> Character {
|
||
|
Character {
|
||
|
position: Vector2::new(0.0, 0.0),
|
||
|
speed: Vector2::new(0.0, 0.0),
|
||
|
controls: None,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// Creates a character with the specified controls.
|
||
|
pub fn with_controls(controls: Controls) -> Character {
|
||
|
Character {
|
||
|
position: Vector2::new(0.0, 0.0),
|
||
|
speed: Vector2::new(0.0, 0.0),
|
||
|
controls: Some(controls),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// Sets the position of the character.
|
||
|
pub fn set_position<S: Into<Vector2<f32>>>(&mut self, position: S) {
|
||
|
self.position = position.into();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Updatable for Character {
|
||
|
fn update(&mut self, duration: &Duration) {
|
||
|
|
||
|
}
|
||
|
|
||
|
fn manage_event(&mut self, event: &Event) {
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Drawable for Character {
|
||
|
fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>(
|
||
|
&'a self,
|
||
|
target: &mut RenderTarget,
|
||
|
states: RenderStates<'texture, 'shader, 'shader_texture>
|
||
|
) {
|
||
|
|
||
|
use sfml::graphics::Transformable;
|
||
|
let mut sprite = Sprite::new();
|
||
|
sprite.set_origin((32.0, 0.0));
|
||
|
sprite.set_position(self.position);
|
||
|
sprite.set_color(&if self.controls.is_some() {
|
||
|
Color::rgb(255, 0, 0)
|
||
|
} else {
|
||
|
Color::rgb(255, 255, 255)
|
||
|
});
|
||
|
sprite.draw(target, states);
|
||
|
}
|
||
|
}
|