generated from GarandPLG/rust-flake-template
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:
@@ -1,6 +1,6 @@
|
|||||||
use crate::app::states::skirmish_states::BoardState;
|
use crate::app::states::skirmish_states::BoardState;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct SkirmishState {
|
pub struct SkirmishState {
|
||||||
pub id: usize,
|
pub id: usize,
|
||||||
pub name: &'static str,
|
pub name: &'static str,
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ use crate::app::{
|
|||||||
};
|
};
|
||||||
use ratatui::layout::Rect;
|
use ratatui::layout::Rect;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct BoardState {
|
pub struct BoardState {
|
||||||
cells_area: Rect,
|
cells_area: Rect,
|
||||||
pub cell_width: usize,
|
pub cell_width: usize,
|
||||||
|
|||||||
@@ -1,13 +1,21 @@
|
|||||||
use crate::app::states::skirmish_states::{Players, units::Unit};
|
use crate::app::states::skirmish_states::{Players, units::Unit};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub struct MinerUnit {
|
pub struct MinerUnit {
|
||||||
owner: Players,
|
owner: Players,
|
||||||
|
hp: u16,
|
||||||
|
can_dig: bool,
|
||||||
|
digging_power: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MinerUnit {
|
impl MinerUnit {
|
||||||
pub fn new(owner: Players) -> Self {
|
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 {
|
fn get_name(&self) -> &'static str {
|
||||||
"Miner"
|
"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},
|
units::{MinerUnit, Unit},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub enum Units {
|
pub enum Units {
|
||||||
Miner(MinerUnit),
|
Miner(MinerUnit),
|
||||||
}
|
}
|
||||||
@@ -28,9 +28,29 @@ impl Unit for Units {
|
|||||||
fn get_owner(&self) -> Players {
|
fn get_owner(&self) -> Players {
|
||||||
self.unit().get_owner()
|
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> {
|
impl Unit for Option<Units> {
|
||||||
|
fn is_unit(&self) -> bool {
|
||||||
|
self.map_or(false, |u| u.is_unit())
|
||||||
|
}
|
||||||
|
|
||||||
fn get_tag(&self) -> char {
|
fn get_tag(&self) -> char {
|
||||||
self.map_or(' ', |u| u.get_tag())
|
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())
|
self.map_or(Players::Unclaimed, |u| u.get_owner())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_unit(&self) -> bool {
|
fn get_hp(&self) -> u16 {
|
||||||
self.map_or(false, |u| u.is_unit())
|
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 {
|
pub trait Unit {
|
||||||
|
fn is_unit(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
fn get_owner(&self) -> Players;
|
fn get_owner(&self) -> Players;
|
||||||
|
|
||||||
fn get_tag(&self) -> char;
|
fn get_tag(&self) -> char;
|
||||||
|
|
||||||
fn get_name(&self) -> &'static str;
|
fn get_name(&self) -> &'static str;
|
||||||
|
|
||||||
fn is_unit(&self) -> bool {
|
fn get_hp(&self) -> u16;
|
||||||
true
|
|
||||||
}
|
fn get_max_hp(&self) -> u16;
|
||||||
|
|
||||||
|
fn get_can_dig(&self) -> bool;
|
||||||
|
|
||||||
|
fn get_digging_power(&self) -> f32;
|
||||||
|
|
||||||
fn base_text(&self) -> Vec<Line<'_>> {
|
fn base_text(&self) -> Vec<Line<'_>> {
|
||||||
let owner: Span<'_> = self.get_owner().get_span();
|
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<'_>> {
|
fn text(&self) -> Vec<Line<'_>> {
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ use ratatui::{
|
|||||||
widgets::{Block, BorderType, Borders, Paragraph, Widget},
|
widgets::{Block, BorderType, Borders, Paragraph, Widget},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub struct CellWidget {
|
pub struct CellWidget {
|
||||||
row: usize,
|
row: usize,
|
||||||
col: usize,
|
col: usize,
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ use ratatui::{
|
|||||||
widgets::{Block, BorderType, Borders, Padding, Paragraph, Widget},
|
widgets::{Block, BorderType, Borders, Padding, Paragraph, Widget},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct SidePanelWidget<'a> {
|
pub struct SidePanelWidget<'a> {
|
||||||
coords: (usize, usize),
|
coords: (usize, usize),
|
||||||
structure: &'a Structures,
|
structure: &'a Structures,
|
||||||
|
|||||||
@@ -150,6 +150,14 @@ pub struct Cli {
|
|||||||
)]
|
)]
|
||||||
pub soundtrack: Soundtrack,
|
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).
|
/// Mute soundtrack (default: disabled).
|
||||||
#[arg(
|
#[arg(
|
||||||
long,
|
long,
|
||||||
|
|||||||
+3
-1
@@ -27,7 +27,9 @@ fn main() -> Result<()> {
|
|||||||
|
|
||||||
thread::spawn(move || handle_ct_events(input_tx, resize_tx));
|
thread::spawn(move || handle_ct_events(input_tx, resize_tx));
|
||||||
thread::spawn(move || handle_tick_event(tick_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 terminal: Terminal<CrosstermBackend<Stdout>> = ratatui::init();
|
||||||
let mut app: App = App::new(args, audio_tx);
|
let mut app: App = App::new(args, audio_tx);
|
||||||
|
|||||||
Reference in New Issue
Block a user