generated from GarandPLG/rust-flake-template
Refactor task handling to use VecDeque references
- Change `get_tasks` to return `&VecDeque<Tasks>` instead of a formatted `String`. - Introduce `get_tasks_formatted` for display purposes. - Update `MinerUnit`, `Unit` trait, `Units` enum, and its `Option` implementation accordingly. - Add `EMPTY_TASKS` lazy static for handling empty task lists. - Adjust `BoardState` to use the new task API (commented out conditional).
This commit is contained in:
@@ -219,11 +219,14 @@ impl BoardState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let (row, col) = self.marked_cells.selected_unit.get_coords();
|
let (row, col) = self.marked_cells.selected_unit.get_coords();
|
||||||
|
|
||||||
|
// if !self.marked_cells.marked_cells.len() == 1 {
|
||||||
let task: Tasks = Tasks::Digging(DiggingTask::new(self.marked_cells.marked_cells.clone()));
|
let task: Tasks = Tasks::Digging(DiggingTask::new(self.marked_cells.marked_cells.clone()));
|
||||||
|
|
||||||
self.get_mut_cell(row, col)
|
self.get_mut_cell(row, col)
|
||||||
.get_mut_option_unit()
|
.get_mut_option_unit()
|
||||||
.set_task(task);
|
.set_task(task);
|
||||||
|
// }
|
||||||
|
|
||||||
self.marked_cells.selected_unit = None;
|
self.marked_cells.selected_unit = None;
|
||||||
self.marked_cells.marked_cells.clear();
|
self.marked_cells.marked_cells.clear();
|
||||||
|
|||||||
@@ -57,7 +57,11 @@ impl Unit for MinerUnit {
|
|||||||
self.coords
|
self.coords
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_tasks(&self) -> String {
|
fn get_tasks(&self) -> &VecDeque<Tasks> {
|
||||||
|
&self.tasks
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_tasks_formatted(&self) -> String {
|
||||||
format!("{:?}", self.tasks)
|
format!("{:?}", self.tasks)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ use ratatui::{
|
|||||||
style::Stylize,
|
style::Stylize,
|
||||||
text::{Line, Span},
|
text::{Line, Span},
|
||||||
};
|
};
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
pub trait Unit {
|
pub trait Unit {
|
||||||
fn is_unit(&self) -> bool {
|
fn is_unit(&self) -> bool {
|
||||||
@@ -25,10 +26,12 @@ pub trait Unit {
|
|||||||
|
|
||||||
fn get_coords(&self) -> (usize, usize);
|
fn get_coords(&self) -> (usize, usize);
|
||||||
|
|
||||||
fn get_tasks(&self) -> String;
|
fn get_tasks(&self) -> &VecDeque<Tasks>;
|
||||||
|
|
||||||
fn set_task(&mut self, task: Tasks);
|
fn set_task(&mut self, task: Tasks);
|
||||||
|
|
||||||
|
fn get_tasks_formatted(&self) -> String;
|
||||||
|
|
||||||
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 hp: u16 = self.get_hp();
|
||||||
@@ -36,7 +39,7 @@ pub trait Unit {
|
|||||||
let hp_percent: u16 = hp * 100 / max_hp;
|
let hp_percent: u16 = hp * 100 / max_hp;
|
||||||
let can_dig: bool = self.get_can_dig();
|
let can_dig: bool = self.get_can_dig();
|
||||||
let digging_power: f32 = self.get_digging_power();
|
let digging_power: f32 = self.get_digging_power();
|
||||||
let tasks: String = self.get_tasks();
|
let tasks: String = self.get_tasks_formatted();
|
||||||
|
|
||||||
let mut lines: Vec<Line<'_>> = Vec::from([
|
let mut lines: Vec<Line<'_>> = Vec::from([
|
||||||
Line::from_iter(["Owner: ".gray(), owner]),
|
Line::from_iter(["Owner: ".gray(), owner]),
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
use crate::app::states::skirmish_states::{
|
use crate::app::states::skirmish_states::{
|
||||||
Players,
|
Players,
|
||||||
tasks::Tasks,
|
tasks::Tasks,
|
||||||
units::{MinerUnit, Unit},
|
units::{MinerUnit, Unit},
|
||||||
};
|
};
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
|
static EMPTY_TASKS: Lazy<VecDeque<Tasks>> = Lazy::new(|| VecDeque::new());
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum Units {
|
pub enum Units {
|
||||||
@@ -58,10 +63,14 @@ impl Unit for Units {
|
|||||||
self.u().get_coords()
|
self.u().get_coords()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_tasks(&self) -> String {
|
fn get_tasks(&self) -> &VecDeque<Tasks> {
|
||||||
self.u().get_tasks()
|
self.u().get_tasks()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_tasks_formatted(&self) -> String {
|
||||||
|
self.u().get_tasks_formatted()
|
||||||
|
}
|
||||||
|
|
||||||
fn set_task(&mut self, task: Tasks) {
|
fn set_task(&mut self, task: Tasks) {
|
||||||
self.u_mut().set_task(task);
|
self.u_mut().set_task(task);
|
||||||
}
|
}
|
||||||
@@ -104,8 +113,13 @@ impl Unit for Option<Units> {
|
|||||||
self.as_ref().map_or((0, 0), |u| u.get_coords())
|
self.as_ref().map_or((0, 0), |u| u.get_coords())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_tasks(&self) -> String {
|
fn get_tasks(&self) -> &VecDeque<Tasks> {
|
||||||
self.as_ref().map_or("".to_string(), |u| u.get_tasks())
|
self.as_ref().map_or(&EMPTY_TASKS, |u| u.get_tasks())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_tasks_formatted(&self) -> String {
|
||||||
|
self.as_ref()
|
||||||
|
.map_or("".to_string(), |u| u.get_tasks_formatted())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_task(&mut self, task: Tasks) {
|
fn set_task(&mut self, task: Tasks) {
|
||||||
|
|||||||
Reference in New Issue
Block a user