mirror of
https://github.com/pcvolkmer/winelounge.git
synced 2025-04-19 18:36:50 +00:00
Code cleanup, move events moved to world
This commit is contained in:
parent
94efb64055
commit
f6dedfca72
42
src/main.rs
42
src/main.rs
@ -1,16 +1,17 @@
|
|||||||
mod player;
|
|
||||||
mod sprite;
|
|
||||||
mod world;
|
|
||||||
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use crate::player::Player;
|
|
||||||
use crate::world::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 crate::player::Player;
|
||||||
|
use crate::world::World;
|
||||||
|
|
||||||
|
mod player;
|
||||||
|
mod sprite;
|
||||||
|
mod world;
|
||||||
|
|
||||||
const GLASS_SPACE: u8 = 5;
|
const GLASS_SPACE: u8 = 5;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -49,34 +50,7 @@ fn main() {
|
|||||||
} => {
|
} => {
|
||||||
break 'running;
|
break 'running;
|
||||||
}
|
}
|
||||||
Event::KeyDown {
|
e => world.handle_event(e),
|
||||||
keycode: Some(Keycode::Up) | Some(Keycode::W),
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
world.move_up();
|
|
||||||
}
|
|
||||||
Event::KeyDown {
|
|
||||||
keycode: Some(Keycode::Down) | Some(Keycode::S),
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
world.move_down();
|
|
||||||
}
|
|
||||||
Event::KeyDown {
|
|
||||||
keycode: Some(Keycode::Left) | Some(Keycode::A),
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
world.move_left();
|
|
||||||
}
|
|
||||||
Event::KeyDown {
|
|
||||||
keycode: Some(Keycode::Right) | Some(Keycode::D),
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
world.move_right();
|
|
||||||
}
|
|
||||||
Event::KeyUp { .. } => {
|
|
||||||
world.stop_player();
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
86
src/world.rs
86
src/world.rs
@ -1,10 +1,13 @@
|
|||||||
use crate::sprite::Sprite;
|
use sdl2::event::Event;
|
||||||
use crate::{Player, GLASS_SPACE};
|
use sdl2::keyboard::Keycode;
|
||||||
use sdl2::pixels::Color;
|
use sdl2::pixels::Color;
|
||||||
use sdl2::rect::{Point, Rect};
|
use sdl2::rect::{Point, Rect};
|
||||||
use sdl2::render::{Texture, WindowCanvas};
|
use sdl2::render::{Texture, WindowCanvas};
|
||||||
use sdl2::ttf::Font;
|
use sdl2::ttf::Font;
|
||||||
|
|
||||||
|
use crate::sprite::Sprite;
|
||||||
|
use crate::{Player, GLASS_SPACE};
|
||||||
|
|
||||||
pub struct World {
|
pub struct World {
|
||||||
player: Player,
|
player: Player,
|
||||||
right_top_box_area: BoxArea,
|
right_top_box_area: BoxArea,
|
||||||
@ -42,16 +45,37 @@ impl World {
|
|||||||
Rect::new(0, 50, 800, 550)
|
Rect::new(0, 50, 800, 550)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_player_collision(&mut self) -> Collision {
|
pub fn handle_event(&mut self, event: Event) {
|
||||||
if let Some(ba) = self.collides_with_box_area() {
|
match event {
|
||||||
return Collision::BoxArea(ba);
|
Event::KeyDown {
|
||||||
} else if self.collides_with_lounge() {
|
keycode: Some(Keycode::Up) | Some(Keycode::W),
|
||||||
return Collision::Lounge;
|
..
|
||||||
} else if self.collides_with_stop() {
|
} => {
|
||||||
return Collision::Stopper;
|
self.move_up();
|
||||||
|
}
|
||||||
|
Event::KeyDown {
|
||||||
|
keycode: Some(Keycode::Down) | Some(Keycode::S),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
self.move_down();
|
||||||
|
}
|
||||||
|
Event::KeyDown {
|
||||||
|
keycode: Some(Keycode::Left) | Some(Keycode::A),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
self.move_left();
|
||||||
|
}
|
||||||
|
Event::KeyDown {
|
||||||
|
keycode: Some(Keycode::Right) | Some(Keycode::D),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
self.move_right();
|
||||||
|
}
|
||||||
|
Event::KeyUp { .. } => {
|
||||||
|
self.stop_player();
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
Collision::None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn move_up(&mut self) {
|
pub fn move_up(&mut self) {
|
||||||
@ -97,20 +121,6 @@ impl World {
|
|||||||
World::update_box_area(&mut self.left_top_box_area);
|
World::update_box_area(&mut self.left_top_box_area);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_box_area(box_area: &mut BoxArea) {
|
|
||||||
let now = chrono::Utc::now().timestamp();
|
|
||||||
let r: i64 = (rand::random::<i64>() % 10) + 3;
|
|
||||||
|
|
||||||
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
|
|
||||||
{
|
|
||||||
box_area.content = BoxAreaContent::Nothing;
|
|
||||||
box_area.last_update = now;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
||||||
@ -180,6 +190,32 @@ impl World {
|
|||||||
canvas.present();
|
canvas.present();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn update_box_area(box_area: &mut BoxArea) {
|
||||||
|
let now = chrono::Utc::now().timestamp();
|
||||||
|
let r: i64 = (rand::random::<i64>() % 10) + 3;
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
box_area.content = BoxAreaContent::Nothing;
|
||||||
|
box_area.last_update = now;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn has_player_collision(&mut self) -> Collision {
|
||||||
|
if let Some(ba) = self.collides_with_box_area() {
|
||||||
|
return Collision::BoxArea(ba);
|
||||||
|
} else if self.collides_with_lounge() {
|
||||||
|
return Collision::Lounge;
|
||||||
|
} else if self.collides_with_stop() {
|
||||||
|
return Collision::Stopper;
|
||||||
|
}
|
||||||
|
|
||||||
|
Collision::None
|
||||||
|
}
|
||||||
|
|
||||||
fn collides_with_box_area(&mut self) -> Option<BoxAreaPosition> {
|
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);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user