From 02282901fa2162d9927d651336ada88fe9b33373 Mon Sep 17 00:00:00 2001 From: Manuel Friedli Date: Mon, 2 Jan 2023 04:37:21 +0100 Subject: [PATCH] Feature: Add skeleton for the tank. --- src/main.rs | 17 +++------- src/{movable.rs => movable/mod.rs} | 3 ++ src/movable/tank.rs | 54 ++++++++++++++++++++++++++++++ src/{ => movable}/ufo.rs | 39 ++++++++++----------- src/tank.rs | 25 -------------- src/terminal.rs | 10 +++++- 6 files changed, 88 insertions(+), 60 deletions(-) rename src/{movable.rs => movable/mod.rs} (85%) create mode 100644 src/movable/tank.rs rename src/{ => movable}/ufo.rs (70%) delete mode 100644 src/tank.rs diff --git a/src/main.rs b/src/main.rs index 0d4ef8a..9d67103 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,23 +5,13 @@ use rand::Rng; use crate::Direction::Left; use crate::movable::Movable; -use crate::ufo::Ufo; +use crate::movable::tank::Tank; +use crate::movable::ufo::Ufo; -mod movable; mod terminal; -mod ufo; -mod tank; +mod movable; -const WIDTH: u16 = 160; -const HEIGHT: u16 = 40; -const HEADER_ROW: u16 = 0; -const FOOTER_ROW: u16 = HEIGHT - 1; -const TANK_ROW: u16 = FOOTER_ROW - 1; -const MIN_UFO_ROW: u16 = 1; -const MAX_UFO_ROW: u16 = TANK_ROW - 5; const DELAY: Duration = Duration::from_millis(10); -const UFO_STR: &str = "<=000=>"; -const TANK_STR: &str = "⊆≡≣🠭≣≡⊇"; const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -29,6 +19,7 @@ fn main() { let terminal = terminal::setup(); const MAX_UFOS: u8 = 100; + let mut tank = Tank::create(); let mut ufos = vec![Ufo::create(), Ufo::create(), Ufo::create(), Ufo::create(), Ufo::create()]; let mut n_ufos = ufos.len() as u8; loop { diff --git a/src/movable.rs b/src/movable/mod.rs similarity index 85% rename from src/movable.rs rename to src/movable/mod.rs index 465a845..5678004 100644 --- a/src/movable.rs +++ b/src/movable/mod.rs @@ -1,5 +1,8 @@ use crate::Direction; +pub mod ufo; +pub mod tank; + pub trait Movable { fn create() -> T; fn mov(&mut self, direction: Direction); diff --git a/src/movable/tank.rs b/src/movable/tank.rs new file mode 100644 index 0000000..c0549e4 --- /dev/null +++ b/src/movable/tank.rs @@ -0,0 +1,54 @@ +use Direction::{Left, Right}; + +use crate::Direction; +use crate::movable::Movable; +use crate::terminal::{clear_pos, print_str_at, TANK_ROW, WIDTH}; + +const TANK_STR: &str = "⊆≡≣🠭≣≡⊇"; + +pub struct Tank { + column: u16, +} + +impl Movable for Tank { + fn create() -> Tank { + let column = (WIDTH - (TANK_STR.len() as u16)) / 2; + let tank = Tank { column }; + tank.draw(); + tank + } + + fn mov(&mut self, direction: Direction) { + clear(self, &direction); + match direction { + Left => { todo!("Implement moving left") } + Right => { todo!("Implement moving right") } + _ => {} + } + } + + fn draw(&self) { + print_str_at(self.column, TANK_ROW, TANK_STR); + } + + fn is_on_screen(&self) -> bool { + todo!("Implement tank visibility check") + } +} + +fn clear(tank: &Tank, direction: &Direction) { + match direction { + Left => { + let right_col = tank.column + (TANK_STR.len() as u16) - 1; + if right_col >= TANK_STR.len() as u16 && right_col < WIDTH { + clear_pos(right_col, TANK_ROW); + } + } + Right => { + if tank.column < WIDTH - (TANK_STR.len() as u16) { + clear_pos(tank.column, TANK_ROW); + } + } + _ => {} + } +} diff --git a/src/ufo.rs b/src/movable/ufo.rs similarity index 70% rename from src/ufo.rs rename to src/movable/ufo.rs index df1f02a..8435a20 100644 --- a/src/ufo.rs +++ b/src/movable/ufo.rs @@ -2,10 +2,12 @@ use std::cmp::{max, min}; use rand::Rng; -use crate::{Direction, MAX_UFO_ROW, MIN_UFO_ROW, UFO_STR, WIDTH}; -use crate::Direction::{Down, Left, Right}; +use crate::Direction; +use crate::Direction::{Left, Right}; use crate::movable::Movable; -use crate::terminal::{clear_pos, print_str_at}; +use crate::terminal::{clear_pos, MAX_UFO_ROW, MIN_UFO_ROW, print_str_at, WIDTH}; + +const UFO_STR: &str = "<=000=>"; pub struct Ufo { row: u16, @@ -31,27 +33,22 @@ impl Movable for Ufo { ufo } - fn mov(&mut self, direction: Direction) { + fn mov(&mut self, _direction: Direction) { clear(self); - match direction { - Down => { todo!("Implement crash movement (down + direction)") } - _ => { - match self.direction { - Left => { - let ufo_str_len = UFO_STR.len() as i16; - if self.column >= -ufo_str_len { - self.column -= 1; - } - } - Right => { - if self.column <= WIDTH as i16 { - self.column += 1; - } - } - _ => { /* Ignore, this can't be the case. */ } - }; + match self.direction { + Left => { + let ufo_str_len = UFO_STR.len() as i16; + if self.column >= -ufo_str_len { + self.column -= 1; + } } + Right => { + if self.column <= WIDTH as i16 { + self.column += 1; + } + } + _ => { /* Ignore, this can't be the case. */ } }; self.draw(); diff --git a/src/tank.rs b/src/tank.rs deleted file mode 100644 index 7a3202f..0000000 --- a/src/tank.rs +++ /dev/null @@ -1,25 +0,0 @@ -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 f0710a6..74b8456 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -5,7 +5,15 @@ 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, VERSION, WIDTH}; +use crate::VERSION; + +pub const WIDTH: u16 = 160; +pub const HEIGHT: u16 = 40; +const HEADER_ROW: u16 = 0; +const FOOTER_ROW: u16 = HEIGHT - 1; +pub const TANK_ROW: u16 = FOOTER_ROW - 1; +pub const MIN_UFO_ROW: u16 = 1; +pub const MAX_UFO_ROW: u16 = TANK_ROW - 5; pub struct Terminal { cols: u16,