Add digging tasks and unit coordinate support

Introduce Tasks handling for units, store coordinates in MinerUnit,
expose getters/setters for unit tasks, add logging of marked cells,
and provide cell accessors for mutable unit options.
This commit is contained in:
2026-05-12 13:42:13 +02:00
parent a3bb9dc6f1
commit f554fa2efc
5 changed files with 70 additions and 7 deletions
+12 -2
View File
@@ -7,16 +7,18 @@ pub struct MinerUnit {
hp: u16,
can_dig: bool,
digging_power: f32,
pub tasks: VecDeque<Tasks>,
coords: (usize, usize),
tasks: VecDeque<Tasks>,
}
impl MinerUnit {
pub fn new(owner: Players) -> Self {
pub fn new(owner: Players, coords: (usize, usize)) -> Self {
Self {
owner,
hp: 100,
can_dig: true,
digging_power: 0.55,
coords,
tasks: VecDeque::new(),
}
}
@@ -50,4 +52,12 @@ impl Unit for MinerUnit {
fn get_digging_power(&self) -> f32 {
self.digging_power
}
fn get_coords(&self) -> (usize, usize) {
self.coords
}
fn set_task(&mut self, task: Tasks) {
self.tasks.push_back(task);
}
}
@@ -1,4 +1,4 @@
use crate::app::states::skirmish_states::Players;
use crate::app::states::skirmish_states::{Players, tasks::Tasks};
use ratatui::{
style::Stylize,
text::{Line, Span},
@@ -23,6 +23,10 @@ pub trait Unit {
fn get_digging_power(&self) -> f32;
fn get_coords(&self) -> (usize, usize);
fn set_task(&mut self, task: Tasks);
fn base_text(&self) -> Vec<Line<'_>> {
let owner: Span<'_> = self.get_owner().get_span();
let hp: u16 = self.get_hp();
@@ -1,5 +1,6 @@
use crate::app::states::skirmish_states::{
Players,
tasks::Tasks,
units::{MinerUnit, Unit},
};
@@ -15,6 +16,13 @@ impl Units {
Self::Miner(m) => m,
}
}
#[inline]
fn u_mut(&mut self) -> &mut dyn Unit {
match self {
Self::Miner(m) => m,
}
}
}
impl Unit for Units {
@@ -45,6 +53,14 @@ impl Unit for Units {
fn get_digging_power(&self) -> f32 {
self.u().get_digging_power()
}
fn get_coords(&self) -> (usize, usize) {
self.u().get_coords()
}
fn set_task(&mut self, task: Tasks) {
self.u_mut().set_task(task);
}
}
impl Unit for Option<Units> {
@@ -79,4 +95,12 @@ impl Unit for Option<Units> {
fn get_digging_power(&self) -> f32 {
self.as_ref().map_or(0.0, |u| u.get_digging_power())
}
fn get_coords(&self) -> (usize, usize) {
self.as_ref().map_or((0, 0), |u| u.get_coords())
}
fn set_task(&mut self, task: Tasks) {
self.as_mut().map_or((), |u| u.set_task(task))
}
}