Chore: Refactoring, clean up.

This commit is contained in:
Manuel Friedli 2023-01-02 00:42:06 +01:00
parent ea6093bd0a
commit 2d19a53586
Signed by: manuel
GPG key ID: 41D08ABA75634DA1
5 changed files with 50 additions and 62 deletions

View file

@ -3,11 +3,14 @@ use std::time::Duration;
use rand::Rng; use rand::Rng;
use crate::Direction::{Down, Left, Right, Up}; use ufo::Ufo;
use crate::movables::{Movable, Ufo};
mod movables; use crate::movable::Movable;
mod movable;
mod terminal; mod terminal;
mod ufo;
mod tank;
const WIDTH: u16 = 160; const WIDTH: u16 = 160;
const HEIGHT: u16 = 40; const HEIGHT: u16 = 40;
@ -20,6 +23,7 @@ const DELAY: Duration = Duration::from_millis(10);
const UFO_STR: &str = "<=000=>"; const UFO_STR: &str = "<=000=>";
const TANK_STR: &str = "⊆≡≣🠭≣≡⊇"; const TANK_STR: &str = "⊆≡≣🠭≣≡⊇";
const VERSION: &str = env!("CARGO_PKG_VERSION");
fn main() { fn main() {
let terminal = terminal::setup(); let terminal = terminal::setup();
@ -42,14 +46,6 @@ fn main() {
} }
thread::sleep(DELAY); thread::sleep(DELAY);
} }
// for _ in 0..N_UFOS {
// let mut ufo = Ufo::create();
// while ufo.is_on_screen() {
// ufo.mov(Direction::Left);
// ufo.draw();
// thread::sleep(DELAY);
// }
// }
terminal::restore(terminal); terminal::restore(terminal);
} }
@ -64,14 +60,3 @@ pub enum Direction {
Up, Up,
Down, Down,
} }
impl Direction {
fn invert(&self) -> Direction {
match self {
Left => Right,
Right => Left,
Up => Down,
Down => Up
}
}
}

8
src/movable.rs Normal file
View file

@ -0,0 +1,8 @@
use crate::Direction;
pub trait Movable<T> {
fn create() -> T;
fn mov(&mut self, direction: Direction);
fn draw(&self);
fn is_on_screen(&self) -> bool;
}

25
src/tank.rs Normal file
View file

@ -0,0 +1,25 @@
use crate::movable::Movable;
use crate::{Direction, TANK_STR, WIDTH};
pub struct Tank {
column: u16,
}
impl Movable<Tank> for Tank {
fn create() -> Tank {
let column = (WIDTH - TANK_STR.len() as u16) / 2;
Tank { column }
}
fn mov(&mut self, direction: Direction) {
todo!("Implement tank movement")
}
fn draw(&self) {
todo!("Implement drawing tank")
}
fn is_on_screen(&self) -> bool {
todo!("Implement tank visibility check")
}
}

View file

@ -5,7 +5,7 @@ use crossterm::cursor::{Hide, MoveTo, RestorePosition, SavePosition, Show};
use crossterm::style::Print; use crossterm::style::Print;
use crossterm::terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen, SetSize, size}; use crossterm::terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen, SetSize, size};
use crate::{FOOTER_ROW, HEADER_ROW, HEIGHT, WIDTH}; use crate::{FOOTER_ROW, HEADER_ROW, HEIGHT, VERSION, WIDTH};
pub struct Terminal { pub struct Terminal {
cols: u16, cols: u16,
@ -42,7 +42,7 @@ pub fn restore(terminal: Terminal) {
.expect("Failed to tear down terminal screen"); .expect("Failed to tear down terminal screen");
} }
pub fn write_header() { fn write_header() {
queue!( queue!(
stdout(), stdout(),
MoveTo(0,HEADER_ROW), MoveTo(0,HEADER_ROW),
@ -51,11 +51,11 @@ pub fn write_header() {
stdout().flush().expect("Failed to write to terminal"); stdout().flush().expect("Failed to write to terminal");
} }
pub fn write_footer() { fn write_footer() {
queue!( queue!(
stdout(), stdout(),
MoveTo(0,FOOTER_ROW), MoveTo(0,FOOTER_ROW),
Print(center_text("v0.0.0-dev", None)) Print(center_text(format!("v{}",VERSION).as_str(), None))
).expect("Failed to write to terminal"); ).expect("Failed to write to terminal");
stdout().flush().expect("Failed to write to terminal"); stdout().flush().expect("Failed to write to terminal");
} }

View file

@ -1,14 +1,13 @@
use rand::Rng; use rand::Rng;
use crate::{Direction, MAX_UFO_ROW, MIN_UFO_ROW, UFO_STR, WIDTH};
use crate::{Direction, MAX_UFO_ROW, MIN_UFO_ROW, TANK_STR, UFO_STR, WIDTH};
use crate::Direction::{Down, Left, Right}; use crate::Direction::{Down, Left, Right};
use crate::movable::Movable;
use crate::terminal::print_str_at; use crate::terminal::print_str_at;
pub trait Movable<T> { pub struct Ufo {
fn create() -> T; row: u16,
fn mov(&mut self, direction: Direction); column: i16,
fn draw(&self); direction: Direction,
fn is_on_screen(&self) -> bool;
} }
impl Movable<Ufo> for Ufo { impl Movable<Ufo> for Ufo {
@ -85,32 +84,3 @@ impl Movable<Ufo> for Ufo {
self.column > -(UFO_STR.len() as i16) && self.column < WIDTH as i16 self.column > -(UFO_STR.len() as i16) && self.column < WIDTH as i16
} }
} }
impl Movable<Tank> for Tank {
fn create() -> Tank {
let column = (WIDTH - TANK_STR.len() as u16) / 2;
Tank { column }
}
fn mov(&mut self, direction: Direction) {
todo!("Implement tank movement")
}
fn draw(&self) {
todo!("Implement drawing tank")
}
fn is_on_screen(&self) -> bool {
todo!("Implement tank visibility check")
}
}
pub struct Ufo {
row: u16,
column: i16,
direction: Direction,
}
pub struct Tank {
column: u16,
}