summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2019-06-21 12:00:24 -0500
committertom barrett <spalf0@gmail.com>2019-06-21 12:00:24 -0500
commit0438244915f9116237cc706e5e80ecedfbc7d591 (patch)
tree7b8e168a4e17415b4d55ae999f6c781bc48ed6c5 /src
parent05856461f35caa0cebf8ba69cbf36132485d5488 (diff)
cannot leave map boundry
Diffstat (limited to 'src')
-rw-r--r--src/player.rs27
-rw-r--r--src/state.rs2
2 files changed, 21 insertions, 8 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,
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),
})
}
}