From 93d16789556b772c263cad3789fba4e7ca35a9a7 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Sat, 6 Oct 2018 21:28:26 +0200 Subject: [PATCH] Entrance of a level --- src/engine/character/mod.rs | 4 ++++ src/engine/map/mod.rs | 14 ++++++++++++++ src/engine/physics/mod.rs | 7 +++++-- src/engine/scene/mod.rs | 4 ++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/engine/character/mod.rs b/src/engine/character/mod.rs index d06b2de..3de743c 100644 --- a/src/engine/character/mod.rs +++ b/src/engine/character/mod.rs @@ -191,6 +191,10 @@ impl Updatable for Character { self.speed.x *= physics::GROUND_FRICTION.x; self.speed += accel * duration + force * 64.0; + if self.speed.y > physics::MAXIMUM_VERTICAL_SPEED { + self.speed.y = physics::MAXIMUM_VERTICAL_SPEED; + } + // Compute position self.position += self.speed * duration; diff --git a/src/engine/map/mod.rs b/src/engine/map/mod.rs index 8ac1ff8..f93274f 100644 --- a/src/engine/map/mod.rs +++ b/src/engine/map/mod.rs @@ -274,6 +274,9 @@ impl Drawable for PositionedTile { /// The map represents the tiles contained in a level. pub struct Map { + /// The entrace point of the character in the map. + entrance: (usize, usize), + /// The tiles contained in the level. tiles: Matrix<(CollisionTile, GraphicTile)>, @@ -359,10 +362,21 @@ impl Map { } Map { + entrance: Map::find_entrance(&matrix), tiles: matrix, } } + /// Finds a possible entrance. + pub fn find_entrance(tiles: &Matrix<(CollisionTile, GraphicTile)>) -> (usize, usize) { + (tiles.rows() - 5, 1) + } + + /// Returns the entrance of the map. + pub fn entrance(&self) -> (usize, usize) { + self.entrance + } + /// Returns an iterator to the positioned tiles. pub fn at(&self, row: usize, col: usize) -> PositionedTile { PositionedTile { diff --git a/src/engine/physics/mod.rs b/src/engine/physics/mod.rs index a633965..b100677 100644 --- a/src/engine/physics/mod.rs +++ b/src/engine/physics/mod.rs @@ -1,10 +1,13 @@ use sfml::system::Vector2; /// The gravity force. -pub const G: Vector2 = Vector2 { x: 0.0, y: 50.0 * 32.0 }; +pub const G: Vector2 = Vector2 { x: 0.0, y: 25.0 * 32.0 }; /// The friction with the ground. pub const GROUND_FRICTION: Vector2 = Vector2 { x: 0.5, y: 1.0 }; /// The speed of a jump. -pub const JUMP_SPEED: Vector2 = Vector2 { x: 0.0, y: -600.0 }; +pub const JUMP_SPEED: Vector2 = Vector2 { x: 0.0, y: -12.5 * 32.0 }; + +/// The maximum vertical speed. +pub const MAXIMUM_VERTICAL_SPEED: f32 = 10.0 * 32.0; diff --git a/src/engine/scene/mod.rs b/src/engine/scene/mod.rs index 3a6cca7..5de8270 100644 --- a/src/engine/scene/mod.rs +++ b/src/engine/scene/mod.rs @@ -30,6 +30,10 @@ impl Scene { /// Adds a character to the scene. pub fn add(&mut self, character: Character) { + let mut character = character; + character.position.x = self.map.entrance().1 as f32 * 16.0; + character.position.y = self.map.entrance().0 as f32 * 16.0; + self.characters.push(character); }