Preparing things
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use sfml::window::Event;
|
||||
use sfml::graphics::{
|
||||
RenderTarget,
|
||||
RenderStates,
|
||||
Drawable,
|
||||
};
|
||||
|
||||
use engine::character::Character;
|
||||
|
||||
/// Contains everything needed to play.
|
||||
pub struct Scene {
|
||||
|
||||
/// The characters contained in the scene.
|
||||
characters: Vec<Character>,
|
||||
}
|
||||
|
||||
impl Scene {
|
||||
|
||||
/// Creates an empty scene.
|
||||
pub fn new() -> Scene {
|
||||
Scene {
|
||||
characters: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a character to the scene.
|
||||
pub fn add(&mut self, character: Character) {
|
||||
self.characters.push(character);
|
||||
}
|
||||
|
||||
/// Updates the whole scene.
|
||||
pub fn update(&mut self, duration: &Duration) {
|
||||
for c in &mut self.characters {
|
||||
c.update(duration);
|
||||
}
|
||||
}
|
||||
|
||||
/// Transfers an event to the elements contained in the scene that should receive events.
|
||||
pub fn manage_event(&mut self, event: &Event) {
|
||||
for c in &mut self.characters {
|
||||
c.manage_event(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drawable for Scene {
|
||||
fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>(
|
||||
&'a self,
|
||||
target: &mut RenderTarget,
|
||||
states: RenderStates<'texture, 'shader, 'shader_texture>
|
||||
) {
|
||||
|
||||
for c in &self.characters {
|
||||
let mut s = RenderStates::default();
|
||||
s.transform = states.transform.clone();
|
||||
s.blend_mode = states.blend_mode;
|
||||
c.draw(target, s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait that needs to be implemented for everything that can be updatable.
|
||||
pub trait Updatable {
|
||||
/// Updates the thing depending on the duration since last frame.
|
||||
fn update(&mut self, duration: &Duration);
|
||||
|
||||
/// Called when an event arrives.
|
||||
fn manage_event(&mut self, event: &Event);
|
||||
}
|
||||
Reference in New Issue
Block a user