Changed check if player is within world

This commit is contained in:
Paul-Christian Volkmer 2022-02-18 23:33:59 +01:00
parent 8ee16171da
commit 0d7a4fb391
2 changed files with 58 additions and 73 deletions

View File

@ -4,7 +4,7 @@ use sdl2::rect::{Point, Rect};
use sdl2::render::{Texture, WindowCanvas}; use sdl2::render::{Texture, WindowCanvas};
pub struct Player { pub struct Player {
pub position: Point, position: Point,
direction: PlayerDirection, direction: PlayerDirection,
footstep: u8, footstep: u8,
pub empty_glasses: u8, pub empty_glasses: u8,
@ -60,33 +60,28 @@ impl Player {
} }
pub fn center(&self) -> Point { 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 { 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) { pub fn render(&self, canvas: &mut WindowCanvas, texture: &Texture) {
let direction = match self.direction { self.sprite()
PlayerDirection::Down => sprite::PlayerDirection::Down, .render(canvas, texture, self.position.x(), self.position.y());
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(),
);
} }
pub fn face_up(&mut self) { pub fn face_up(&mut self) {
@ -132,4 +127,21 @@ impl Player {
pub fn stop(&mut self) { pub fn stop(&mut self) {
self.footstep = 0; 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)
}
} }

View File

@ -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<BoxAreaPosition> { pub fn collides_with_box_area(&mut self) -> Option<BoxAreaPosition> {
if self.right_top_box_area.collides_with(&self.player) { if self.right_top_box_area.collides_with(&self.player) {
return Some(BoxAreaPosition::RightTop); return Some(BoxAreaPosition::RightTop);
@ -69,42 +73,34 @@ impl World {
} }
pub fn move_up(&mut self) { pub fn move_up(&mut self) {
if self.player.position.y > 50 { self.player.move_up();
self.player.move_up(); if self.collides_with_stop() || !self.player.within_rect(&Self::playable_rect()) {
if self.collides_with_stop() { self.player.move_down();
self.player.move_down(); self.player.face_up();
self.player.face_up();
}
} }
} }
pub fn move_down(&mut self) { pub fn move_down(&mut self) {
if self.player.position.y < 600 - 110 { self.player.move_down();
self.player.move_down(); if self.collides_with_stop() || !self.player.within_rect(&Self::playable_rect()) {
if self.collides_with_stop() { self.player.move_up();
self.player.move_up(); self.player.face_down();
self.player.face_down();
}
} }
} }
pub fn move_left(&mut self) { pub fn move_left(&mut self) {
if self.player.position.x > 0 { self.player.move_left();
self.player.move_left(); if self.collides_with_stop() || !self.player.within_rect(&Self::playable_rect()) {
if self.collides_with_stop() { self.player.move_right();
self.player.move_right(); self.player.face_left();
self.player.face_left();
}
} }
} }
pub fn move_right(&mut self) { pub fn move_right(&mut self) {
if self.player.position.x < 800 - 40 { self.player.move_right();
self.player.move_right(); if self.collides_with_stop() || !self.player.within_rect(&Self::playable_rect()) {
if self.collides_with_stop() { self.player.move_left();
self.player.move_left(); self.player.face_right();
self.player.face_right();
}
} }
} }
@ -137,14 +133,14 @@ impl World {
canvas.clear(); canvas.clear();
canvas.set_draw_color(Color::RGB(160, 90, 44)); 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)); canvas.set_draw_color(Color::RGB(206, 182, 115));
// Points/Glasses // Points/Glasses
(1..=GLASS_SPACE).for_each(|i| { (1..=GLASS_SPACE).for_each(|i| {
canvas.set_draw_color(Color::RGB(128, 51, 0)); 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 { if self.player.filled_glasses + self.player.empty_glasses >= i {
Sprite::GlassEmpty.render(canvas, texture, (i as i32) * 25 - 15, 10); Sprite::GlassEmpty.render(canvas, texture, (i as i32) * 25 - 15, 10);
@ -187,10 +183,10 @@ impl World {
let t2 = canvas.texture_creator(); let t2 = canvas.texture_creator();
let t2 = t2.create_texture_from_surface(&x).unwrap(); let t2 = t2.create_texture_from_surface(&x).unwrap();
canvas.copy( let _r = canvas.copy(
&t2, &t2,
x.rect(), 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)); canvas.set_draw_color(Color::RGB(206, 182, 115));
@ -236,29 +232,6 @@ impl BoxArea {
Rect::new(x_offset, y_offset, 110, 110) 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 { fn collides_with(&self, player: &Player) -> bool {
self.bounding_rect().contains_point(player.center()) self.bounding_rect().contains_point(player.center())
} }