Code cleanup

This commit is contained in:
Paul-Christian Volkmer 2022-02-17 17:28:04 +01:00
parent efd0b529d0
commit 9489535601
3 changed files with 457 additions and 471 deletions

View File

@ -3,15 +3,12 @@ mod world;
use std::time::Duration; use std::time::Duration;
use crate::player::player::Player; use crate::player::Player;
use crate::world::world::{BoxAreaContent, BoxAreaPosition, World}; use crate::world::{BoxAreaContent, BoxAreaPosition, World};
use sdl2::event::Event; use sdl2::event::Event;
use sdl2::image::LoadTexture; use sdl2::image::LoadTexture;
use sdl2::keyboard::Keycode; use sdl2::keyboard::Keycode;
use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::rect::{Point, Rect};
use sdl2::render::{Texture, WindowCanvas};
use sdl2::ttf::Font;
const GLASS_SPACE: u8 = 5; const GLASS_SPACE: u8 = 5;
@ -94,8 +91,7 @@ fn main() {
None => Option::None, None => Option::None,
}; };
match colliding_box_area { if let Some(ba) = colliding_box_area {
Some(ba) => {
let content = match &ba.content { let content = match &ba.content {
BoxAreaContent::HiddenBox => BoxAreaContent::random(), BoxAreaContent::HiddenBox => BoxAreaContent::random(),
BoxAreaContent::EmptyGlass => BoxAreaContent::EmptyGlass, BoxAreaContent::EmptyGlass => BoxAreaContent::EmptyGlass,
@ -116,8 +112,6 @@ fn main() {
ba.update_content(BoxAreaContent::FilledBottle); ba.update_content(BoxAreaContent::FilledBottle);
} }
} }
None => {}
}
if chrono::Utc::now().timestamp_millis() % 1000 > 950 { if chrono::Utc::now().timestamp_millis() % 1000 > 950 {
world.update_box_areas(); world.update_box_areas();

View File

@ -1,4 +1,3 @@
pub mod player {
use crate::GLASS_SPACE; use crate::GLASS_SPACE;
use sdl2::rect::{Point, Rect}; use sdl2::rect::{Point, Rect};
@ -12,22 +11,22 @@ pub mod player {
} }
enum PlayerDirection { enum PlayerDirection {
UP, Up,
DOWN, Down,
LEFT, Left,
RIGHT, Right,
} }
impl Player { impl Player {
pub fn init() -> Player { pub fn init() -> Player {
return Player { Player {
position: Point::new(380, 250), position: Point::new(380, 250),
direction: PlayerDirection::DOWN, direction: PlayerDirection::Down,
footstep: 0, footstep: 0,
empty_glasses: 0, empty_glasses: 0,
filled_glasses: 0, filled_glasses: 0,
points: 0, points: 0,
}; }
} }
pub fn can_pick_glass(&self) -> bool { pub fn can_pick_glass(&self) -> bool {
@ -35,8 +34,8 @@ pub mod player {
} }
pub fn pick_glass(&mut self) { pub fn pick_glass(&mut self) {
self.empty_glasses = self.empty_glasses + 1; self.empty_glasses += 1;
self.points = self.points + 2 self.points += 2
} }
pub fn can_fill_glass(&self) -> bool { pub fn can_fill_glass(&self) -> bool {
@ -44,9 +43,9 @@ pub mod player {
} }
pub fn fill_glass(&mut self) { pub fn fill_glass(&mut self) {
self.empty_glasses = self.empty_glasses - 1; self.empty_glasses -= 1;
self.filled_glasses = self.filled_glasses + 1; self.filled_glasses += 1;
self.points = self.points + 3 self.points += 3
} }
pub fn can_drink_glass(&self) -> bool { pub fn can_drink_glass(&self) -> bool {
@ -54,8 +53,8 @@ pub mod player {
} }
pub fn drink_glass(&mut self) { pub fn drink_glass(&mut self) {
self.filled_glasses = self.filled_glasses - 1; self.filled_glasses -= 1;
self.points = self.points + 5 self.points += 5
} }
pub fn center(&self) -> Point { pub fn center(&self) -> Point {
@ -74,55 +73,54 @@ pub mod player {
}; };
match self.direction { match self.direction {
PlayerDirection::DOWN => Rect::new(x, 5, 40, 115), PlayerDirection::Down => Rect::new(x, 5, 40, 115),
PlayerDirection::LEFT => Rect::new(x, 255, 40, 115), PlayerDirection::Left => Rect::new(x, 255, 40, 115),
PlayerDirection::RIGHT => Rect::new(x, 130, 40, 115), PlayerDirection::Right => Rect::new(x, 130, 40, 115),
PlayerDirection::UP => Rect::new(x, 380, 40, 115), PlayerDirection::Up => Rect::new(x, 380, 40, 115),
} }
} }
pub fn face_up(&mut self) { pub fn face_up(&mut self) {
self.direction = PlayerDirection::UP; self.direction = PlayerDirection::Up;
} }
pub fn face_down(&mut self) { pub fn face_down(&mut self) {
self.direction = PlayerDirection::DOWN; self.direction = PlayerDirection::Down;
} }
pub fn face_left(&mut self) { pub fn face_left(&mut self) {
self.direction = PlayerDirection::LEFT; self.direction = PlayerDirection::Left;
} }
pub fn face_right(&mut self) { pub fn face_right(&mut self) {
self.direction = PlayerDirection::RIGHT; self.direction = PlayerDirection::Right;
} }
pub fn move_up(&mut self) { pub fn move_up(&mut self) {
self.face_up(); self.face_up();
self.footstep = &self.footstep % 2 + 1; self.footstep = &self.footstep % 2 + 1;
self.position.y = self.position.y - 15 self.position.y -= 15
} }
pub fn move_down(&mut self) { pub fn move_down(&mut self) {
self.face_down(); self.face_down();
self.footstep = &self.footstep % 2 + 1; self.footstep = &self.footstep % 2 + 1;
self.position.y = self.position.y + 15 self.position.y += 15
} }
pub fn move_left(&mut self) { pub fn move_left(&mut self) {
self.face_left(); self.face_left();
self.footstep = &self.footstep % 2 + 1; self.footstep = &self.footstep % 2 + 1;
self.position.x = self.position.x - 15 self.position.x -= 15
} }
pub fn move_right(&mut self) { pub fn move_right(&mut self) {
self.face_right(); self.face_right();
self.footstep = &self.footstep % 2 + 1; self.footstep = &self.footstep % 2 + 1;
self.position.x = self.position.x + 15 self.position.x += 15
} }
pub fn stop(&mut self) { pub fn stop(&mut self) {
self.footstep = 0; self.footstep = 0;
} }
} }
}

View File

@ -1,4 +1,3 @@
pub mod world {
use crate::{Player, GLASS_SPACE}; use crate::{Player, GLASS_SPACE};
use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::rect::{Point, Rect}; use sdl2::rect::{Point, Rect};
@ -18,10 +17,7 @@ pub mod world {
pub fn init() -> World { pub fn init() -> World {
World { World {
player: Player::init(), player: Player::init(),
right_top_box_area: BoxArea::new( right_top_box_area: BoxArea::new(BoxAreaPosition::RightTop, BoxAreaContent::EmptyGlass),
BoxAreaPosition::RightTop,
BoxAreaContent::EmptyGlass,
),
right_bottom_box_area: BoxArea::new( right_bottom_box_area: BoxArea::new(
BoxAreaPosition::RightBottom, BoxAreaPosition::RightBottom,
BoxAreaContent::HiddenBox, BoxAreaContent::HiddenBox,
@ -68,7 +64,7 @@ pub mod world {
return true; return true;
} }
} }
return false; false
} }
pub fn move_up(&mut self) { pub fn move_up(&mut self) {
@ -129,8 +125,7 @@ pub mod world {
if box_area.content == BoxAreaContent::Nothing && box_area.last_update + 10 < now { if box_area.content == BoxAreaContent::Nothing && box_area.last_update + 10 < now {
box_area.content = BoxAreaContent::HiddenBox; box_area.content = BoxAreaContent::HiddenBox;
box_area.last_update = now; box_area.last_update = now;
} else if box_area.content != BoxAreaContent::Nothing } else if box_area.content != BoxAreaContent::Nothing && box_area.last_update + 30 < now - r
&& box_area.last_update + 30 < now - r
{ {
box_area.content = BoxAreaContent::Nothing; box_area.content = BoxAreaContent::Nothing;
box_area.last_update = now; box_area.last_update = now;
@ -255,11 +250,11 @@ pub mod world {
impl BoxArea { impl BoxArea {
fn new(position: BoxAreaPosition, content: BoxAreaContent) -> BoxArea { fn new(position: BoxAreaPosition, content: BoxAreaContent) -> BoxArea {
return BoxArea { BoxArea {
position, position,
content, content,
last_update: chrono::Utc::now().timestamp(), last_update: chrono::Utc::now().timestamp(),
}; }
} }
pub fn update_content(&mut self, content: BoxAreaContent) { pub fn update_content(&mut self, content: BoxAreaContent) {
@ -388,4 +383,3 @@ pub mod world {
} }
} }
} }
}