mirror of
https://github.com/pcvolkmer/winelounge.git
synced 2025-04-19 18:36:50 +00:00
Code cleanup
This commit is contained in:
parent
efd0b529d0
commit
9489535601
12
src/main.rs
12
src/main.rs
@ -3,15 +3,12 @@ mod world;
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::player::player::Player;
|
||||
use crate::world::world::{BoxAreaContent, BoxAreaPosition, World};
|
||||
use crate::player::Player;
|
||||
use crate::world::{BoxAreaContent, BoxAreaPosition, World};
|
||||
use sdl2::event::Event;
|
||||
use sdl2::image::LoadTexture;
|
||||
use sdl2::keyboard::Keycode;
|
||||
use sdl2::pixels::Color;
|
||||
use sdl2::rect::{Point, Rect};
|
||||
use sdl2::render::{Texture, WindowCanvas};
|
||||
use sdl2::ttf::Font;
|
||||
|
||||
const GLASS_SPACE: u8 = 5;
|
||||
|
||||
@ -94,8 +91,7 @@ fn main() {
|
||||
None => Option::None,
|
||||
};
|
||||
|
||||
match colliding_box_area {
|
||||
Some(ba) => {
|
||||
if let Some(ba) = colliding_box_area {
|
||||
let content = match &ba.content {
|
||||
BoxAreaContent::HiddenBox => BoxAreaContent::random(),
|
||||
BoxAreaContent::EmptyGlass => BoxAreaContent::EmptyGlass,
|
||||
@ -116,8 +112,6 @@ fn main() {
|
||||
ba.update_content(BoxAreaContent::FilledBottle);
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
if chrono::Utc::now().timestamp_millis() % 1000 > 950 {
|
||||
world.update_box_areas();
|
||||
|
@ -1,33 +1,32 @@
|
||||
pub mod player {
|
||||
use crate::GLASS_SPACE;
|
||||
use sdl2::rect::{Point, Rect};
|
||||
use crate::GLASS_SPACE;
|
||||
use sdl2::rect::{Point, Rect};
|
||||
|
||||
pub struct Player {
|
||||
pub struct Player {
|
||||
pub position: Point,
|
||||
direction: PlayerDirection,
|
||||
footstep: u8,
|
||||
pub empty_glasses: u8,
|
||||
pub filled_glasses: u8,
|
||||
pub points: u32,
|
||||
}
|
||||
}
|
||||
|
||||
enum PlayerDirection {
|
||||
UP,
|
||||
DOWN,
|
||||
LEFT,
|
||||
RIGHT,
|
||||
}
|
||||
enum PlayerDirection {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
impl Player {
|
||||
impl Player {
|
||||
pub fn init() -> Player {
|
||||
return Player {
|
||||
Player {
|
||||
position: Point::new(380, 250),
|
||||
direction: PlayerDirection::DOWN,
|
||||
direction: PlayerDirection::Down,
|
||||
footstep: 0,
|
||||
empty_glasses: 0,
|
||||
filled_glasses: 0,
|
||||
points: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn can_pick_glass(&self) -> bool {
|
||||
@ -35,8 +34,8 @@ pub mod player {
|
||||
}
|
||||
|
||||
pub fn pick_glass(&mut self) {
|
||||
self.empty_glasses = self.empty_glasses + 1;
|
||||
self.points = self.points + 2
|
||||
self.empty_glasses += 1;
|
||||
self.points += 2
|
||||
}
|
||||
|
||||
pub fn can_fill_glass(&self) -> bool {
|
||||
@ -44,9 +43,9 @@ pub mod player {
|
||||
}
|
||||
|
||||
pub fn fill_glass(&mut self) {
|
||||
self.empty_glasses = self.empty_glasses - 1;
|
||||
self.filled_glasses = self.filled_glasses + 1;
|
||||
self.points = self.points + 3
|
||||
self.empty_glasses -= 1;
|
||||
self.filled_glasses += 1;
|
||||
self.points += 3
|
||||
}
|
||||
|
||||
pub fn can_drink_glass(&self) -> bool {
|
||||
@ -54,8 +53,8 @@ pub mod player {
|
||||
}
|
||||
|
||||
pub fn drink_glass(&mut self) {
|
||||
self.filled_glasses = self.filled_glasses - 1;
|
||||
self.points = self.points + 5
|
||||
self.filled_glasses -= 1;
|
||||
self.points += 5
|
||||
}
|
||||
|
||||
pub fn center(&self) -> Point {
|
||||
@ -74,55 +73,54 @@ pub mod player {
|
||||
};
|
||||
|
||||
match self.direction {
|
||||
PlayerDirection::DOWN => Rect::new(x, 5, 40, 115),
|
||||
PlayerDirection::LEFT => Rect::new(x, 255, 40, 115),
|
||||
PlayerDirection::RIGHT => Rect::new(x, 130, 40, 115),
|
||||
PlayerDirection::UP => Rect::new(x, 380, 40, 115),
|
||||
PlayerDirection::Down => Rect::new(x, 5, 40, 115),
|
||||
PlayerDirection::Left => Rect::new(x, 255, 40, 115),
|
||||
PlayerDirection::Right => Rect::new(x, 130, 40, 115),
|
||||
PlayerDirection::Up => Rect::new(x, 380, 40, 115),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn face_up(&mut self) {
|
||||
self.direction = PlayerDirection::UP;
|
||||
self.direction = PlayerDirection::Up;
|
||||
}
|
||||
|
||||
pub fn face_down(&mut self) {
|
||||
self.direction = PlayerDirection::DOWN;
|
||||
self.direction = PlayerDirection::Down;
|
||||
}
|
||||
|
||||
pub fn face_left(&mut self) {
|
||||
self.direction = PlayerDirection::LEFT;
|
||||
self.direction = PlayerDirection::Left;
|
||||
}
|
||||
|
||||
pub fn face_right(&mut self) {
|
||||
self.direction = PlayerDirection::RIGHT;
|
||||
self.direction = PlayerDirection::Right;
|
||||
}
|
||||
|
||||
pub fn move_up(&mut self) {
|
||||
self.face_up();
|
||||
self.footstep = &self.footstep % 2 + 1;
|
||||
self.position.y = self.position.y - 15
|
||||
self.position.y -= 15
|
||||
}
|
||||
|
||||
pub fn move_down(&mut self) {
|
||||
self.face_down();
|
||||
self.footstep = &self.footstep % 2 + 1;
|
||||
self.position.y = self.position.y + 15
|
||||
self.position.y += 15
|
||||
}
|
||||
|
||||
pub fn move_left(&mut self) {
|
||||
self.face_left();
|
||||
self.footstep = &self.footstep % 2 + 1;
|
||||
self.position.x = self.position.x - 15
|
||||
self.position.x -= 15
|
||||
}
|
||||
|
||||
pub fn move_right(&mut self) {
|
||||
self.face_right();
|
||||
self.footstep = &self.footstep % 2 + 1;
|
||||
self.position.x = self.position.x + 15
|
||||
self.position.x += 15
|
||||
}
|
||||
|
||||
pub fn stop(&mut self) {
|
||||
self.footstep = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
58
src/world.rs
58
src/world.rs
@ -1,27 +1,23 @@
|
||||
pub mod world {
|
||||
use crate::{Player, GLASS_SPACE};
|
||||
use sdl2::pixels::Color;
|
||||
use sdl2::rect::{Point, Rect};
|
||||
use sdl2::render::{Texture, WindowCanvas};
|
||||
use sdl2::ttf::Font;
|
||||
use crate::{Player, GLASS_SPACE};
|
||||
use sdl2::pixels::Color;
|
||||
use sdl2::rect::{Point, Rect};
|
||||
use sdl2::render::{Texture, WindowCanvas};
|
||||
use sdl2::ttf::Font;
|
||||
|
||||
pub struct World {
|
||||
pub struct World {
|
||||
pub player: Player,
|
||||
pub right_top_box_area: BoxArea,
|
||||
pub right_bottom_box_area: BoxArea,
|
||||
pub left_bottom_box_area: BoxArea,
|
||||
pub left_top_box_area: BoxArea,
|
||||
stops: Vec<Point>,
|
||||
}
|
||||
}
|
||||
|
||||
impl World {
|
||||
impl World {
|
||||
pub fn init() -> World {
|
||||
World {
|
||||
player: Player::init(),
|
||||
right_top_box_area: BoxArea::new(
|
||||
BoxAreaPosition::RightTop,
|
||||
BoxAreaContent::EmptyGlass,
|
||||
),
|
||||
right_top_box_area: BoxArea::new(BoxAreaPosition::RightTop, BoxAreaContent::EmptyGlass),
|
||||
right_bottom_box_area: BoxArea::new(
|
||||
BoxAreaPosition::RightBottom,
|
||||
BoxAreaContent::HiddenBox,
|
||||
@ -68,7 +64,7 @@ pub mod world {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
false
|
||||
}
|
||||
|
||||
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 {
|
||||
box_area.content = BoxAreaContent::HiddenBox;
|
||||
box_area.last_update = now;
|
||||
} else if box_area.content != BoxAreaContent::Nothing
|
||||
&& box_area.last_update + 30 < now - r
|
||||
} else if box_area.content != BoxAreaContent::Nothing && box_area.last_update + 30 < now - r
|
||||
{
|
||||
box_area.content = BoxAreaContent::Nothing;
|
||||
box_area.last_update = now;
|
||||
@ -244,22 +239,22 @@ pub mod world {
|
||||
|
||||
canvas.present();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BoxArea {
|
||||
#[derive(Debug)]
|
||||
pub struct BoxArea {
|
||||
position: BoxAreaPosition,
|
||||
pub content: BoxAreaContent,
|
||||
last_update: i64,
|
||||
}
|
||||
}
|
||||
|
||||
impl BoxArea {
|
||||
impl BoxArea {
|
||||
fn new(position: BoxAreaPosition, content: BoxAreaContent) -> BoxArea {
|
||||
return BoxArea {
|
||||
BoxArea {
|
||||
position,
|
||||
content,
|
||||
last_update: chrono::Utc::now().timestamp(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_content(&mut self, content: BoxAreaContent) {
|
||||
@ -360,26 +355,26 @@ pub mod world {
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum BoxAreaPosition {
|
||||
#[derive(Debug)]
|
||||
pub enum BoxAreaPosition {
|
||||
RightTop,
|
||||
RightBottom,
|
||||
LeftBottom,
|
||||
LeftTop,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum BoxAreaContent {
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum BoxAreaContent {
|
||||
Nothing,
|
||||
HiddenBox,
|
||||
EmptyGlass,
|
||||
FilledBottle,
|
||||
EmptyBottle,
|
||||
}
|
||||
}
|
||||
|
||||
impl BoxAreaContent {
|
||||
impl BoxAreaContent {
|
||||
pub fn random() -> BoxAreaContent {
|
||||
match rand::random::<i32>() % 5 {
|
||||
1 | 4 => BoxAreaContent::EmptyGlass,
|
||||
@ -387,5 +382,4 @@ pub mod world {
|
||||
_ => BoxAreaContent::Nothing,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user