Add some short descriptions and removed indirection for player move

This commit is contained in:
Paul-Christian Volkmer 2022-03-22 12:49:27 +01:00
parent f6dedfca72
commit ccf90b4da7
3 changed files with 50 additions and 43 deletions

View File

@ -3,6 +3,7 @@ use crate::{sprite, GLASS_SPACE};
use sdl2::rect::{Point, Rect}; use sdl2::rect::{Point, Rect};
use sdl2::render::{Texture, WindowCanvas}; use sdl2::render::{Texture, WindowCanvas};
/// The player with his position, direction ...
pub struct Player { pub struct Player {
position: Point, position: Point,
direction: PlayerDirection, direction: PlayerDirection,
@ -12,6 +13,7 @@ pub struct Player {
pub points: u32, pub points: u32,
} }
/// Player only can turn in 90deg angle
enum PlayerDirection { enum PlayerDirection {
Up, Up,
Down, Down,
@ -20,6 +22,7 @@ enum PlayerDirection {
} }
impl Player { impl Player {
/// Initializes Player with fixed position and direction.
pub fn init() -> Player { pub fn init() -> Player {
Player { Player {
position: Point::new(380, 250), position: Point::new(380, 250),
@ -31,29 +34,35 @@ impl Player {
} }
} }
/// Checks, if player can pick a glass or if inventory is full
pub fn can_pick_glass(&self) -> bool { pub fn can_pick_glass(&self) -> bool {
self.empty_glasses + self.filled_glasses < GLASS_SPACE self.empty_glasses + self.filled_glasses < GLASS_SPACE
} }
/// Make player pick a glass
pub fn pick_glass(&mut self) { pub fn pick_glass(&mut self) {
self.empty_glasses += 1; self.empty_glasses += 1;
self.points += 2 self.points += 2
} }
/// Checks if player has an empty glass and can fill one glass with wine
pub fn can_fill_glass(&self) -> bool { pub fn can_fill_glass(&self) -> bool {
self.empty_glasses > 0 self.empty_glasses > 0
} }
/// Make player fill one glass with wine
pub fn fill_glass(&mut self) { pub fn fill_glass(&mut self) {
self.empty_glasses -= 1; self.empty_glasses -= 1;
self.filled_glasses += 1; self.filled_glasses += 1;
self.points += 3 self.points += 3
} }
/// Checks if player has at least one filled glass
pub fn can_drink_glass(&self) -> bool { pub fn can_drink_glass(&self) -> bool {
self.filled_glasses > 0 self.filled_glasses > 0
} }
/// Make player drink one glass of wine
pub fn drink_glass(&mut self) { pub fn drink_glass(&mut self) {
self.filled_glasses -= 1; self.filled_glasses -= 1;
self.points += 5 self.points += 5
@ -79,6 +88,7 @@ impl Player {
&& self.position.x < (rect.width() - self.bounding_rect().width()) as i32 && self.position.x < (rect.width() - self.bounding_rect().width()) as i32
} }
/// Renders player
pub fn render(&self, canvas: &mut WindowCanvas, texture: &Texture) { pub fn render(&self, canvas: &mut WindowCanvas, texture: &Texture) {
self.sprite() self.sprite()
.render(canvas, texture, self.position.x(), self.position.y()); .render(canvas, texture, self.position.x(), self.position.y());

View File

@ -30,6 +30,7 @@ pub enum PlayerDirection {
} }
impl Sprite { impl Sprite {
/// Returns bounding rect of current sprite.
pub fn rect(&self) -> Rect { pub fn rect(&self) -> Rect {
match &self { match &self {
Sprite::BottleEmpty => Rect::new(35, 550, 20, 50), Sprite::BottleEmpty => Rect::new(35, 550, 20, 50),
@ -47,10 +48,12 @@ impl Sprite {
} }
} }
/// Returns size of currect sprite
pub fn size(&self) -> (u32, u32) { pub fn size(&self) -> (u32, u32) {
self.rect().size() self.rect().size()
} }
/// Renders current sprite with given Canvas and texture at the given position,
pub fn render(&self, canvas: &mut WindowCanvas, texture: &Texture, x: i32, y: i32) { pub fn render(&self, canvas: &mut WindowCanvas, texture: &Texture, x: i32, y: i32) {
let render_rect = Rect::new(x, y, self.rect().width(), self.rect().height()); let render_rect = Rect::new(x, y, self.rect().width(), self.rect().height());
let _r = canvas.copy(texture, self.rect(), render_rect); let _r = canvas.copy(texture, self.rect(), render_rect);

View File

@ -17,7 +17,9 @@ pub struct World {
stops: Vec<Point>, stops: Vec<Point>,
} }
/// The world, the player and any item exists within
impl World { impl World {
/// Creates and initializes new playable world.
pub fn init() -> World { pub fn init() -> World {
World { World {
player: Player::init(), player: Player::init(),
@ -45,75 +47,58 @@ impl World {
Rect::new(0, 50, 800, 550) Rect::new(0, 50, 800, 550)
} }
/// Handles key events for player move.
///
/// This checks if player collides with any stop item or will move out of world.
/// If player can move, move him and turn him to the correct side.
pub fn handle_event(&mut self, event: Event) { pub fn handle_event(&mut self, event: Event) {
match event { match event {
Event::KeyDown { Event::KeyDown {
keycode: Some(Keycode::Up) | Some(Keycode::W), keycode: Some(Keycode::Up) | Some(Keycode::W),
.. ..
} => { } => {
self.move_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();
}
} }
Event::KeyDown { Event::KeyDown {
keycode: Some(Keycode::Down) | Some(Keycode::S), keycode: Some(Keycode::Down) | Some(Keycode::S),
.. ..
} => { } => {
self.move_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();
}
} }
Event::KeyDown { Event::KeyDown {
keycode: Some(Keycode::Left) | Some(Keycode::A), keycode: Some(Keycode::Left) | Some(Keycode::A),
.. ..
} => { } => {
self.move_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();
}
} }
Event::KeyDown { Event::KeyDown {
keycode: Some(Keycode::Right) | Some(Keycode::D), keycode: Some(Keycode::Right) | Some(Keycode::D),
.. ..
} => { } => {
self.move_right(); self.player.move_right();
} if self.collides_with_stop() || !self.player.within_rect(&Self::playable_rect()) {
Event::KeyUp { .. } => { self.player.move_left();
self.stop_player(); self.player.face_right();
}
} }
Event::KeyUp { .. } => self.player.stop(),
_ => {} _ => {}
} }
} }
pub fn move_up(&mut self) { /// Updates box areas to provide new boxes and remove items after some time
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) {
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) {
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) {
self.player.move_right();
if self.collides_with_stop() || !self.player.within_rect(&Self::playable_rect()) {
self.player.move_left();
self.player.face_right();
}
}
pub fn stop_player(&mut self) {
self.player.stop()
}
pub fn update_box_areas(&mut self) { pub fn update_box_areas(&mut self) {
World::update_box_area(&mut self.right_top_box_area); World::update_box_area(&mut self.right_top_box_area);
World::update_box_area(&mut self.right_bottom_box_area); World::update_box_area(&mut self.right_bottom_box_area);
@ -121,11 +106,13 @@ impl World {
World::update_box_area(&mut self.left_top_box_area); World::update_box_area(&mut self.left_top_box_area);
} }
/// Handles both, collisions with lounge and any box area
pub fn handle_collisions(&mut self) { pub fn handle_collisions(&mut self) {
self.handle_lounge_collisions(); self.handle_lounge_collisions();
self.handle_boxarea_collisions(); self.handle_boxarea_collisions();
} }
/// Renders world using given canvas, texture and font
pub fn render(&self, canvas: &mut WindowCanvas, texture: &Texture, font: &Font) { pub fn render(&self, canvas: &mut WindowCanvas, texture: &Texture, font: &Font) {
canvas.clear(); canvas.clear();
@ -299,6 +286,7 @@ struct BoxArea {
} }
impl BoxArea { impl BoxArea {
/// Creates a new BoxArea
fn new(position: BoxAreaPosition, content: BoxAreaContent) -> BoxArea { fn new(position: BoxAreaPosition, content: BoxAreaContent) -> BoxArea {
BoxArea { BoxArea {
position, position,
@ -329,10 +317,12 @@ impl BoxArea {
Rect::new(x_offset, y_offset, 110, 110) Rect::new(x_offset, y_offset, 110, 110)
} }
/// Checks if player collides with this BoxSrea
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())
} }
/// Renders BoxArea using goven Canvas and Texture
fn render(&self, canvas: &mut WindowCanvas, texture: &Texture) { fn render(&self, canvas: &mut WindowCanvas, texture: &Texture) {
let x_offset = self.bounding_rect().x(); let x_offset = self.bounding_rect().x();
let y_offset = self.bounding_rect().y(); let y_offset = self.bounding_rect().y();
@ -366,6 +356,8 @@ impl BoxArea {
} }
} }
/// Position of a BoxArea.
/// There are only four possible values for each vertex of the world.
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum BoxAreaPosition { pub enum BoxAreaPosition {
RightTop, RightTop,
@ -374,6 +366,7 @@ pub enum BoxAreaPosition {
LeftTop, LeftTop,
} }
/// Content of a BoxArea
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum BoxAreaContent { pub enum BoxAreaContent {
Nothing, Nothing,
@ -384,6 +377,7 @@ pub enum BoxAreaContent {
} }
impl BoxAreaContent { impl BoxAreaContent {
/// Selects new random BoxAreaContent
fn random() -> BoxAreaContent { fn random() -> BoxAreaContent {
match rand::random::<i32>() % 5 { match rand::random::<i32>() % 5 {
1 | 4 => BoxAreaContent::EmptyGlass, 1 | 4 => BoxAreaContent::EmptyGlass,