Add Miner stats and conditional audio flag

- Extend MinerUnit with hp, digging ability, and power, and provide
  getters for these fields.
- Update the Unit trait and Units enum to expose hp, max hp,
  can_dig, and digging_power, and enhance base_text to display them.
- Remove unnecessary `Eq` derives from several structs and enums.
- Introduce a `--disable-audio` CLI flag and conditionally spawn the
  audio thread based on its value.
This commit is contained in:
2026-05-05 16:58:29 +02:00
parent 35209dd064
commit a1d0a84885
9 changed files with 113 additions and 14 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
use crate::app::states::skirmish_states::BoardState;
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq)]
pub struct SkirmishState {
pub id: usize,
pub name: &'static str,
+1 -1
View File
@@ -12,7 +12,7 @@ use crate::app::{
};
use ratatui::layout::Rect;
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq)]
pub struct BoardState {
cells_area: Rect,
pub cell_width: usize,
+26 -2
View File
@@ -1,13 +1,21 @@
use crate::app::states::skirmish_states::{Players, units::Unit};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MinerUnit {
owner: Players,
hp: u16,
can_dig: bool,
digging_power: f32,
}
impl MinerUnit {
pub fn new(owner: Players) -> Self {
Self { owner }
Self {
owner,
hp: 100,
can_dig: true,
digging_power: 0.55,
}
}
}
@@ -23,4 +31,20 @@ impl Unit for MinerUnit {
fn get_name(&self) -> &'static str {
"Miner"
}
fn get_hp(&self) -> u16 {
self.hp
}
fn get_max_hp(&self) -> u16 {
100
}
fn get_can_dig(&self) -> bool {
self.can_dig
}
fn get_digging_power(&self) -> f32 {
self.digging_power
}
}
@@ -3,7 +3,7 @@ use crate::app::states::skirmish_states::{
units::{MinerUnit, Unit},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Units {
Miner(MinerUnit),
}
@@ -28,9 +28,29 @@ impl Unit for Units {
fn get_owner(&self) -> Players {
self.unit().get_owner()
}
fn get_hp(&self) -> u16 {
self.unit().get_hp()
}
fn get_max_hp(&self) -> u16 {
self.unit().get_max_hp()
}
fn get_can_dig(&self) -> bool {
self.unit().get_can_dig()
}
fn get_digging_power(&self) -> f32 {
self.unit().get_digging_power()
}
}
impl Unit for Option<Units> {
fn is_unit(&self) -> bool {
self.map_or(false, |u| u.is_unit())
}
fn get_tag(&self) -> char {
self.map_or(' ', |u| u.get_tag())
}
@@ -43,7 +63,19 @@ impl Unit for Option<Units> {
self.map_or(Players::Unclaimed, |u| u.get_owner())
}
fn is_unit(&self) -> bool {
self.map_or(false, |u| u.is_unit())
fn get_hp(&self) -> u16 {
self.map_or(0, |u| u.get_hp())
}
fn get_max_hp(&self) -> u16 {
self.map_or(0, |u| u.get_max_hp())
}
fn get_can_dig(&self) -> bool {
self.map_or(false, |u| u.get_can_dig())
}
fn get_digging_power(&self) -> f32 {
self.map_or(0.0, |u| u.get_digging_power())
}
}
@@ -5,20 +5,53 @@ use ratatui::{
};
pub trait Unit {
fn is_unit(&self) -> bool {
true
}
fn get_owner(&self) -> Players;
fn get_tag(&self) -> char;
fn get_name(&self) -> &'static str;
fn is_unit(&self) -> bool {
true
}
fn get_hp(&self) -> u16;
fn get_max_hp(&self) -> u16;
fn get_can_dig(&self) -> bool;
fn get_digging_power(&self) -> f32;
fn base_text(&self) -> Vec<Line<'_>> {
let owner: Span<'_> = self.get_owner().get_span();
let hp: u16 = self.get_hp();
let max_hp: u16 = self.get_max_hp();
let hp_percent: u16 = hp * 100 / max_hp;
let can_dig: bool = self.get_can_dig();
let digging_power: f32 = self.get_digging_power();
vec![Line::from_iter(vec!["Owner: ".gray(), owner])]
let mut lines: Vec<Line<'_>> = Vec::from([
Line::from_iter(["Owner: ".gray(), owner]),
Line::from_iter([
"HP: ".gray(),
hp.green(),
"/".gray(),
max_hp.green(),
" ( ".gray(),
hp_percent.green(),
"% )".green(),
]),
]);
if can_dig {
lines.push(Line::from_iter([
"Digging power: ".gray(),
digging_power.green(),
]));
}
lines
}
fn text(&self) -> Vec<Line<'_>> {
+1 -1
View File
@@ -11,7 +11,7 @@ use ratatui::{
widgets::{Block, BorderType, Borders, Paragraph, Widget},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CellWidget {
row: usize,
col: usize,
+1 -1
View File
@@ -12,7 +12,7 @@ use ratatui::{
widgets::{Block, BorderType, Borders, Padding, Paragraph, Widget},
};
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq)]
pub struct SidePanelWidget<'a> {
coords: (usize, usize),
structure: &'a Structures,
+8
View File
@@ -150,6 +150,14 @@ pub struct Cli {
)]
pub soundtrack: Soundtrack,
/// Disable soundtrack (default: disabled).
#[arg(
long,
help = "Disable soundtrack (default: disable)",
default_value_t = false
)]
pub disable_audio: bool,
/// Mute soundtrack (default: disabled).
#[arg(
long,
+3 -1
View File
@@ -27,7 +27,9 @@ fn main() -> Result<()> {
thread::spawn(move || handle_ct_events(input_tx, resize_tx));
thread::spawn(move || handle_tick_event(tick_tx));
thread::spawn(move || handle_audio(audio_rx, args.mute, args.soundtrack));
if !args.disable_audio {
thread::spawn(move || handle_audio(audio_rx, args.mute, args.soundtrack));
}
let mut terminal: Terminal<CrosstermBackend<Stdout>> = ratatui::init();
let mut app: App = App::new(args, audio_tx);