Preparing things

This commit is contained in:
Thomas Forgione 2018-10-02 21:48:19 +02:00
parent 4485a7d935
commit eaefcd3272
7 changed files with 337 additions and 1 deletions

View File

@ -0,0 +1,82 @@
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);
}
}

View File

@ -0,0 +1,14 @@
/// Contains the data needed to manage the controls of the player.
pub struct Controls {
}
impl Controls {
/// Creates the default controls.
pub fn new() -> Controls {
Controls {
}
}
}

12
src/engine/mod.rs Normal file
View File

@ -0,0 +1,12 @@
/// This module contains the struct Scene.
pub mod scene;
/// This module contains everything related to characters, playable or enemies.
pub mod character;
/// This module contains everything related to the controls of the game.
pub mod controls;
/// This module helps us dealing with textures.
pub mod texture;

71
src/engine/scene/mod.rs Normal file
View File

@ -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);
}

68
src/engine/texture/mod.rs Normal file
View File

@ -0,0 +1,68 @@
use sfml::graphics::{IntRect, Texture as SfTexture};
macro_rules! make_textures {
( $(
$enum_name: ident,
$texture_name: ident,
$function_name: ident,
$texture_path: tt, )
*) => {
/// Describes all the textures that will be used in this game.
#[derive(Copy, Clone)]
pub enum Texture {
$(
/// A texture.
$enum_name,
)*
}
/// A struct that holds all the textures.
pub struct TextureManager {
$(
/// A texture.
$texture_name: SfTexture,
)*
}
impl TextureManager {
/// Creates a new texture manager and initializes all textures.
pub fn new() -> TextureManager {
TextureManager {
$(
$texture_name: TextureManager::$function_name(),
)*
}
}
$(
/// Creates the texture.
fn $function_name() -> SfTexture {
let bytes = include_bytes!($texture_path).to_vec();
TextureManager::make_texture_from_bytes(bytes)
}
)*
/// Returns the real texture from the texture id.
pub fn get(&self, id: Texture) -> &SfTexture {
match id {
$(
Texture::$enum_name => &self.$texture_name,
)*
}
}
}
}
}
make_textures!(
);
impl TextureManager {
/// Creates a textures from an array of bytes.
fn make_texture_from_bytes(bytes: Vec<u8>) -> SfTexture {
SfTexture::from_memory(&bytes, &IntRect::new(0, 0, 500, 500))
.expect("Failed to create texture")
}
}

View File

@ -1,3 +1,90 @@
extern crate sfml;
extern crate rusty;
use std::time::{
Instant,
Duration,
};
use sfml::window::{
ContextSettings,
Style,
Event,
Key,
};
use sfml::graphics::{
RenderWindow,
RenderTarget,
Color,
};
use rusty::engine::scene::Scene;
use rusty::engine::character::Character;
use rusty::engine::controls::Controls;
fn main() {
println!("Hello, world!");
let game_width = 800;
let game_height = 600;
let mut character = Character::with_controls(Controls::new());
character.set_position((300.0, 200.0));
let mut scene = Scene::new();
scene.add(character);
scene.add(Character::new());
let context_settings = ContextSettings {
..Default::default()
};
let mut window = RenderWindow::new(
(game_width, game_height),
"Free Rusty Maker",
Style::CLOSE,
&context_settings,
);
window.set_framerate_limit(60);
let mut running = true;
loop {
// Manage the events
while let Some(event) = window.poll_event() {
match event {
// Quit the game if window is closed
Event::Closed => running = false,
// Quit the game if escape is pressed
Event::KeyPressed {
code: Key::Escape, ..
} => running = false,
_ => (),
}
scene.manage_event(&event);
}
// Manage the physics
// ...
// Do the rendering
window.clear(&Color::rgb(50, 200, 50));
window.draw(&scene);
// Display and manage the frame rate
window.display();
if ! running {
break;
}
}
}

View File

@ -4,3 +4,5 @@
extern crate sfml;
/// This module contains all the tools needed for the game.
pub mod engine;