From 0d7a4fb391f46c4ffe767c40d29e380b90f951cc Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Fri, 18 Feb 2022 23:33:59 +0100 Subject: [PATCH] Changed check if player is within world --- src/player.rs | 56 +++++++++++++++++++++++--------------- src/world.rs | 75 +++++++++++++++++---------------------------------- 2 files changed, 58 insertions(+), 73 deletions(-) diff --git a/src/player.rs b/src/player.rs index b79d5f3..7dcabee 100644 --- a/src/player.rs +++ b/src/player.rs @@ -4,7 +4,7 @@ use sdl2::rect::{Point, Rect}; use sdl2::render::{Texture, WindowCanvas}; pub struct Player { - pub position: Point, + position: Point, direction: PlayerDirection, footstep: u8, pub empty_glasses: u8, @@ -60,33 +60,28 @@ impl Player { } pub fn center(&self) -> Point { - Point::new(self.position.x() + 19, self.position.y() + 56) + self.bounding_rect().center() } pub fn bounding_rect(&self) -> Rect { - Rect::new(self.position.x(), self.position.y(), 40, 115) + Rect::new( + self.position.x(), + self.position.y(), + self.sprite().rect().width(), + self.sprite().rect().height(), + ) + } + + pub fn within_rect(&self, rect: &Rect) -> bool { + self.position.y > rect.y() + && self.position.y < rect.y() + (rect.height() - self.bounding_rect().height()) as i32 + && self.position.x > rect.x() + && self.position.x < (rect.width() - self.bounding_rect().width()) as i32 } pub fn render(&self, canvas: &mut WindowCanvas, texture: &Texture) { - let direction = match self.direction { - PlayerDirection::Down => sprite::PlayerDirection::Down, - PlayerDirection::Left => sprite::PlayerDirection::Left, - PlayerDirection::Right => sprite::PlayerDirection::Right, - PlayerDirection::Up => sprite::PlayerDirection::Up, - }; - - let footstep = match self.footstep { - 1 => sprite::PlayerFootstep::Left, - 2 => sprite::PlayerFootstep::Right, - _ => sprite::PlayerFootstep::None, - }; - - Sprite::Player(direction, footstep).render( - canvas, - texture, - self.position.x(), - self.position.y(), - ); + self.sprite() + .render(canvas, texture, self.position.x(), self.position.y()); } pub fn face_up(&mut self) { @@ -132,4 +127,21 @@ impl Player { pub fn stop(&mut self) { self.footstep = 0; } + + fn sprite(&self) -> Sprite { + let direction = match self.direction { + PlayerDirection::Down => sprite::PlayerDirection::Down, + PlayerDirection::Left => sprite::PlayerDirection::Left, + PlayerDirection::Right => sprite::PlayerDirection::Right, + PlayerDirection::Up => sprite::PlayerDirection::Up, + }; + + let footstep = match self.footstep { + 1 => sprite::PlayerFootstep::Left, + 2 => sprite::PlayerFootstep::Right, + _ => sprite::PlayerFootstep::None, + }; + + Sprite::Player(direction, footstep) + } } diff --git a/src/world.rs b/src/world.rs index 4ce9190..bdd370a 100644 --- a/src/world.rs +++ b/src/world.rs @@ -38,6 +38,10 @@ impl World { } } + pub fn playable_rect() -> Rect { + Rect::new(0, 50, 800, 550) + } + pub fn collides_with_box_area(&mut self) -> Option { if self.right_top_box_area.collides_with(&self.player) { return Some(BoxAreaPosition::RightTop); @@ -69,42 +73,34 @@ impl World { } pub fn move_up(&mut self) { - if self.player.position.y > 50 { - self.player.move_up(); - if self.collides_with_stop() { - self.player.move_down(); - self.player.face_up(); - } + self.player.move_up(); + if self.collides_with_stop() || !self.player.within_rect(&Self::playable_rect()) { + self.player.move_down(); + self.player.face_up(); } } pub fn move_down(&mut self) { - if self.player.position.y < 600 - 110 { - self.player.move_down(); - if self.collides_with_stop() { - self.player.move_up(); - self.player.face_down(); - } + self.player.move_down(); + if self.collides_with_stop() || !self.player.within_rect(&Self::playable_rect()) { + self.player.move_up(); + self.player.face_down(); } } pub fn move_left(&mut self) { - if self.player.position.x > 0 { - self.player.move_left(); - if self.collides_with_stop() { - self.player.move_right(); - self.player.face_left(); - } + self.player.move_left(); + if self.collides_with_stop() || !self.player.within_rect(&Self::playable_rect()) { + self.player.move_right(); + self.player.face_left(); } } pub fn move_right(&mut self) { - if self.player.position.x < 800 - 40 { - self.player.move_right(); - if self.collides_with_stop() { - self.player.move_left(); - self.player.face_right(); - } + self.player.move_right(); + if self.collides_with_stop() || !self.player.within_rect(&Self::playable_rect()) { + self.player.move_left(); + self.player.face_right(); } } @@ -137,14 +133,14 @@ impl World { canvas.clear(); canvas.set_draw_color(Color::RGB(160, 90, 44)); - canvas.fill_rect(Rect::new(0, 0, 800, 45)); + let _r = canvas.fill_rect(Rect::new(0, 0, 800, 45)); canvas.set_draw_color(Color::RGB(206, 182, 115)); // Points/Glasses (1..=GLASS_SPACE).for_each(|i| { canvas.set_draw_color(Color::RGB(128, 51, 0)); - canvas.fill_rect(Rect::new(5, 37, GLASS_SPACE as u32 * 25 + 5, 4)); + let _r = canvas.fill_rect(Rect::new(5, 37, GLASS_SPACE as u32 * 25 + 5, 4)); if self.player.filled_glasses + self.player.empty_glasses >= i { Sprite::GlassEmpty.render(canvas, texture, (i as i32) * 25 - 15, 10); @@ -187,10 +183,10 @@ impl World { let t2 = canvas.texture_creator(); let t2 = t2.create_texture_from_surface(&x).unwrap(); - canvas.copy( + let _r = canvas.copy( &t2, x.rect(), - Some(Rect::new(790 - x.width() as i32, 8, x.width(), x.height())), + Some(Rect::new(790 - x.width() as i32, 16, x.width(), x.height())), ); canvas.set_draw_color(Color::RGB(206, 182, 115)); @@ -236,29 +232,6 @@ impl BoxArea { Rect::new(x_offset, y_offset, 110, 110) } - fn enter_rect(&self) -> Rect { - match self.position { - BoxAreaPosition::RightTop => { - Rect::new(self.bounding_rect().x(), self.bounding_rect().y(), 25, 110) - } - BoxAreaPosition::RightBottom => { - Rect::new(self.bounding_rect().x(), self.bounding_rect().y(), 25, 110) - } - BoxAreaPosition::LeftBottom => Rect::new( - self.bounding_rect().x() + 85, - self.bounding_rect().y(), - 25, - 110, - ), - BoxAreaPosition::LeftTop => Rect::new( - self.bounding_rect().x() + 85, - self.bounding_rect().y(), - 25, - 110, - ), - } - } - fn collides_with(&self, player: &Player) -> bool { self.bounding_rect().contains_point(player.center()) }