diff --git a/src/app/states/skirmish_states/board.rs b/src/app/states/skirmish_states/board.rs index 9aabeb8..80fe5cd 100644 --- a/src/app/states/skirmish_states/board.rs +++ b/src/app/states/skirmish_states/board.rs @@ -81,9 +81,9 @@ impl BoardState { } else if enemy_base { Structures::Base(BaseBuilding::new(Players::Enemy)) } else if player_ore { - Structures::Ore(Ore::new(Players::Player)) + Structures::Ore(Ore::new(Players::Player, 1)) } else if enemy_ore { - Structures::Ore(Ore::new(Players::Enemy)) + Structures::Ore(Ore::new(Players::Enemy, 1)) } else { Structures::Stone(Stone::new()) }; diff --git a/src/app/states/skirmish_states/structures/base.rs b/src/app/states/skirmish_states/structures/base.rs index 62e2146..d91b2f1 100644 --- a/src/app/states/skirmish_states/structures/base.rs +++ b/src/app/states/skirmish_states/structures/base.rs @@ -15,7 +15,7 @@ impl BaseBuilding { durability: 1500, stress: 0, owner, - level: b'1', + level: 1, } } } @@ -34,7 +34,7 @@ impl Structure for BaseBuilding { } fn get_level(&self) -> char { - self.level as char + (self.level + b'0') as char } fn get_durability(&self) -> u16 { diff --git a/src/app/states/skirmish_states/structures/ore.rs b/src/app/states/skirmish_states/structures/ore.rs index e976546..fb84ee6 100644 --- a/src/app/states/skirmish_states/structures/ore.rs +++ b/src/app/states/skirmish_states/structures/ore.rs @@ -1,22 +1,26 @@ use crate::app::states::skirmish_states::{Players, structures::Structure}; -use ratatui::style::Color; +use ratatui::{ + style::{Color, Stylize}, + text::Line, +}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Ore { owner: Players, ore_amount: u16, - // ore_max_amount: u16, durability: u16, stress: u8, + level: u8, } impl Ore { - pub fn new(owner: Players) -> Self { + pub fn new(owner: Players, level: u8) -> Self { Self { owner, - ore_amount: 1000, + ore_amount: 5000, durability: 1000, stress: 0, + level, } } @@ -25,7 +29,12 @@ impl Ore { } pub fn get_max_ore_amount(&self) -> u16 { - 1000 + 5000 + } + + pub fn set_owner(&mut self, new_owner: Players) -> &mut Self { + self.owner = new_owner; + self } } @@ -47,7 +56,11 @@ impl Structure for Ore { } fn get_level(&self) -> char { - ' ' + if self.level != 0 { + (self.level + b'0') as char + } else { + ' ' + } } fn get_max_durability(&self) -> u16 { @@ -65,4 +78,17 @@ impl Structure for Ore { fn get_stress(&self) -> u8 { self.stress } + + fn text(&self) -> Vec> { + let mut lines: Vec> = Structure::base_text(self); + + lines.push(Line::from_iter([ + "Ore: ".gray(), + self.get_ore_amount().to_string().cyan(), + "/".gray(), + self.get_max_ore_amount().to_string().cyan(), + ])); + + lines + } } diff --git a/src/app/states/skirmish_states/structures/structures_enum.rs b/src/app/states/skirmish_states/structures/structures_enum.rs index 9cebc09..2960571 100644 --- a/src/app/states/skirmish_states/structures/structures_enum.rs +++ b/src/app/states/skirmish_states/structures/structures_enum.rs @@ -2,7 +2,7 @@ use crate::app::states::skirmish_states::{ Players, structures::{BaseBuilding, Ore, Stone, Structure, Tunnel}, }; -use ratatui::style::Color; +use ratatui::{style::Color, text::Line}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Structures { @@ -56,4 +56,8 @@ impl Structure for Structures { fn get_owner(&self) -> Players { self.structure().get_owner() } + + fn text(&self) -> Vec> { + self.structure().text() + } } diff --git a/src/app/states/skirmish_states/structures/structures_trait.rs b/src/app/states/skirmish_states/structures/structures_trait.rs index 28950b0..db4843d 100644 --- a/src/app/states/skirmish_states/structures/structures_trait.rs +++ b/src/app/states/skirmish_states/structures/structures_trait.rs @@ -1,13 +1,57 @@ use crate::app::states::skirmish_states::Players; -use ratatui::style::Color; +use ratatui::{ + style::{Color, Stylize}, + text::Line, +}; 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; + fn get_max_durability(&self) -> u16; + fn get_name(&self) -> &'static str; + fn get_owner(&self) -> Players; + + fn base_text(&self) -> Vec> { + let durability: u16 = self.get_durability(); + let max_durability: u16 = self.get_max_durability(); + let durability_percent: u32 = durability as u32 * 100 / max_durability as u32; + let stress: u8 = self.get_stress(); + let level: char = self.get_level(); + + let mut lines: Vec> = vec![ + Line::from_iter([ + "Durability: ".gray(), + durability.to_string().cyan(), + "/".gray(), + max_durability.to_string().cyan(), + " ( ".gray(), + durability_percent.to_string().cyan(), + "% )".gray(), + ]), + Line::from_iter(["Stress: ".gray(), stress.to_string().cyan(), "%".gray()]), + ]; + + if level != ' ' { + lines.push(Line::from_iter([ + "Level: ".gray(), + level.to_string().cyan(), + ])); + } + + lines + } + + fn text(&self) -> Vec> { + self.base_text() + } } diff --git a/src/app/states/skirmish_states/units/units_enum.rs b/src/app/states/skirmish_states/units/units_enum.rs index 260eae2..a4a8406 100644 --- a/src/app/states/skirmish_states/units/units_enum.rs +++ b/src/app/states/skirmish_states/units/units_enum.rs @@ -42,4 +42,8 @@ impl Unit for Option { fn get_owner(&self) -> Players { self.map_or(Players::Unclaimed, |u| u.get_owner()) } + + fn is_unit(&self) -> bool { + self.map_or(false, |u| u.is_unit()) + } } diff --git a/src/app/states/skirmish_states/units/units_trait.rs b/src/app/states/skirmish_states/units/units_trait.rs index 89c130b..59e6b54 100644 --- a/src/app/states/skirmish_states/units/units_trait.rs +++ b/src/app/states/skirmish_states/units/units_trait.rs @@ -1,7 +1,27 @@ use crate::app::states::skirmish_states::Players; +use ratatui::{ + style::Stylize, + text::{Line, Span}, +}; pub trait Unit { fn get_owner(&self) -> Players; + fn get_tag(&self) -> char; + fn get_name(&self) -> &'static str; + + fn is_unit(&self) -> bool { + true + } + + fn base_text(&self) -> Vec> { + let owner: Span<'_> = self.get_owner().get_span(); + + vec![Line::from_iter(vec!["Owner: ".gray(), owner])] + } + + fn text(&self) -> Vec> { + self.base_text() + } } diff --git a/src/app/widgets/side_panel.rs b/src/app/widgets/side_panel.rs index 8acc021..4e5b738 100644 --- a/src/app/widgets/side_panel.rs +++ b/src/app/widgets/side_panel.rs @@ -8,8 +8,7 @@ use crate::app::{ use ratatui::{ buffer::Buffer, layout::{Alignment, Constraint, Layout, Margin, Rect}, - style::{Color, Stylize}, - text::{Line, Span}, + style::Color, widgets::{Block, BorderType, Borders, Padding, Paragraph, Widget}, }; @@ -61,52 +60,12 @@ impl<'a> SidePanelWidget<'a> { } fn get_area_constraints(&self) -> [Constraint; 2] { - if !self.unit.get_name().is_empty() { + if self.unit.is_unit() { [Constraint::Percentage(50), Constraint::Percentage(50)] } else { [Constraint::Percentage(100), Constraint::Percentage(0)] } } - - // TODO: Relocate this to Structure trait implementations - fn structure_text(&self) -> Vec> { - let s: &Structures = self.structure; - let durability: u16 = s.get_durability(); - let max_durability: u16 = s.get_max_durability(); - let durability_percent: u32 = durability as u32 * 100u32 / max_durability as u32; - let stress: u8 = s.get_stress(); - let level: char = s.get_level(); - - let mut lines: Vec> = vec![ - Line::from_iter([ - "Durability: ".gray(), - durability.to_string().cyan(), - "/".gray(), - max_durability.to_string().cyan(), - " ( ".gray(), - durability_percent.to_string().cyan(), - "% )".gray(), - ]), - Line::from_iter(["Stress: ".gray(), stress.to_string().cyan(), "%".gray()]), - ]; - - if level != ' ' { - lines.push(Line::from_iter([ - "Level: ".gray(), - level.to_string().cyan(), - ])); - } - - lines - } - - // TODO: Relocate this to Unit trait implementations - fn unit_text(&self) -> Vec> { - let u: &Option = self.unit; - let owner: Span<'_> = u.get_owner().get_span(); - - vec![Line::from_iter(vec!["Owner: ".gray(), owner])] - } } impl Widget for SidePanelWidget<'_> { @@ -121,13 +80,13 @@ impl Widget for SidePanelWidget<'_> { let [structure_area, unit_area] = Layout::vertical(self.get_area_constraints()).areas(inner_area); - Paragraph::new(self.structure_text()) + Paragraph::new(self.structure.text()) .alignment(Alignment::Left) .block(self.get_inner_block(self.structure.get_name().to_string(), Color::Cyan)) .render(structure_area, buf); - if !self.unit.get_name().is_empty() { - Paragraph::new(self.unit_text()) + if self.unit.is_unit() { + Paragraph::new(self.unit.text()) .alignment(Alignment::Left) .block(self.get_inner_block(self.unit.get_name().to_string(), Color::Green)) .render(unit_area, buf);