mirror of
https://github.com/pcvolkmer/winelounge.git
synced 2025-04-19 18:36:50 +00:00
Add some short descriptions and removed indirection for player move
This commit is contained in:
parent
f6dedfca72
commit
ccf90b4da7
@ -3,6 +3,7 @@ use crate::{sprite, GLASS_SPACE};
|
||||
use sdl2::rect::{Point, Rect};
|
||||
use sdl2::render::{Texture, WindowCanvas};
|
||||
|
||||
/// The player with his position, direction ...
|
||||
pub struct Player {
|
||||
position: Point,
|
||||
direction: PlayerDirection,
|
||||
@ -12,6 +13,7 @@ pub struct Player {
|
||||
pub points: u32,
|
||||
}
|
||||
|
||||
/// Player only can turn in 90deg angle
|
||||
enum PlayerDirection {
|
||||
Up,
|
||||
Down,
|
||||
@ -20,6 +22,7 @@ enum PlayerDirection {
|
||||
}
|
||||
|
||||
impl Player {
|
||||
/// Initializes Player with fixed position and direction.
|
||||
pub fn init() -> Player {
|
||||
Player {
|
||||
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 {
|
||||
self.empty_glasses + self.filled_glasses < GLASS_SPACE
|
||||
}
|
||||
|
||||
/// Make player pick a glass
|
||||
pub fn pick_glass(&mut self) {
|
||||
self.empty_glasses += 1;
|
||||
self.points += 2
|
||||
}
|
||||
|
||||
/// Checks if player has an empty glass and can fill one glass with wine
|
||||
pub fn can_fill_glass(&self) -> bool {
|
||||
self.empty_glasses > 0
|
||||
}
|
||||
|
||||
/// Make player fill one glass with wine
|
||||
pub fn fill_glass(&mut self) {
|
||||
self.empty_glasses -= 1;
|
||||
self.filled_glasses += 1;
|
||||
self.points += 3
|
||||
}
|
||||
|
||||
/// Checks if player has at least one filled glass
|
||||
pub fn can_drink_glass(&self) -> bool {
|
||||
self.filled_glasses > 0
|
||||
}
|
||||
|
||||
/// Make player drink one glass of wine
|
||||
pub fn drink_glass(&mut self) {
|
||||
self.filled_glasses -= 1;
|
||||
self.points += 5
|
||||
@ -79,6 +88,7 @@ impl Player {
|
||||
&& self.position.x < (rect.width() - self.bounding_rect().width()) as i32
|
||||
}
|
||||
|
||||
/// Renders player
|
||||
pub fn render(&self, canvas: &mut WindowCanvas, texture: &Texture) {
|
||||
self.sprite()
|
||||
.render(canvas, texture, self.position.x(), self.position.y());
|
||||
|
@ -30,6 +30,7 @@ pub enum PlayerDirection {
|
||||
}
|
||||
|
||||
impl Sprite {
|
||||
/// Returns bounding rect of current sprite.
|
||||
pub fn rect(&self) -> Rect {
|
||||
match &self {
|
||||
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) {
|
||||
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) {
|
||||
let render_rect = Rect::new(x, y, self.rect().width(), self.rect().height());
|
||||
let _r = canvas.copy(texture, self.rect(), render_rect);
|
||||
|
80
src/world.rs
80
src/world.rs
@ -17,7 +17,9 @@ pub struct World {
|
||||
stops: Vec<Point>,
|
||||
}
|
||||
|
||||
/// The world, the player and any item exists within
|
||||
impl World {
|
||||
/// Creates and initializes new playable world.
|
||||
pub fn init() -> World {
|
||||
World {
|
||||
player: Player::init(),
|
||||
@ -45,75 +47,58 @@ impl World {
|
||||
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) {
|
||||
match event {
|
||||
Event::KeyDown {
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
keycode: Some(Keycode::Right) | Some(Keycode::D),
|
||||
..
|
||||
} => {
|
||||
self.move_right();
|
||||
}
|
||||
Event::KeyUp { .. } => {
|
||||
self.stop_player();
|
||||
self.player.move_right();
|
||||
if self.collides_with_stop() || !self.player.within_rect(&Self::playable_rect()) {
|
||||
self.player.move_left();
|
||||
self.player.face_right();
|
||||
}
|
||||
}
|
||||
Event::KeyUp { .. } => self.player.stop(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn move_up(&mut self) {
|
||||
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()
|
||||
}
|
||||
|
||||
/// Updates box areas to provide new boxes and remove items after some time
|
||||
pub fn update_box_areas(&mut self) {
|
||||
World::update_box_area(&mut self.right_top_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);
|
||||
}
|
||||
|
||||
/// Handles both, collisions with lounge and any box area
|
||||
pub fn handle_collisions(&mut self) {
|
||||
self.handle_lounge_collisions();
|
||||
self.handle_boxarea_collisions();
|
||||
}
|
||||
|
||||
/// Renders world using given canvas, texture and font
|
||||
pub fn render(&self, canvas: &mut WindowCanvas, texture: &Texture, font: &Font) {
|
||||
canvas.clear();
|
||||
|
||||
@ -299,6 +286,7 @@ struct BoxArea {
|
||||
}
|
||||
|
||||
impl BoxArea {
|
||||
/// Creates a new BoxArea
|
||||
fn new(position: BoxAreaPosition, content: BoxAreaContent) -> BoxArea {
|
||||
BoxArea {
|
||||
position,
|
||||
@ -329,10 +317,12 @@ impl BoxArea {
|
||||
Rect::new(x_offset, y_offset, 110, 110)
|
||||
}
|
||||
|
||||
/// Checks if player collides with this BoxSrea
|
||||
fn collides_with(&self, player: &Player) -> bool {
|
||||
self.bounding_rect().contains_point(player.center())
|
||||
}
|
||||
|
||||
/// Renders BoxArea using goven Canvas and Texture
|
||||
fn render(&self, canvas: &mut WindowCanvas, texture: &Texture) {
|
||||
let x_offset = self.bounding_rect().x();
|
||||
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)]
|
||||
pub enum BoxAreaPosition {
|
||||
RightTop,
|
||||
@ -374,6 +366,7 @@ pub enum BoxAreaPosition {
|
||||
LeftTop,
|
||||
}
|
||||
|
||||
/// Content of a BoxArea
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum BoxAreaContent {
|
||||
Nothing,
|
||||
@ -384,6 +377,7 @@ pub enum BoxAreaContent {
|
||||
}
|
||||
|
||||
impl BoxAreaContent {
|
||||
/// Selects new random BoxAreaContent
|
||||
fn random() -> BoxAreaContent {
|
||||
match rand::random::<i32>() % 5 {
|
||||
1 | 4 => BoxAreaContent::EmptyGlass,
|
||||
|
Loading…
x
Reference in New Issue
Block a user