diff --git a/src/app/helpers/mod.rs b/src/app/helpers/mod.rs index 1a0ee7f..54e22b5 100644 --- a/src/app/helpers/mod.rs +++ b/src/app/helpers/mod.rs @@ -1,9 +1,7 @@ pub mod block_title; mod cells_area; mod main_menu_option; -mod zoom_level; pub use block_title::{block_single_title_helper, block_title_helper}; pub use cells_area::cells_area_helper; pub use main_menu_option::main_menu_option_helper; -pub use zoom_level::{CellSizes, cell_size_helper}; diff --git a/src/app/helpers/zoom_level.rs b/src/app/helpers/zoom_level.rs deleted file mode 100644 index 97c0927..0000000 --- a/src/app/helpers/zoom_level.rs +++ /dev/null @@ -1,17 +0,0 @@ -use crate::app::states::ZoomLevel; - -pub enum CellSizes { - Width, - Height, -} - -pub fn cell_size_helper(cell_size: CellSizes, zoom_level: ZoomLevel) -> usize { - match (cell_size, zoom_level) { - (CellSizes::Width, ZoomLevel::ZoomedIn) => 13, - (CellSizes::Width, ZoomLevel::Default) => 9, - (CellSizes::Width, ZoomLevel::ZoomedOut) => 5, - (CellSizes::Height, ZoomLevel::ZoomedIn) => 7, - (CellSizes::Height, ZoomLevel::Default) => 5, - (CellSizes::Height, ZoomLevel::ZoomedOut) => 3, - } -} diff --git a/src/app/keybindings/skirmish.rs b/src/app/keybindings/skirmish.rs index 6bb8ad8..8418376 100644 --- a/src/app/keybindings/skirmish.rs +++ b/src/app/keybindings/skirmish.rs @@ -1,10 +1,7 @@ use crate::app::{ App, keybindings::{Action, common_keybindings, event_to_action}, - states::{ - ZoomLevel, - skirmish_states::{BoardState, MoveFocusedCell}, - }, + states::skirmish_states::{BoardState, MoveFocusedCell, ZoomLevel}, }; use ratatui::crossterm::event::KeyEvent; diff --git a/src/app/states/mod.rs b/src/app/states/mod.rs index 2658d06..14cacc8 100644 --- a/src/app/states/mod.rs +++ b/src/app/states/mod.rs @@ -9,5 +9,5 @@ pub use main_menu::MainMenuState; pub use perk_decks::{PerkDecks, PerkDecksState}; pub use settings::SettingsState; pub use skills_config::SkillsConfigState; -pub use skirmish::{CellStructure, GameMode, Players, SkirmishState, Units, ZoomLevel}; +pub use skirmish::{GameMode, Players, SkirmishState}; pub use skirmish_states::{FocusedCell, Offset}; diff --git a/src/app/states/skirmish.rs b/src/app/states/skirmish.rs index a0d4735..41737bd 100644 --- a/src/app/states/skirmish.rs +++ b/src/app/states/skirmish.rs @@ -1,8 +1,4 @@ -use crate::app::states::skirmish_states::{ - BoardState, - structures::{BaseBuilding, Stone, Tunnel}, - units::MinerUnit, -}; +use crate::app::states::skirmish_states::BoardState; use clap::ValueEnum; #[derive(Debug, Clone, PartialEq, Eq)] @@ -18,27 +14,8 @@ pub enum GameMode { FrontLines, } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ValueEnum)] -pub enum ZoomLevel { - ZoomedIn, - Default, - ZoomedOut, -} - #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Players { Player, Enemy, } - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum CellStructure { - Base(BaseBuilding), - Tunnel(Tunnel), - Stone(Stone), -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Units { - Miner(MinerUnit), -} diff --git a/src/app/states/skirmish_states/board.rs b/src/app/states/skirmish_states/board.rs index bec2f59..b793a4f 100644 --- a/src/app/states/skirmish_states/board.rs +++ b/src/app/states/skirmish_states/board.rs @@ -1,11 +1,11 @@ use crate::app::{ - helpers::{CellSizes, cell_size_helper, cells_area_helper}, + helpers::cells_area_helper, states::{ - CellStructure, FocusedCell, Offset, Players, Units, ZoomLevel, + FocusedCell, Offset, Players, skirmish_states::{ - MoveFocusedCell, - structures::{BaseBuilding, Stone}, - units::MinerUnit, + CellSizes, MoveFocusedCell, ZoomLevel, + structures::{BaseBuilding, Stone, Structures}, + units::{MinerUnit, Units}, }, }, widgets::CellWidget, @@ -36,8 +36,8 @@ impl BoardState { pub fn new(area: &Rect, map_width: usize, map_height: usize, zoom_level: ZoomLevel) -> Self { let cells_area: Rect = cells_area_helper(area); - let cell_width: usize = cell_size_helper(CellSizes::Width, zoom_level); - let cell_height: usize = cell_size_helper(CellSizes::Height, zoom_level); + let cell_width: usize = zoom_level.get_cell_size(CellSizes::Width); + let cell_height: usize = zoom_level.get_cell_size(CellSizes::Height); let cols: usize = (cells_area.width / cell_width as u16) as usize; let rows: usize = (cells_area.height / cell_height as u16) as usize; @@ -67,12 +67,12 @@ impl BoardState { let player_base: bool = row == player_base_coords.0 && col == player_base_coords.1; let enemy_base: bool = row == enemy_base_coords.0 && col == enemy_base_coords.1; - let structure: CellStructure = if player_base { - CellStructure::Base(BaseBuilding::new(Players::Player)) + let structure: Structures = if player_base { + Structures::Base(BaseBuilding::new(Players::Player)) } else if enemy_base { - CellStructure::Base(BaseBuilding::new(Players::Enemy)) + Structures::Base(BaseBuilding::new(Players::Enemy)) } else { - CellStructure::Stone(Stone::new()) + Structures::Stone(Stone::new()) }; let unit: Option = if player_base { @@ -227,8 +227,8 @@ impl BoardState { pub fn change_zoom(&mut self, new_zoom_level: ZoomLevel) { self.zoom_level = new_zoom_level; - self.cell_width = cell_size_helper(CellSizes::Width, self.zoom_level); - self.cell_height = cell_size_helper(CellSizes::Height, self.zoom_level); + self.cell_width = new_zoom_level.get_cell_size(CellSizes::Width); + self.cell_height = new_zoom_level.get_cell_size(CellSizes::Height); self.cols = (self.cells_area.width / self.cell_width as u16) as usize; self.rows = (self.cells_area.height / self.cell_height as u16) as usize; diff --git a/src/app/states/skirmish_states/mod.rs b/src/app/states/skirmish_states/mod.rs index 3643a90..a7c5a33 100644 --- a/src/app/states/skirmish_states/mod.rs +++ b/src/app/states/skirmish_states/mod.rs @@ -3,7 +3,9 @@ mod focused_cell; mod offset; pub mod structures; pub mod units; +pub mod zoom_level; pub use board::BoardState; pub use focused_cell::{FocusedCell, MoveFocusedCell}; pub use offset::Offset; +pub use zoom_level::{CellSizes, ZoomLevel}; diff --git a/src/app/states/skirmish_states/structures/base.rs b/src/app/states/skirmish_states/structures/base.rs index 13751a1..4742e58 100644 --- a/src/app/states/skirmish_states/structures/base.rs +++ b/src/app/states/skirmish_states/structures/base.rs @@ -1,34 +1,50 @@ +use crate::app::states::{Players, skirmish_states::structures::Structure}; use ratatui::style::Color; -use crate::app::states::Players; - #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct BaseBuilding { + durability: u16, + stress: u8, owner: Players, level: u8, } impl BaseBuilding { pub fn new(owner: Players) -> Self { - Self { owner, level: b'1' } - } - - pub fn get_tag(&self) -> char { - 'B' - } - - pub fn get_color(&self) -> Color { - match self.owner { - Players::Player => Color::LightBlue, - Players::Enemy => Color::LightRed, + Self { + durability: 1500, + stress: 0, + owner, + level: b'1', } } pub fn get_owner(&self) -> Players { self.owner } +} - pub fn get_level(&self) -> char { +impl Structure for BaseBuilding { + fn get_tag(&self) -> char { + 'B' + } + + fn get_color(&self) -> Color { + match self.owner { + Players::Player => Color::LightBlue, + Players::Enemy => Color::LightRed, + } + } + + fn get_level(&self) -> char { self.level as char } + + fn get_durability(&self) -> u16 { + self.durability + } + + fn get_stress(&self) -> u8 { + self.stress + } } diff --git a/src/app/states/skirmish_states/structures/mod.rs b/src/app/states/skirmish_states/structures/mod.rs index 35b7ebb..e8597db 100644 --- a/src/app/states/skirmish_states/structures/mod.rs +++ b/src/app/states/skirmish_states/structures/mod.rs @@ -1,7 +1,11 @@ mod base; mod stone; +mod structures_enum; +mod structures_trait; mod tunnel; pub use base::BaseBuilding; pub use stone::Stone; +pub use structures_enum::Structures; +pub use structures_trait::Structure; pub use tunnel::Tunnel; diff --git a/src/app/states/skirmish_states/structures/stone.rs b/src/app/states/skirmish_states/structures/stone.rs index f09eca2..901889a 100644 --- a/src/app/states/skirmish_states/structures/stone.rs +++ b/src/app/states/skirmish_states/structures/stone.rs @@ -1,20 +1,39 @@ +use crate::app::states::skirmish_states::structures::Structure; use ratatui::style::Color; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Stone { durability: u16, + stress: u8, } impl Stone { pub fn new() -> Self { - Self { durability: 1000 } + Self { + durability: 1000, + stress: 0, + } } +} - pub fn get_tag(&self) -> char { +impl Structure for Stone { + fn get_tag(&self) -> char { ' ' } - pub fn get_color(&self) -> Color { + fn get_color(&self) -> Color { Color::White } + + fn get_level(&self) -> char { + ' ' + } + + fn get_durability(&self) -> u16 { + self.durability + } + + fn get_stress(&self) -> u8 { + self.stress + } } diff --git a/src/app/states/skirmish_states/structures/structures_enum.rs b/src/app/states/skirmish_states/structures/structures_enum.rs new file mode 100644 index 0000000..350440a --- /dev/null +++ b/src/app/states/skirmish_states/structures/structures_enum.rs @@ -0,0 +1,35 @@ +use crate::app::states::skirmish_states::structures::{BaseBuilding, Stone, Structure, Tunnel}; +use ratatui::style::Color; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Structures { + Base(BaseBuilding), + Tunnel(Tunnel), + Stone(Stone), +} + +impl Structures { + pub fn get_color(&self) -> Color { + match self { + Structures::Base(b) => b.get_color(), + Structures::Tunnel(t) => t.get_color(), + Structures::Stone(s) => s.get_color(), + } + } + + pub fn get_tag(&self) -> char { + match self { + Structures::Base(b) => b.get_tag(), + Structures::Tunnel(t) => t.get_tag(), + Structures::Stone(s) => s.get_tag(), + } + } + + pub fn get_level(&self) -> char { + match self { + Structures::Base(b) => b.get_level(), + Structures::Tunnel(t) => t.get_level(), + Structures::Stone(s) => s.get_level(), + } + } +} diff --git a/src/app/states/skirmish_states/structures/structures_trait.rs b/src/app/states/skirmish_states/structures/structures_trait.rs new file mode 100644 index 0000000..f911a9f --- /dev/null +++ b/src/app/states/skirmish_states/structures/structures_trait.rs @@ -0,0 +1,9 @@ +use ratatui::style::Color; + +pub trait Structure { + fn get_tag(&self) -> char; + fn get_color(&self) -> Color; + fn get_level(&self) -> char; + fn get_stress(&self) -> u8; + fn get_durability(&self) -> u16; +} diff --git a/src/app/states/skirmish_states/structures/tunnel.rs b/src/app/states/skirmish_states/structures/tunnel.rs index a0366bd..547d1e8 100644 --- a/src/app/states/skirmish_states/structures/tunnel.rs +++ b/src/app/states/skirmish_states/structures/tunnel.rs @@ -1,3 +1,4 @@ +use crate::app::states::skirmish_states::structures::Structure; use ratatui::style::Color; #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -19,20 +20,26 @@ impl Tunnel { lamp: false, } } +} - pub fn get_tag(&self) -> char { +impl Structure for Tunnel { + fn get_tag(&self) -> char { 'T' } - pub fn get_color(&self) -> Color { + fn get_color(&self) -> Color { Color::Gray } - pub fn get_durability(&self) -> u16 { + fn get_level(&self) -> char { + ' ' + } + + fn get_durability(&self) -> u16 { self.durability } - pub fn get_stress(&self) -> u8 { + fn get_stress(&self) -> u8 { self.stress } } diff --git a/src/app/states/skirmish_states/units/miner.rs b/src/app/states/skirmish_states/units/miner.rs index 609fac2..06f2031 100644 --- a/src/app/states/skirmish_states/units/miner.rs +++ b/src/app/states/skirmish_states/units/miner.rs @@ -1,4 +1,4 @@ -use crate::app::states::Players; +use crate::app::states::{Players, skirmish_states::units::Unit}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct MinerUnit { @@ -9,8 +9,14 @@ impl MinerUnit { pub fn new(owner: Players) -> Self { Self { owner } } +} - pub fn get_tag(self) -> char { +impl Unit for MinerUnit { + fn get_owner(&self) -> Players { + self.owner + } + + fn get_tag(&self) -> char { 'M' } } diff --git a/src/app/states/skirmish_states/units/mod.rs b/src/app/states/skirmish_states/units/mod.rs index b8343c9..491ebe6 100644 --- a/src/app/states/skirmish_states/units/mod.rs +++ b/src/app/states/skirmish_states/units/mod.rs @@ -1,3 +1,7 @@ mod miner; +mod units_enum; +mod units_trait; pub use miner::MinerUnit; +pub use units_enum::Units; +pub use units_trait::{OptionalUnit, Unit}; diff --git a/src/app/states/skirmish_states/units/units_enum.rs b/src/app/states/skirmish_states/units/units_enum.rs new file mode 100644 index 0000000..951426d --- /dev/null +++ b/src/app/states/skirmish_states/units/units_enum.rs @@ -0,0 +1,20 @@ +use crate::app::states::skirmish_states::units::{MinerUnit, OptionalUnit, Unit}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Units { + Miner(MinerUnit), +} + +impl Units { + pub fn get_tag(&self) -> char { + match self { + Units::Miner(m) => m.get_tag(), + } + } +} + +impl OptionalUnit for Option { + fn try_get_tag(&self) -> char { + self.map_or(' ', |u| u.get_tag()) + } +} diff --git a/src/app/states/skirmish_states/units/units_trait.rs b/src/app/states/skirmish_states/units/units_trait.rs new file mode 100644 index 0000000..1641fc8 --- /dev/null +++ b/src/app/states/skirmish_states/units/units_trait.rs @@ -0,0 +1,10 @@ +use crate::app::states::Players; + +pub trait Unit { + fn get_owner(&self) -> Players; + fn get_tag(&self) -> char; +} + +pub trait OptionalUnit { + fn try_get_tag(&self) -> char; +} diff --git a/src/app/states/skirmish_states/zoom_level.rs b/src/app/states/skirmish_states/zoom_level.rs new file mode 100644 index 0000000..b85f643 --- /dev/null +++ b/src/app/states/skirmish_states/zoom_level.rs @@ -0,0 +1,50 @@ +use clap::ValueEnum; +use ratatui::text::Line; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ValueEnum)] +pub enum ZoomLevel { + ZoomedIn, + Default, + ZoomedOut, +} + +pub enum CellSizes { + Width, + Height, +} + +impl ZoomLevel { + pub fn get_cell_size(&self, cell_size: CellSizes) -> usize { + match (cell_size, self) { + (CellSizes::Width, ZoomLevel::ZoomedIn) => 13, + (CellSizes::Width, ZoomLevel::Default) => 9, + (CellSizes::Width, ZoomLevel::ZoomedOut) => 5, + (CellSizes::Height, ZoomLevel::ZoomedIn) => 7, + (CellSizes::Height, ZoomLevel::Default) => 5, + (CellSizes::Height, ZoomLevel::ZoomedOut) => 3, + } + } + + pub fn get_cell_text_area( + &self, + tag: char, + structure_level: char, + unit: char, + ) -> Vec> { + match self { + ZoomLevel::ZoomedIn => vec![ + Line::from(format!(" {} {} ", structure_level, unit)), + Line::from(format!(" ")), + Line::from(format!(" {} ", tag)), + Line::from(format!(" ")), + Line::from(format!(" ")), + ], + ZoomLevel::Default => vec![ + Line::from(format!(" {} {} ", structure_level, unit)), + Line::from(format!(" {} ", tag)), + Line::from(format!(" ")), + ], + ZoomLevel::ZoomedOut => vec![Line::from(format!(" {} ", tag))], + } + } +} diff --git a/src/app/widgets/cell.rs b/src/app/widgets/cell.rs index 76a71d5..fa57896 100644 --- a/src/app/widgets/cell.rs +++ b/src/app/widgets/cell.rs @@ -1,4 +1,8 @@ -use crate::app::states::{CellStructure, Units, ZoomLevel, skirmish_states::structures::Stone}; +use crate::app::states::skirmish_states::{ + ZoomLevel, + structures::{Stone, Structures}, + units::{OptionalUnit, Units}, +}; use ratatui::{ buffer::Buffer, layout::{Alignment, Rect}, @@ -13,7 +17,7 @@ pub struct CellWidget { col: usize, selected: bool, zoom_level: ZoomLevel, - structure: CellStructure, + structure: Structures, marked: bool, unit: Option, } @@ -24,7 +28,7 @@ impl CellWidget { col: usize, zoom_level: ZoomLevel, selected: bool, - structure: CellStructure, + structure: Structures, unit: Option, ) -> Self { Self { @@ -48,7 +52,7 @@ impl CellWidget { self } - pub fn set_structure(&mut self, sctructure: CellStructure) -> &mut Self { + pub fn set_structure(&mut self, sctructure: Structures) -> &mut Self { self.structure = sctructure; self } @@ -75,7 +79,7 @@ impl CellWidget { } fn display_coords(&self) -> Span<'_> { - if self.selected || self.structure != CellStructure::Stone(Stone::new()) || self.marked { + if self.selected || self.structure != Structures::Stone(Stone::new()) || self.marked { format!("{}{}", self.col_to_letters(), self.row).green() } else { "".to_span() @@ -84,61 +88,22 @@ impl CellWidget { fn fg_color(&self) -> Color { if self.marked && self.selected { - return Color::Magenta; + Color::Magenta } else if self.marked && !self.selected { - return Color::LightMagenta; - } else if self.selected && !self.marked { - return Color::LightYellow; - } - - match self.structure { - CellStructure::Base(base) => base.get_color(), - CellStructure::Tunnel(tunnel) => tunnel.get_color(), - CellStructure::Stone(stone) => stone.get_color(), + Color::LightMagenta + } else if !self.marked && self.selected { + Color::LightYellow + } else { + self.structure.get_color() } } fn get_text_area(&self) -> Vec> { - let tag: char = match self.structure { - CellStructure::Base(base) => base.get_tag(), - CellStructure::Tunnel(tunnel) => tunnel.get_tag(), - CellStructure::Stone(stone) => stone.get_tag(), - }; - - let b_level: char = match self.structure { - CellStructure::Base(base) => base.get_level(), - _ => ' ', - }; - - let unit: char = match self.unit { - Some(unit) => match unit { - Units::Miner(miner) => miner.get_tag(), - // _ => ' ', - }, - None => ' ', - }; - - let mut text_area: Vec> = Vec::new(); - - match self.zoom_level { - ZoomLevel::ZoomedIn => { - text_area.push(Line::from(format!(" {} {} ", b_level, unit))); - text_area.push(Line::from(format!(" "))); - text_area.push(Line::from(format!(" {} ", tag))); - text_area.push(Line::from(format!(" "))); - text_area.push(Line::from(format!(" "))); - } - ZoomLevel::Default => { - text_area.push(Line::from(format!(" {} {} ", b_level, unit))); - text_area.push(Line::from(format!(" {} ", tag))); - text_area.push(Line::from(format!(" "))); - } - ZoomLevel::ZoomedOut => { - text_area.push(Line::from(format!(" {} ", tag))); - } - } - - text_area + self.zoom_level.get_cell_text_area( + self.structure.get_tag(), + self.structure.get_level(), + self.unit.try_get_tag(), + ) } fn get_block(&self) -> Block<'_> { diff --git a/src/cli.rs b/src/cli.rs index 5f9081c..5205672 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,5 +1,5 @@ use crate::app::{ - states::{GameMode, PerkDecks, ZoomLevel}, + states::{GameMode, PerkDecks, skirmish_states::ZoomLevel}, threads::Soundtrack, view::View, };