generated from GarandPLG/rust-flake-template
Add Task trait and improve miner task display
Define a full `Task` trait with icon and path accessors. Implement the trait for `DiggingTask` and delegate it through the `Tasks` enum. Introduce helper functions in `MinerUnit` to convert coordinates to spreadsheet‑style strings and use them in a revamped `get_tasks_formatted`. Update the unit rendering logic to show the tasks queue only when tasks are present.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use crate::app::states::skirmish_states::tasks::Task;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct DiggingTask {
|
||||
path: VecDeque<(usize, usize)>,
|
||||
@@ -10,3 +12,21 @@ impl DiggingTask {
|
||||
Self { path }
|
||||
}
|
||||
}
|
||||
|
||||
impl Task for DiggingTask {
|
||||
fn get_icon(&self) -> char {
|
||||
'⛏'
|
||||
}
|
||||
|
||||
fn get_path(&self) -> &VecDeque<(usize, usize)> {
|
||||
&self.path
|
||||
}
|
||||
|
||||
fn get_path_front_coords(&self) -> &(usize, usize) {
|
||||
self.path.front().map_or(&(0, 0), |c| c)
|
||||
}
|
||||
|
||||
fn get_path_back_coords(&self) -> &(usize, usize) {
|
||||
self.path.back().map_or(&(0, 0), |c| c)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1,11 @@
|
||||
pub trait Task {}
|
||||
use std::collections::VecDeque;
|
||||
|
||||
pub trait Task {
|
||||
fn get_icon(&self) -> char;
|
||||
|
||||
fn get_path(&self) -> &VecDeque<(usize, usize)>;
|
||||
|
||||
fn get_path_front_coords(&self) -> &(usize, usize);
|
||||
|
||||
fn get_path_back_coords(&self) -> &(usize, usize);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,34 @@
|
||||
use crate::app::states::skirmish_states::tasks::DiggingTask;
|
||||
use crate::app::states::skirmish_states::tasks::{DiggingTask, Task};
|
||||
use std::collections::VecDeque;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Tasks {
|
||||
Digging(DiggingTask),
|
||||
}
|
||||
|
||||
impl Tasks {
|
||||
#[inline]
|
||||
fn t(&self) -> &dyn Task {
|
||||
match self {
|
||||
Self::Digging(d) => d,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Task for Tasks {
|
||||
fn get_icon(&self) -> char {
|
||||
self.t().get_icon()
|
||||
}
|
||||
|
||||
fn get_path(&self) -> &VecDeque<(usize, usize)> {
|
||||
self.t().get_path()
|
||||
}
|
||||
|
||||
fn get_path_front_coords(&self) -> &(usize, usize) {
|
||||
self.t().get_path_front_coords()
|
||||
}
|
||||
|
||||
fn get_path_back_coords(&self) -> &(usize, usize) {
|
||||
self.t().get_path_back_coords()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
use crate::app::states::skirmish_states::{Players, tasks::Tasks, units::Unit};
|
||||
use crate::app::states::skirmish_states::{
|
||||
Players,
|
||||
tasks::{Task, Tasks},
|
||||
units::Unit,
|
||||
};
|
||||
use std::collections::VecDeque;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
@@ -22,6 +26,22 @@ impl MinerUnit {
|
||||
tasks: VecDeque::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn col_to_letters(&self, col: usize) -> String {
|
||||
let mut col: usize = col + 1;
|
||||
let mut letters: Vec<char> = Vec::new();
|
||||
|
||||
while col > 0 {
|
||||
letters.push((b'A' + ((col - 1) % 26) as u8) as char);
|
||||
col = (col - 1) / 26;
|
||||
}
|
||||
|
||||
letters.iter().rev().collect()
|
||||
}
|
||||
|
||||
fn display_coords(&self, coords: &(usize, usize)) -> String {
|
||||
format!("{}{}", self.col_to_letters(coords.0), coords.1)
|
||||
}
|
||||
}
|
||||
|
||||
impl Unit for MinerUnit {
|
||||
@@ -62,7 +82,17 @@ impl Unit for MinerUnit {
|
||||
}
|
||||
|
||||
fn get_tasks_formatted(&self) -> String {
|
||||
format!("{:?}", self.tasks)
|
||||
// format!("{:?}", self.tasks)
|
||||
let mut output: String = String::new();
|
||||
for task in &self.tasks {
|
||||
output.push(task.get_icon());
|
||||
output.push(' ');
|
||||
output += &self.display_coords(task.get_path_front_coords());
|
||||
output += " -> ";
|
||||
output += &self.display_coords(task.get_path_back_coords());
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
fn set_task(&mut self, task: Tasks) {
|
||||
|
||||
@@ -39,6 +39,7 @@ pub trait Unit {
|
||||
let hp_percent: u16 = hp * 100 / max_hp;
|
||||
let can_dig: bool = self.get_can_dig();
|
||||
let digging_power: f32 = self.get_digging_power();
|
||||
let have_any_tasks: bool = self.get_tasks().is_empty();
|
||||
let tasks: String = self.get_tasks_formatted();
|
||||
|
||||
let mut lines: Vec<Line<'_>> = Vec::from([
|
||||
@@ -52,7 +53,6 @@ pub trait Unit {
|
||||
hp_percent.green(),
|
||||
"% )".gray(),
|
||||
]),
|
||||
Line::from_iter(["Tasks Queue: ".gray(), tasks.green()]),
|
||||
]);
|
||||
|
||||
if can_dig {
|
||||
@@ -62,6 +62,10 @@ pub trait Unit {
|
||||
]));
|
||||
}
|
||||
|
||||
if !have_any_tasks {
|
||||
lines.push(Line::from_iter(["Tasks Queue: ".gray(), tasks.green()]));
|
||||
}
|
||||
|
||||
lines
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user