Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ pub mod events {
use crate::world::world::{World, PlayerStatus, Bullet};

use crossterm::{
event::{poll, read, Event, KeyCode},
cursor::MoveTo,
style::Print,
QueueableCommand
cursor::MoveTo, event::{self, poll, read, Event, KeyCode, KeyEvent, KeyModifiers}, style::Print, QueueableCommand
};

use std::{
io::{Stdout, Write},
time::Duration,
};

pub fn handle_pressed_keys(world: &mut World) {
if poll(Duration::from_millis(10)).unwrap() {
let key = read().unwrap();
Expand All @@ -23,6 +19,30 @@ pub mod events {
}

match key {
Event::Key(KeyEvent{
modifiers:KeyModifiers::CONTROL,
code:KeyCode::Left,
..})=>{if world.status == PlayerStatus::Alive && world.player_location.c > 1 {
world.player_location.c -= 4
}}
Event::Key(KeyEvent{
modifiers:KeyModifiers::CONTROL,
code:KeyCode::Right,
..})=>{if world.status == PlayerStatus::Alive && world.player_location.c > 1 {
world.player_location.c += 4
}}
Event::Key(KeyEvent{
modifiers:KeyModifiers::CONTROL,
code:KeyCode::Up,
..})=>{if world.status == PlayerStatus::Alive && world.player_location.l > 1 {
world.player_location.l -= 4
}}
Event::Key(KeyEvent{
modifiers:KeyModifiers::CONTROL,
code:KeyCode::Down,
..})=>{if world.status == PlayerStatus::Alive && world.player_location.l <world.maxl-4 {
world.player_location.l += 4
}}
Event::Key(event) => {
// I'm reading from keyboard into event
match event.code {
Expand Down
171 changes: 86 additions & 85 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,86 +1,87 @@
use rand:: thread_rng;
use std::io::stdout;
use std::{thread, time};

use crossterm::{
cursor::{Hide, Show},
terminal::{disable_raw_mode, enable_raw_mode, size, Clear},
ExecutableCommand, QueueableCommand,
};

mod physics;
mod world;
mod greeting;
mod events;

use world::world::{*};
use physics::physics::{*};
use greeting::greeting::{*};
use events::events::{*};


/// Game Physic Rules
/// TODO: Move to Physics.rs module later
fn physics(world: &mut World) {
let mut rng = thread_rng();

// check if player hit the ground
check_player_status(world);

// check enemy hit something
check_enemy_status(world);
check_fuel_status(world);

// move the map Downward
update_map(&mut rng, world);

// create new enemy
create_enemy(&mut rng, world);
create_fuel(&mut rng, world);

// Move elements along map movements
move_enemies(world);
move_fuel(world);
move_bullets(world);

if world.gas >= 1 {
world.gas -= 1;
}
}


fn main() -> std::io::Result<()> {
// init the screen
let mut sc = stdout();
let (maxc, maxl) = size().unwrap();
sc.execute(Hide)?;
enable_raw_mode()?;

// init the world
let slowness = 100;
let mut world = World::new(maxc, maxl);

// show welcoming banner
welcome_screen(&sc, &world);

while world.status == PlayerStatus::Alive || world.status == PlayerStatus::Paused {
handle_pressed_keys(&mut world);
if world.status != PlayerStatus::Paused {
physics(&mut world);
world.draw(&sc)?;
} else {
pause_screen(&sc, &world);
}
thread::sleep(time::Duration::from_millis(slowness));
}



// game is finished
sc.queue(Clear(crossterm::terminal::ClearType::All))?;
goodbye_screen(&sc, &world);
sc.queue(Clear(crossterm::terminal::ClearType::All))?
.execute(Show)?;
disable_raw_mode()?;
Ok(())
use rand:: thread_rng;
use std::io::stdout;
use std::{thread, time};

use crossterm::{
cursor::{Hide, Show},
terminal::{EnterAlternateScreen,LeaveAlternateScreen,disable_raw_mode, enable_raw_mode, size, Clear},
ExecutableCommand, QueueableCommand,
};

mod physics;
mod world;
mod greeting;
mod events;

use world::world::{*};
use physics::physics::{*};
use greeting::greeting::{*};
use events::events::{*};


/// Game Physic Rules
/// TODO: Move to Physics.rs module later
fn physics(world: &mut World) {
let mut rng = thread_rng();

// check if player hit the ground
check_player_status(world);

// check enemy hit something
check_enemy_status(world);
check_fuel_status(world);

// move the map Downward
update_map(&mut rng, world);

// create new enemy
create_enemy(&mut rng, world);
create_fuel(&mut rng, world);

// Move elements along map movements
move_enemies(world);
move_fuel(world);
move_bullets(world);

if world.gas >= 1 {
world.gas -= 1;
}
}


fn main() -> std::io::Result<()> {
// init the screen
let mut sc = stdout();
sc.execute(EnterAlternateScreen)?;
let (maxc, maxl) = size().unwrap();
sc.execute(Hide)?;
enable_raw_mode()?;

// init the world
let slowness = 100;
let mut world = World::new(maxc, maxl);

// show welcoming banner
welcome_screen(&sc, &world);

while world.status == PlayerStatus::Alive || world.status == PlayerStatus::Paused {
handle_pressed_keys(&mut world);
if world.status != PlayerStatus::Paused {
physics(&mut world);
world.draw(&sc)?;
} else {
pause_screen(&sc, &world);
}
thread::sleep(time::Duration::from_millis(slowness));
}


// game is finished
sc.queue(Clear(crossterm::terminal::ClearType::All))?;
goodbye_screen(&sc, &world);
sc.queue(Clear(crossterm::terminal::ClearType::All))?
.execute(Show)?;
sc.execute(LeaveAlternateScreen)?;
disable_raw_mode()?;
Ok(())
}