From 0438244915f9116237cc706e5e80ecedfbc7d591 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Fri, 21 Jun 2019 12:00:24 -0500 Subject: cannot leave map boundry --- src/player.rs | 27 ++++++++++++++++++++------- src/state.rs | 2 +- 2 files changed, 21 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/player.rs b/src/player.rs index 0350656..bc019b9 100644 --- a/src/player.rs +++ b/src/player.rs @@ -13,10 +13,12 @@ pub struct Player { tile: Rect, animation: Vec<(u32, Rect)>, animations: HashMap>, + map_height: f32, + map_width: f32, } impl Player { - pub fn new() -> Player { + pub fn new(dimensions: (f32, f32)) -> Player { let mut animations = HashMap::new(); let mut idle = Vec::new(); @@ -36,6 +38,8 @@ impl Player { tile: Rect::zero(), animation: Vec::new(), animations, + map_width: dimensions.0, + map_height: dimensions.1, } } @@ -77,6 +81,21 @@ impl Player { PlayerState::MovingRight => self.position.x += constants::PLAYER_SPEED, PlayerState::Idle => (), } + + let pixel_width = constants::TILE_WIDTH * constants::TILE_SCALE; + let pixel_height = constants::TILE_HEIGHT * constants::TILE_SCALE; + + if self.position.x < 0.0 { + self.position.x = 0.0; + } else if self.position.x + pixel_height > self.map_width { + self.position.x = self.map_width - pixel_width; + } + + if self.position.y < 0.0 { + self.position.y = 0.0; + } else if self.position.y + pixel_height > self.map_height { + self.position.y = self.map_height - pixel_height; + } } fn find_tile(&mut self, context: &mut Context) { @@ -160,12 +179,6 @@ impl Player { } } -impl Default for Player { - fn default() -> Self { - Player::new() - } -} - #[derive(Clone, Hash, Eq, PartialEq)] enum PlayerState { Idle, diff --git a/src/state.rs b/src/state.rs index 4962c39..b0a497c 100644 --- a/src/state.rs +++ b/src/state.rs @@ -28,7 +28,7 @@ impl State { tileset: Tileset::new(filesystem::open(context, "/tileset.tsx")?), spritebatch: SpriteBatch::new(image), camera: Camera::new(context, map_dimensions), - player: Player::new(), + player: Player::new(map_dimensions), }) } } -- cgit v1.2.3