Rust fmt
This commit is contained in:
parent
a8f928f488
commit
f3643ba31b
|
@ -1,3 +1 @@
|
||||||
fn main() {
|
fn main() {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,26 +1,18 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
extern crate sfml;
|
|
||||||
extern crate rusty;
|
extern crate rusty;
|
||||||
|
extern crate sfml;
|
||||||
|
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
use clap::{
|
use clap::{App, Arg};
|
||||||
Arg,
|
|
||||||
App,
|
|
||||||
};
|
|
||||||
|
|
||||||
use sfml::window::{
|
use sfml::window::{joystick, Event, Key};
|
||||||
Event,
|
|
||||||
Key,
|
|
||||||
joystick,
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
use rusty::engine::scene::Scene;
|
|
||||||
use rusty::engine::character::Character;
|
use rusty::engine::character::Character;
|
||||||
use rusty::engine::controls::Controls;
|
use rusty::engine::controls::Controls;
|
||||||
use rusty::engine::renderer::Renderer;
|
use rusty::engine::renderer::Renderer;
|
||||||
|
use rusty::engine::scene::Scene;
|
||||||
|
|
||||||
fn parse_resolution(res: &str) -> Result<(u32, u32), String> {
|
fn parse_resolution(res: &str) -> Result<(u32, u32), String> {
|
||||||
let split = res.split('x').collect::<Vec<_>>();
|
let split = res.split('x').collect::<Vec<_>>();
|
||||||
|
@ -43,21 +35,22 @@ fn parse_resolution(res: &str) -> Result<(u32, u32), String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
let matches = App::new("Rusty Maker")
|
let matches = App::new("Rusty Maker")
|
||||||
.version(crate_version!())
|
.version(crate_version!())
|
||||||
.arg(Arg::with_name("resolution")
|
.arg(
|
||||||
|
Arg::with_name("resolution")
|
||||||
.short("r")
|
.short("r")
|
||||||
.long("resolution")
|
.long("resolution")
|
||||||
.value_name("WIDTHxHEIGHT")
|
.value_name("WIDTHxHEIGHT")
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.default_value("800x450")
|
.default_value("800x450")
|
||||||
.validator(|x| parse_resolution(&x).map(|_| ())))
|
.validator(|x| parse_resolution(&x).map(|_| ())),
|
||||||
.arg(Arg::with_name("fullscreen")
|
).arg(
|
||||||
|
Arg::with_name("fullscreen")
|
||||||
.short("f")
|
.short("f")
|
||||||
.long("fullscreen")
|
.long("fullscreen")
|
||||||
.takes_value(false))
|
.takes_value(false),
|
||||||
.get_matches();
|
).get_matches();
|
||||||
|
|
||||||
let resolution = parse_resolution(matches.value_of("resolution").unwrap()).unwrap();
|
let resolution = parse_resolution(matches.value_of("resolution").unwrap()).unwrap();
|
||||||
let fullscreen = matches.is_present("fullscreen");
|
let fullscreen = matches.is_present("fullscreen");
|
||||||
|
@ -83,10 +76,8 @@ fn main() {
|
||||||
let mut running = true;
|
let mut running = true;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
|
||||||
// Manage the events
|
// Manage the events
|
||||||
while let Some(event) = renderer.poll_event() {
|
while let Some(event) = renderer.poll_event() {
|
||||||
|
|
||||||
match event {
|
match event {
|
||||||
// Quit the game if window is closed
|
// Quit the game if window is closed
|
||||||
Event::Closed => running = false,
|
Event::Closed => running = false,
|
||||||
|
@ -123,7 +114,5 @@ fn main() {
|
||||||
if !running {
|
if !running {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,30 +1,16 @@
|
||||||
use std::time::{
|
use std::time::{Duration, Instant};
|
||||||
Instant,
|
|
||||||
Duration,
|
|
||||||
};
|
|
||||||
|
|
||||||
|
use sfml::graphics::{FloatRect, IntRect, View};
|
||||||
use sfml::system::Vector2;
|
use sfml::system::Vector2;
|
||||||
use sfml::window::Event;
|
use sfml::window::Event;
|
||||||
use sfml::graphics::{
|
|
||||||
IntRect,
|
|
||||||
FloatRect,
|
|
||||||
View,
|
|
||||||
};
|
|
||||||
|
|
||||||
|
use engine::controls::{Action, Controls};
|
||||||
use engine::scene::Updatable;
|
use engine::scene::Updatable;
|
||||||
use engine::controls::{
|
|
||||||
Controls,
|
|
||||||
Action,
|
|
||||||
};
|
|
||||||
|
|
||||||
|
use engine::math::{clamp, duration_as_f32, duration_as_frame};
|
||||||
|
use engine::physics;
|
||||||
use engine::renderer::Drawable;
|
use engine::renderer::Drawable;
|
||||||
use engine::texture::Texture;
|
use engine::texture::Texture;
|
||||||
use engine::physics;
|
|
||||||
use engine::math::{
|
|
||||||
duration_as_f32,
|
|
||||||
duration_as_frame,
|
|
||||||
clamp,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// The different sides a character can face.
|
/// The different sides a character can face.
|
||||||
pub enum Side {
|
pub enum Side {
|
||||||
|
@ -60,7 +46,6 @@ impl Side {
|
||||||
|
|
||||||
/// A character, enemy or controllable.
|
/// A character, enemy or controllable.
|
||||||
pub struct Character {
|
pub struct Character {
|
||||||
|
|
||||||
/// The position of the character.
|
/// The position of the character.
|
||||||
pub position: Vector2<f32>,
|
pub position: Vector2<f32>,
|
||||||
|
|
||||||
|
@ -91,7 +76,6 @@ pub struct Character {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Character {
|
impl Character {
|
||||||
|
|
||||||
/// Creates a character in (0, 0).
|
/// Creates a character in (0, 0).
|
||||||
fn generic(controls: Option<Controls>) -> Character {
|
fn generic(controls: Option<Controls>) -> Character {
|
||||||
Character {
|
Character {
|
||||||
|
@ -159,7 +143,6 @@ impl Character {
|
||||||
|
|
||||||
impl Updatable for Character {
|
impl Updatable for Character {
|
||||||
fn update(&mut self, duration: &Duration) {
|
fn update(&mut self, duration: &Duration) {
|
||||||
|
|
||||||
let mut force: Vector2<f32> = Vector2::new(0.0, 0.0);
|
let mut force: Vector2<f32> = Vector2::new(0.0, 0.0);
|
||||||
|
|
||||||
if let Some(ref controls) = self.controls {
|
if let Some(ref controls) = self.controls {
|
||||||
|
@ -167,17 +150,13 @@ impl Updatable for Character {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(side) = Side::from_force(force) {
|
if let Some(side) = Side::from_force(force) {
|
||||||
|
|
||||||
self.side = side;
|
self.side = side;
|
||||||
|
|
||||||
if self.animation_timer.is_none() {
|
if self.animation_timer.is_none() {
|
||||||
self.animation_timer = Some(Instant::now());
|
self.animation_timer = Some(Instant::now());
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
self.animation_timer = None;
|
self.animation_timer = None;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let duration = duration_as_f32(duration);
|
let duration = duration_as_f32(duration);
|
||||||
|
@ -202,7 +181,6 @@ impl Updatable for Character {
|
||||||
|
|
||||||
// Compute position
|
// Compute position
|
||||||
self.position += self.speed * duration;
|
self.position += self.speed * duration;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn manage_event(&mut self, event: &Event) {
|
fn manage_event(&mut self, event: &Event) {
|
||||||
|
@ -216,7 +194,7 @@ impl Updatable for Character {
|
||||||
Some(Action::Jump(true)) => {
|
Some(Action::Jump(true)) => {
|
||||||
self.jump();
|
self.jump();
|
||||||
self.can_jump = false;
|
self.can_jump = false;
|
||||||
},
|
}
|
||||||
|
|
||||||
Some(Action::Jump(false)) => {
|
Some(Action::Jump(false)) => {
|
||||||
self.can_jump = true;
|
self.can_jump = true;
|
||||||
|
@ -224,7 +202,6 @@ impl Updatable for Character {
|
||||||
|
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,7 @@
|
||||||
use sfml::system::Vector2;
|
use sfml::system::Vector2;
|
||||||
use sfml::window::joystick::{
|
use sfml::window::joystick::{axis_position, is_button_pressed, is_connected, Axis, COUNT};
|
||||||
Axis,
|
|
||||||
axis_position,
|
|
||||||
is_connected,
|
|
||||||
is_button_pressed,
|
|
||||||
COUNT,
|
|
||||||
};
|
|
||||||
|
|
||||||
use sfml::window::{
|
use sfml::window::{Event, Key};
|
||||||
Key,
|
|
||||||
Event,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// The different actions that a user can do.
|
/// The different actions that a user can do.
|
||||||
pub enum Action {
|
pub enum Action {
|
||||||
|
@ -32,7 +23,6 @@ pub enum Controls {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Controls {
|
impl Controls {
|
||||||
|
|
||||||
/// Returns the default keyboard controls.
|
/// Returns the default keyboard controls.
|
||||||
pub fn default_keyboard() -> Controls {
|
pub fn default_keyboard() -> Controls {
|
||||||
Controls::Keyboard(KeyboardMap::default())
|
Controls::Keyboard(KeyboardMap::default())
|
||||||
|
@ -81,13 +71,11 @@ impl Controls {
|
||||||
Controls::Gamepad(ref map) => map.is_running(),
|
Controls::Gamepad(ref map) => map.is_running(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A map between keyboard keys and actions.
|
/// A map between keyboard keys and actions.
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct KeyboardMap {
|
pub struct KeyboardMap {
|
||||||
|
|
||||||
/// The key corresponding to the jump button.
|
/// The key corresponding to the jump button.
|
||||||
jump_key: Key,
|
jump_key: Key,
|
||||||
|
|
||||||
|
@ -99,11 +87,9 @@ pub struct KeyboardMap {
|
||||||
|
|
||||||
/// The key corresponding to the right button.
|
/// The key corresponding to the right button.
|
||||||
right_key: Key,
|
right_key: Key,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KeyboardMap {
|
impl KeyboardMap {
|
||||||
|
|
||||||
/// Creates the default keyboard config.
|
/// Creates the default keyboard config.
|
||||||
pub fn default() -> KeyboardMap {
|
pub fn default() -> KeyboardMap {
|
||||||
KeyboardMap {
|
KeyboardMap {
|
||||||
|
@ -116,22 +102,17 @@ impl KeyboardMap {
|
||||||
|
|
||||||
/// Converts an event and depending on the config, returns the corresponding action.
|
/// Converts an event and depending on the config, returns the corresponding action.
|
||||||
pub fn convert(&self, event: &Event) -> Option<Action> {
|
pub fn convert(&self, event: &Event) -> Option<Action> {
|
||||||
|
|
||||||
match event {
|
match event {
|
||||||
Event::KeyPressed { code, .. } if *code == self.jump_key =>
|
Event::KeyPressed { code, .. } if *code == self.jump_key => Some(Action::Jump(true)),
|
||||||
Some(Action::Jump(true)),
|
|
||||||
|
|
||||||
Event::KeyReleased { code, .. } if *code == self.jump_key =>
|
Event::KeyReleased { code, .. } if *code == self.jump_key => Some(Action::Jump(false)),
|
||||||
Some(Action::Jump(false)),
|
|
||||||
|
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the direction of the keys.
|
/// Returns the direction of the keys.
|
||||||
pub fn direction(&self) -> Vector2<f32> {
|
pub fn direction(&self) -> Vector2<f32> {
|
||||||
|
|
||||||
let mut ret = Vector2::new(0.0, 0.0);
|
let mut ret = Vector2::new(0.0, 0.0);
|
||||||
|
|
||||||
const RIGHT: Vector2<f32> = Vector2 { x: 1.0, y: 0.0 };
|
const RIGHT: Vector2<f32> = Vector2 { x: 1.0, y: 0.0 };
|
||||||
|
@ -145,20 +126,17 @@ impl KeyboardMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
ret
|
ret
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns whether the running button is held down.
|
/// Returns whether the running button is held down.
|
||||||
pub fn is_running(&self) -> bool {
|
pub fn is_running(&self) -> bool {
|
||||||
self.run_key.is_pressed()
|
self.run_key.is_pressed()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A map between gamepad buttons and actions.
|
/// A map between gamepad buttons and actions.
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct GamepadMap {
|
pub struct GamepadMap {
|
||||||
|
|
||||||
/// Id of the gamepad.
|
/// Id of the gamepad.
|
||||||
id: u32,
|
id: u32,
|
||||||
|
|
||||||
|
@ -170,16 +148,13 @@ pub struct GamepadMap {
|
||||||
|
|
||||||
/// Left / Right axis.
|
/// Left / Right axis.
|
||||||
left_right_axis: Axis,
|
left_right_axis: Axis,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GamepadMap {
|
impl GamepadMap {
|
||||||
|
|
||||||
/// Creates the default gamepad from an id.
|
/// Creates the default gamepad from an id.
|
||||||
///
|
///
|
||||||
/// Returns None if the gamepad corresponding to the id is not connected.
|
/// Returns None if the gamepad corresponding to the id is not connected.
|
||||||
pub fn from_id(id: u32) -> Option<GamepadMap> {
|
pub fn from_id(id: u32) -> Option<GamepadMap> {
|
||||||
|
|
||||||
if !is_connected(id) {
|
if !is_connected(id) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
@ -207,21 +182,18 @@ impl GamepadMap {
|
||||||
|
|
||||||
/// Converts an event and depending on the config, returns the corresponding action.
|
/// Converts an event and depending on the config, returns the corresponding action.
|
||||||
pub fn convert(&self, event: &Event) -> Option<Action> {
|
pub fn convert(&self, event: &Event) -> Option<Action> {
|
||||||
|
|
||||||
match event {
|
match event {
|
||||||
Event::JoystickButtonPressed { joystickid, button } if
|
Event::JoystickButtonPressed { joystickid, button }
|
||||||
*joystickid == self.id && *button == self.jump_button => {
|
if *joystickid == self.id && *button == self.jump_button =>
|
||||||
|
{
|
||||||
Some(Action::Jump(true))
|
Some(Action::Jump(true))
|
||||||
|
}
|
||||||
|
|
||||||
},
|
Event::JoystickButtonReleased { joystickid, button }
|
||||||
|
if *joystickid == self.id && *button == self.jump_button =>
|
||||||
Event::JoystickButtonReleased { joystickid, button } if
|
{
|
||||||
*joystickid == self.id && *button == self.jump_button => {
|
|
||||||
|
|
||||||
Some(Action::Jump(false))
|
Some(Action::Jump(false))
|
||||||
|
}
|
||||||
},
|
|
||||||
|
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
|
@ -229,10 +201,7 @@ impl GamepadMap {
|
||||||
|
|
||||||
/// Returns the direction of the directionnal buttons of the gamepad.
|
/// Returns the direction of the directionnal buttons of the gamepad.
|
||||||
pub fn direction(&self) -> Vector2<f32> {
|
pub fn direction(&self) -> Vector2<f32> {
|
||||||
Vector2::new(
|
Vector2::new(axis_position(self.id, self.left_right_axis) / 100.0, 0.0)
|
||||||
axis_position(self.id, self.left_right_axis) / 100.0,
|
|
||||||
0.0
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns whether the run button is held down.
|
/// Returns whether the run button is held down.
|
||||||
|
|
|
@ -1,15 +1,9 @@
|
||||||
|
use sfml::graphics::{FloatRect, IntRect};
|
||||||
use sfml::system::Vector2;
|
use sfml::system::Vector2;
|
||||||
use sfml::graphics::{
|
|
||||||
IntRect,
|
|
||||||
FloatRect
|
|
||||||
};
|
|
||||||
|
|
||||||
use engine::texture::Texture;
|
use engine::math::{clamp, Matrix};
|
||||||
use engine::renderer::Drawable;
|
use engine::renderer::Drawable;
|
||||||
use engine::math::{
|
use engine::texture::Texture;
|
||||||
Matrix,
|
|
||||||
clamp,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// This enum represents if the collision happens on the X axis or the Y axis.
|
/// This enum represents if the collision happens on the X axis or the Y axis.
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
|
@ -42,11 +36,9 @@ impl CollisionAxis {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// This struct represents the different sides from which a collision can occur.
|
/// This struct represents the different sides from which a collision can occur.
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||||
pub struct CollisionTile {
|
pub struct CollisionTile {
|
||||||
|
|
||||||
/// If the character comes from the top, it will collide if this bool is true.
|
/// If the character comes from the top, it will collide if this bool is true.
|
||||||
pub from_top: bool,
|
pub from_top: bool,
|
||||||
|
|
||||||
|
@ -58,7 +50,6 @@ pub struct CollisionTile {
|
||||||
|
|
||||||
/// If the character comes from the bottom, it will collide if this bool is true.
|
/// If the character comes from the bottom, it will collide if this bool is true.
|
||||||
pub from_bottom: bool,
|
pub from_bottom: bool,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CollisionTile {
|
impl CollisionTile {
|
||||||
|
@ -118,7 +109,6 @@ pub enum GraphicTile {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GraphicTile {
|
impl GraphicTile {
|
||||||
|
|
||||||
/// Checks if a graphic tile has a top border.
|
/// Checks if a graphic tile has a top border.
|
||||||
pub fn is_top(self) -> bool {
|
pub fn is_top(self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
|
@ -151,8 +141,6 @@ impl GraphicTile {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// Creates a vec containing all the non hidden graphic tiles.
|
/// Creates a vec containing all the non hidden graphic tiles.
|
||||||
pub fn all() -> Vec<GraphicTile> {
|
pub fn all() -> Vec<GraphicTile> {
|
||||||
vec![
|
vec![
|
||||||
|
@ -175,8 +163,8 @@ impl GraphicTile {
|
||||||
top: Option<CollisionTile>,
|
top: Option<CollisionTile>,
|
||||||
left: Option<CollisionTile>,
|
left: Option<CollisionTile>,
|
||||||
right: Option<CollisionTile>,
|
right: Option<CollisionTile>,
|
||||||
bottom: Option<CollisionTile>) -> GraphicTile {
|
bottom: Option<CollisionTile>,
|
||||||
|
) -> GraphicTile {
|
||||||
GraphicTile::from_neighbours(
|
GraphicTile::from_neighbours(
|
||||||
top.unwrap_or_else(CollisionTile::full),
|
top.unwrap_or_else(CollisionTile::full),
|
||||||
left.unwrap_or_else(CollisionTile::full),
|
left.unwrap_or_else(CollisionTile::full),
|
||||||
|
@ -190,15 +178,14 @@ impl GraphicTile {
|
||||||
top: CollisionTile,
|
top: CollisionTile,
|
||||||
left: CollisionTile,
|
left: CollisionTile,
|
||||||
right: CollisionTile,
|
right: CollisionTile,
|
||||||
bottom: CollisionTile) -> GraphicTile {
|
bottom: CollisionTile,
|
||||||
|
) -> GraphicTile {
|
||||||
let mut all = GraphicTile::all()
|
let mut all = GraphicTile::all()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|x| (x, true))
|
.map(|x| (x, true))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
for (ref mut tile, ref mut possible) in &mut all {
|
for (ref mut tile, ref mut possible) in &mut all {
|
||||||
|
|
||||||
if tile.is_top() == (top == CollisionTile::full()) {
|
if tile.is_top() == (top == CollisionTile::full()) {
|
||||||
*possible = false;
|
*possible = false;
|
||||||
}
|
}
|
||||||
|
@ -214,7 +201,6 @@ impl GraphicTile {
|
||||||
if tile.is_bottom() == (bottom == CollisionTile::full()) {
|
if tile.is_bottom() == (bottom == CollisionTile::full()) {
|
||||||
*possible = false;
|
*possible = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (tile, possible) in all {
|
for (tile, possible) in all {
|
||||||
|
@ -276,20 +262,16 @@ impl Drawable for PositionedTile {
|
||||||
|
|
||||||
/// The map represents the tiles contained in a level.
|
/// The map represents the tiles contained in a level.
|
||||||
pub struct Map {
|
pub struct Map {
|
||||||
|
|
||||||
/// The entrace point of the character in the map.
|
/// The entrace point of the character in the map.
|
||||||
entrance: (usize, usize),
|
entrance: (usize, usize),
|
||||||
|
|
||||||
/// The tiles contained in the level.
|
/// The tiles contained in the level.
|
||||||
tiles: Matrix<(CollisionTile, GraphicTile)>,
|
tiles: Matrix<(CollisionTile, GraphicTile)>,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Map {
|
impl Map {
|
||||||
|
|
||||||
/// Creates a map full of nothing, with a ground at the bottom.
|
/// Creates a map full of nothing, with a ground at the bottom.
|
||||||
pub fn new(rows: usize, cols: usize) -> Map {
|
pub fn new(rows: usize, cols: usize) -> Map {
|
||||||
|
|
||||||
let mut tiles = Matrix::from_size(rows, cols, CollisionTile::empty());
|
let mut tiles = Matrix::from_size(rows, cols, CollisionTile::empty());
|
||||||
let rows = tiles.rows();
|
let rows = tiles.rows();
|
||||||
|
|
||||||
|
@ -303,7 +285,6 @@ impl Map {
|
||||||
tiles[(25, 15)] = CollisionTile::full();
|
tiles[(25, 15)] = CollisionTile::full();
|
||||||
|
|
||||||
Map::from_collision_tiles(tiles)
|
Map::from_collision_tiles(tiles)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a map from a txt file.
|
/// Creates a map from a txt file.
|
||||||
|
@ -337,15 +318,12 @@ impl Map {
|
||||||
let rows = tiles.rows();
|
let rows = tiles.rows();
|
||||||
let cols = tiles.cols();
|
let cols = tiles.cols();
|
||||||
|
|
||||||
let mut matrix = Matrix::from_size(rows, cols,
|
let mut matrix =
|
||||||
(CollisionTile::empty(), GraphicTile::Hidden)
|
Matrix::from_size(rows, cols, (CollisionTile::empty(), GraphicTile::Hidden));
|
||||||
);
|
|
||||||
|
|
||||||
for i in 0..rows {
|
for i in 0..rows {
|
||||||
for j in 0..cols {
|
for j in 0..cols {
|
||||||
|
|
||||||
let graphic = if tiles[(i, j)] == CollisionTile::full() {
|
let graphic = if tiles[(i, j)] == CollisionTile::full() {
|
||||||
|
|
||||||
// TODO This is uggly
|
// TODO This is uggly
|
||||||
// If there is an overflow, we should give None instead
|
// If there is an overflow, we should give None instead
|
||||||
let (i, j) = (i as isize, j as isize);
|
let (i, j) = (i as isize, j as isize);
|
||||||
|
@ -402,9 +380,11 @@ impl Map {
|
||||||
/// Checks whether the bounding box collides with elements of the map.
|
/// Checks whether the bounding box collides with elements of the map.
|
||||||
///
|
///
|
||||||
/// Returns the new correct position.
|
/// Returns the new correct position.
|
||||||
pub fn collides_bbox(&self, old: FloatRect, new: FloatRect)
|
pub fn collides_bbox(
|
||||||
-> Option<(CollisionAxis, Vector2<f32>)> {
|
&self,
|
||||||
|
old: FloatRect,
|
||||||
|
new: FloatRect,
|
||||||
|
) -> Option<(CollisionAxis, Vector2<f32>)> {
|
||||||
let cols = self.tiles.cols() - 1;
|
let cols = self.tiles.cols() - 1;
|
||||||
let rows = self.tiles.rows() - 1;
|
let rows = self.tiles.rows() - 1;
|
||||||
|
|
||||||
|
@ -418,10 +398,8 @@ impl Map {
|
||||||
let mut collision_y = false;
|
let mut collision_y = false;
|
||||||
let mut new = new;
|
let mut new = new;
|
||||||
|
|
||||||
|
|
||||||
for col in min_col..=max_col {
|
for col in min_col..=max_col {
|
||||||
for row in min_row..=max_row {
|
for row in min_row..=max_row {
|
||||||
|
|
||||||
let tile_left = col as f32 * 16.0;
|
let tile_left = col as f32 * 16.0;
|
||||||
let tile_top = row as f32 * 16.0;
|
let tile_top = row as f32 * 16.0;
|
||||||
|
|
||||||
|
@ -432,10 +410,10 @@ impl Map {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collisions between feet and ground
|
// Collisions between feet and ground
|
||||||
if self.tiles[(row, col)].0.from_top &&
|
if self.tiles[(row, col)].0.from_top
|
||||||
old.top + old.height <= tile_top &&
|
&& old.top + old.height <= tile_top
|
||||||
new.top + new.height >= tile_top {
|
&& new.top + new.height >= tile_top
|
||||||
|
{
|
||||||
collision_y = true;
|
collision_y = true;
|
||||||
new.top = tile_top - new.height;
|
new.top = tile_top - new.height;
|
||||||
}
|
}
|
||||||
|
@ -445,10 +423,10 @@ impl Map {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collisions between right and right wall
|
// Collisions between right and right wall
|
||||||
if self.tiles[(row, col)].0.from_left &&
|
if self.tiles[(row, col)].0.from_left
|
||||||
old.left + old.width <= tile_left &&
|
&& old.left + old.width <= tile_left
|
||||||
new.left + new.width >= tile_left {
|
&& new.left + new.width >= tile_left
|
||||||
|
{
|
||||||
collision_x = true;
|
collision_x = true;
|
||||||
new.left = tile_left - new.width;
|
new.left = tile_left - new.width;
|
||||||
}
|
}
|
||||||
|
@ -458,10 +436,10 @@ impl Map {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collisions between left and left wall
|
// Collisions between left and left wall
|
||||||
if self.tiles[(row, col)].0.from_right &&
|
if self.tiles[(row, col)].0.from_right
|
||||||
old.left >= tile_left + 16.0 &&
|
&& old.left >= tile_left + 16.0
|
||||||
new.left <= tile_left + 16.0 {
|
&& new.left <= tile_left + 16.0
|
||||||
|
{
|
||||||
collision_x = true;
|
collision_x = true;
|
||||||
new.left = tile_left + 16.0;
|
new.left = tile_left + 16.0;
|
||||||
}
|
}
|
||||||
|
@ -471,14 +449,13 @@ impl Map {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collisions between head and roof
|
// Collisions between head and roof
|
||||||
if self.tiles[(row, col)].0.from_bottom &&
|
if self.tiles[(row, col)].0.from_bottom
|
||||||
old.top >= tile_top + 16.0 &&
|
&& old.top >= tile_top + 16.0
|
||||||
new.top <= tile_top + 16.0 {
|
&& new.top <= tile_top + 16.0
|
||||||
|
{
|
||||||
collision_y = true;
|
collision_y = true;
|
||||||
new.top = tile_top + 16.0;
|
new.top = tile_top + 16.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -494,10 +471,8 @@ impl Map {
|
||||||
|
|
||||||
/// Checks if two boxes overlap.
|
/// Checks if two boxes overlap.
|
||||||
pub fn overlap(box1: FloatRect, box2: FloatRect) -> bool {
|
pub fn overlap(box1: FloatRect, box2: FloatRect) -> bool {
|
||||||
box2.left < box1.left + box1.width &&
|
box2.left < box1.left + box1.width
|
||||||
box2.left + box2.width > box1.left &&
|
&& box2.left + box2.width > box1.left
|
||||||
box2.top < box1.top + box1.height &&
|
&& box2.top < box1.top + box1.height
|
||||||
box2.top + box2.height > box1.top
|
&& box2.top + box2.height > box1.top
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use std::ops::{
|
use std::ops::{Index, IndexMut};
|
||||||
Index,
|
|
||||||
IndexMut
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Clamp a number between two boundaries.
|
/// Clamp a number between two boundaries.
|
||||||
pub fn clamp(number: f32, min: f32, max: f32) -> f32 {
|
pub fn clamp(number: f32, min: f32, max: f32) -> f32 {
|
||||||
|
@ -30,7 +27,6 @@ pub fn duration_as_f32(duration: &Duration) -> f32 {
|
||||||
|
|
||||||
/// A generic matrix type, useful for levels.
|
/// A generic matrix type, useful for levels.
|
||||||
pub struct Matrix<T> {
|
pub struct Matrix<T> {
|
||||||
|
|
||||||
/// The number of rows of the matrix.
|
/// The number of rows of the matrix.
|
||||||
rows: usize,
|
rows: usize,
|
||||||
|
|
||||||
|
@ -39,10 +35,12 @@ pub struct Matrix<T> {
|
||||||
|
|
||||||
/// The contained data in the matrix.
|
/// The contained data in the matrix.
|
||||||
data: Vec<T>,
|
data: Vec<T>,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Matrix<T> where T: Clone {
|
impl<T> Matrix<T>
|
||||||
|
where
|
||||||
|
T: Clone,
|
||||||
|
{
|
||||||
/// Creates a matrix from an element and duplicates it.
|
/// Creates a matrix from an element and duplicates it.
|
||||||
pub fn from_size(rows: usize, cols: usize, element: T) -> Matrix<T> {
|
pub fn from_size(rows: usize, cols: usize, element: T) -> Matrix<T> {
|
||||||
Matrix {
|
Matrix {
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
/// This module contains the struct Scene.
|
/// This module contains the struct Scene.
|
||||||
pub mod scene;
|
pub mod scene;
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,19 @@
|
||||||
use sfml::system::Vector2;
|
use sfml::system::Vector2;
|
||||||
|
|
||||||
/// The gravity force.
|
/// The gravity force.
|
||||||
pub const G: Vector2<f32> = Vector2 { x: 0.0, y: 25.0 * 32.0 };
|
pub const G: Vector2<f32> = Vector2 {
|
||||||
|
x: 0.0,
|
||||||
|
y: 25.0 * 32.0,
|
||||||
|
};
|
||||||
|
|
||||||
/// The friction with the ground.
|
/// The friction with the ground.
|
||||||
pub const GROUND_FRICTION: Vector2<f32> = Vector2 { x: 0.5, y: 1.0 };
|
pub const GROUND_FRICTION: Vector2<f32> = Vector2 { x: 0.5, y: 1.0 };
|
||||||
|
|
||||||
/// The speed of a jump.
|
/// The speed of a jump.
|
||||||
pub const JUMP_SPEED: Vector2<f32> = Vector2 { x: 0.0, y: -12.5 * 32.0 };
|
pub const JUMP_SPEED: Vector2<f32> = Vector2 {
|
||||||
|
x: 0.0,
|
||||||
|
y: -12.5 * 32.0,
|
||||||
|
};
|
||||||
|
|
||||||
/// The maximum vertical speed.
|
/// The maximum vertical speed.
|
||||||
pub const MAXIMUM_VERTICAL_SPEED: f32 = 10.0 * 32.0;
|
pub const MAXIMUM_VERTICAL_SPEED: f32 = 10.0 * 32.0;
|
||||||
|
|
|
@ -1,26 +1,14 @@
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
use sfml::graphics::{
|
use sfml::graphics::{Color, IntRect, RenderTarget, RenderWindow, Sprite, View};
|
||||||
RenderWindow,
|
|
||||||
RenderTarget,
|
|
||||||
Sprite,
|
|
||||||
Color,
|
|
||||||
IntRect,
|
|
||||||
View,
|
|
||||||
};
|
|
||||||
|
|
||||||
use sfml::window::{
|
use sfml::window::{Event, Style};
|
||||||
Style,
|
|
||||||
Event,
|
|
||||||
};
|
|
||||||
|
|
||||||
use sfml::system::{
|
use sfml::system::Vector2;
|
||||||
Vector2,
|
|
||||||
};
|
|
||||||
|
|
||||||
use engine::texture::{Texture, TextureManager};
|
|
||||||
use engine::scene::Scene;
|
|
||||||
use engine::map::GraphicTile;
|
use engine::map::GraphicTile;
|
||||||
|
use engine::scene::Scene;
|
||||||
|
use engine::texture::{Texture, TextureManager};
|
||||||
|
|
||||||
/// Our custom drawable trait.
|
/// Our custom drawable trait.
|
||||||
pub trait Drawable {
|
pub trait Drawable {
|
||||||
|
@ -36,7 +24,6 @@ pub trait Drawable {
|
||||||
|
|
||||||
/// The game window.
|
/// The game window.
|
||||||
pub struct Renderer {
|
pub struct Renderer {
|
||||||
|
|
||||||
/// The window on which the rendering will be done.
|
/// The window on which the rendering will be done.
|
||||||
window: RenderWindow,
|
window: RenderWindow,
|
||||||
|
|
||||||
|
@ -45,17 +32,19 @@ pub struct Renderer {
|
||||||
|
|
||||||
/// The global timer of the renderer.
|
/// The global timer of the renderer.
|
||||||
started: Instant,
|
started: Instant,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Renderer {
|
impl Renderer {
|
||||||
/// Creates a new renderer.
|
/// Creates a new renderer.
|
||||||
pub fn new(width: u32, height: u32, fullscreen: bool) -> Renderer {
|
pub fn new(width: u32, height: u32, fullscreen: bool) -> Renderer {
|
||||||
|
|
||||||
let mut window = RenderWindow::new(
|
let mut window = RenderWindow::new(
|
||||||
(width, height),
|
(width, height),
|
||||||
"Free Rusty Maker",
|
"Free Rusty Maker",
|
||||||
if fullscreen { Style::FULLSCREEN } else { Style::CLOSE },
|
if fullscreen {
|
||||||
|
Style::FULLSCREEN
|
||||||
|
} else {
|
||||||
|
Style::CLOSE
|
||||||
|
},
|
||||||
&Default::default(),
|
&Default::default(),
|
||||||
);
|
);
|
||||||
window.set_vertical_sync_enabled(true);
|
window.set_vertical_sync_enabled(true);
|
||||||
|
|
|
@ -1,25 +1,22 @@
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use sfml::graphics::View;
|
||||||
use sfml::system::Vector2;
|
use sfml::system::Vector2;
|
||||||
use sfml::window::Event;
|
use sfml::window::Event;
|
||||||
use sfml::graphics::View;
|
|
||||||
|
|
||||||
use engine::character::Character;
|
use engine::character::Character;
|
||||||
use engine::map::Map;
|
use engine::map::Map;
|
||||||
|
|
||||||
/// Contains everything needed to play.
|
/// Contains everything needed to play.
|
||||||
pub struct Scene {
|
pub struct Scene {
|
||||||
|
|
||||||
/// The characters contained in the scene.
|
/// The characters contained in the scene.
|
||||||
characters: Vec<Character>,
|
characters: Vec<Character>,
|
||||||
|
|
||||||
/// The map of the scene.
|
/// The map of the scene.
|
||||||
map: Map,
|
map: Map,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Scene {
|
impl Scene {
|
||||||
|
|
||||||
/// Creates an empty scene.
|
/// Creates an empty scene.
|
||||||
pub fn new() -> Scene {
|
pub fn new() -> Scene {
|
||||||
Scene {
|
Scene {
|
||||||
|
@ -41,7 +38,7 @@ impl Scene {
|
||||||
fn controlable(&self) -> Option<&Character> {
|
fn controlable(&self) -> Option<&Character> {
|
||||||
for character in &self.characters {
|
for character in &self.characters {
|
||||||
if character.controls().is_some() {
|
if character.controls().is_some() {
|
||||||
return Some(&character)
|
return Some(&character);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
|
@ -78,7 +75,6 @@ impl Scene {
|
||||||
|
|
||||||
/// Updates the whole scene.
|
/// Updates the whole scene.
|
||||||
pub fn update(&mut self, duration: &Duration) {
|
pub fn update(&mut self, duration: &Duration) {
|
||||||
|
|
||||||
for c in &mut self.characters {
|
for c in &mut self.characters {
|
||||||
let old = c.bbox();
|
let old = c.bbox();
|
||||||
|
|
||||||
|
@ -118,7 +114,6 @@ impl Scene {
|
||||||
pub fn map(&self) -> &Map {
|
pub fn map(&self) -> &Map {
|
||||||
&self.map
|
&self.map
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Trait that needs to be implemented for everything that can be updatable.
|
/// Trait that needs to be implemented for everything that can be updatable.
|
||||||
|
|
|
@ -57,8 +57,14 @@ macro_rules! make_textures {
|
||||||
}
|
}
|
||||||
|
|
||||||
make_textures!(
|
make_textures!(
|
||||||
Mario, mario, make_mario_texture, "../../../assets/textures/mario.png",
|
Mario,
|
||||||
Overworld, overworld, make_overworld_texture, "../../../assets/textures/overworld.png",
|
mario,
|
||||||
|
make_mario_texture,
|
||||||
|
"../../../assets/textures/mario.png",
|
||||||
|
Overworld,
|
||||||
|
overworld,
|
||||||
|
make_overworld_texture,
|
||||||
|
"../../../assets/textures/overworld.png",
|
||||||
);
|
);
|
||||||
|
|
||||||
impl TextureManager {
|
impl TextureManager {
|
||||||
|
|
Loading…
Reference in New Issue