From 2d19a53586ad6a9db06077da6d266a905db90dbc Mon Sep 17 00:00:00 2001 From: Manuel Friedli Date: Mon, 2 Jan 2023 00:42:06 +0100 Subject: [PATCH] Chore: Refactoring, clean up. --- src/main.rs | 29 +++++++------------------ src/movable.rs | 8 +++++++ src/tank.rs | 25 ++++++++++++++++++++++ src/terminal.rs | 8 +++---- src/{movables.rs => ufo.rs} | 42 ++++++------------------------------- 5 files changed, 50 insertions(+), 62 deletions(-) create mode 100644 src/movable.rs create mode 100644 src/tank.rs rename src/{movables.rs => ufo.rs} (81%) diff --git a/src/main.rs b/src/main.rs index b92c0bb..6ad1c8f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,11 +3,14 @@ use std::time::Duration; use rand::Rng; -use crate::Direction::{Down, Left, Right, Up}; -use crate::movables::{Movable, Ufo}; +use ufo::Ufo; -mod movables; +use crate::movable::Movable; + +mod movable; mod terminal; +mod ufo; +mod tank; const WIDTH: u16 = 160; const HEIGHT: u16 = 40; @@ -20,6 +23,7 @@ const DELAY: Duration = Duration::from_millis(10); const UFO_STR: &str = "<=000=>"; const TANK_STR: &str = "⊆≡≣🠭≣≡⊇"; +const VERSION: &str = env!("CARGO_PKG_VERSION"); fn main() { let terminal = terminal::setup(); @@ -42,14 +46,6 @@ fn main() { } 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); } @@ -64,14 +60,3 @@ pub enum Direction { Up, Down, } - -impl Direction { - fn invert(&self) -> Direction { - match self { - Left => Right, - Right => Left, - Up => Down, - Down => Up - } - } -} diff --git a/src/movable.rs b/src/movable.rs new file mode 100644 index 0000000..465a845 --- /dev/null +++ b/src/movable.rs @@ -0,0 +1,8 @@ +use crate::Direction; + +pub trait Movable { + fn create() -> T; + fn mov(&mut self, direction: Direction); + fn draw(&self); + fn is_on_screen(&self) -> bool; +} diff --git a/src/tank.rs b/src/tank.rs new file mode 100644 index 0000000..7a3202f --- /dev/null +++ b/src/tank.rs @@ -0,0 +1,25 @@ +use crate::movable::Movable; +use crate::{Direction, TANK_STR, WIDTH}; + +pub struct Tank { + column: u16, +} + +impl Movable 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") + } +} diff --git a/src/terminal.rs b/src/terminal.rs index 6984e92..f0710a6 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -5,7 +5,7 @@ use crossterm::cursor::{Hide, MoveTo, RestorePosition, SavePosition, Show}; use crossterm::style::Print; 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 { cols: u16, @@ -42,7 +42,7 @@ pub fn restore(terminal: Terminal) { .expect("Failed to tear down terminal screen"); } -pub fn write_header() { +fn write_header() { queue!( stdout(), MoveTo(0,HEADER_ROW), @@ -51,11 +51,11 @@ pub fn write_header() { stdout().flush().expect("Failed to write to terminal"); } -pub fn write_footer() { +fn write_footer() { queue!( stdout(), 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"); stdout().flush().expect("Failed to write to terminal"); } diff --git a/src/movables.rs b/src/ufo.rs similarity index 81% rename from src/movables.rs rename to src/ufo.rs index d54ae12..bc10ad1 100644 --- a/src/movables.rs +++ b/src/ufo.rs @@ -1,14 +1,13 @@ use rand::Rng; - -use crate::{Direction, MAX_UFO_ROW, MIN_UFO_ROW, TANK_STR, UFO_STR, WIDTH}; +use crate::{Direction, MAX_UFO_ROW, MIN_UFO_ROW, UFO_STR, WIDTH}; use crate::Direction::{Down, Left, Right}; +use crate::movable::Movable; use crate::terminal::print_str_at; -pub trait Movable { - fn create() -> T; - fn mov(&mut self, direction: Direction); - fn draw(&self); - fn is_on_screen(&self) -> bool; +pub struct Ufo { + row: u16, + column: i16, + direction: Direction, } impl Movable for Ufo { @@ -85,32 +84,3 @@ impl Movable for Ufo { self.column > -(UFO_STR.len() as i16) && self.column < WIDTH as i16 } } - -impl Movable 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, -}