diff options
author | tom barrett <spalf0@gmail.com> | 2019-06-21 12:00:24 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-06-21 12:00:24 -0500 |
commit | 0438244915f9116237cc706e5e80ecedfbc7d591 (patch) | |
tree | 7b8e168a4e17415b4d55ae999f6c781bc48ed6c5 /src/player.rs | |
parent | 05856461f35caa0cebf8ba69cbf36132485d5488 (diff) |
cannot leave map boundry
Diffstat (limited to 'src/player.rs')
-rw-r--r-- | src/player.rs | 27 |
1 files changed, 20 insertions, 7 deletions
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<PlayerState, Vec<(u32, Rect)>>, + 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, |