Feature: Add skeleton for the tank.

This commit is contained in:
Manuel Friedli 2023-01-02 04:37:21 +01:00
parent 38b2677ed1
commit 02282901fa
Signed by: manuel
GPG Key ID: 41D08ABA75634DA1
6 changed files with 88 additions and 60 deletions

View File

@ -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 {

View File

@ -1,5 +1,8 @@
use crate::Direction;
pub mod ufo;
pub mod tank;
pub trait Movable<T> {
fn create() -> T;
fn mov(&mut self, direction: Direction);

54
src/movable/tank.rs Normal file
View File

@ -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<Tank> 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);
}
}
_ => {}
}
}

View File

@ -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<Ufo> 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();

View File

@ -1,25 +0,0 @@
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,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,