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
+21 -4
View File
@@ -5,11 +5,13 @@ use crate::app::{
skirmish_states::{ skirmish_states::{
CellSizes, MarkedCells, MoveFocusedCell, Players, ZoomLevel, CellSizes, MarkedCells, MoveFocusedCell, Players, ZoomLevel,
structures::{BaseBuilding, Ore, Stone, Structures}, structures::{BaseBuilding, Ore, Stone, Structures},
units::{MinerUnit, Units}, tasks::{DiggingTask, Tasks},
units::{MinerUnit, Unit, Units},
}, },
}, },
widgets::CellWidget, widgets::CellWidget,
}; };
use log::info;
use ratatui::layout::Rect; use ratatui::layout::Rect;
use std::collections::VecDeque; use std::collections::VecDeque;
@@ -87,9 +89,15 @@ impl BoardState {
}; };
let unit: Option<Units> = if player_base { let unit: Option<Units> = if player_base {
Some(Units::Miner(MinerUnit::new(Players::Player))) Some(Units::Miner(MinerUnit::new(
Players::Player,
player_base_coords,
)))
} else if enemy_base { } else if enemy_base {
Some(Units::Miner(MinerUnit::new(Players::Enemy))) Some(Units::Miner(MinerUnit::new(
Players::Enemy,
enemy_base_coords,
)))
} else { } else {
None None
}; };
@@ -161,6 +169,8 @@ impl BoardState {
self.marked_cells self.marked_cells
.marked_cells .marked_cells
.push_back((new_cell.0, new_cell.1)); .push_back((new_cell.0, new_cell.1));
info!("{:?}", self.marked_cells);
} }
pub fn undo_marked_cell(&mut self) { pub fn undo_marked_cell(&mut self) {
@@ -198,8 +208,8 @@ impl BoardState {
let cell: &mut CellWidget = self.get_mut_cell(row, col); let cell: &mut CellWidget = self.get_mut_cell(row, col);
cell.set_marked(true); cell.set_marked(true);
self.marked_cells.selected_unit = cell.get_option_unit();
self.marked_cells.selected_unit = cell.get_option_unit();
self.marked_cells.marked_cells.push_back((row, col)); self.marked_cells.marked_cells.push_back((row, col));
} }
@@ -208,6 +218,13 @@ impl BoardState {
self.get_mut_cell(row, col).set_marked(false); self.get_mut_cell(row, col).set_marked(false);
} }
let (row, col) = self.marked_cells.selected_unit.get_coords();
let task: Tasks = Tasks::Digging(DiggingTask::new(self.marked_cells.marked_cells.clone()));
let unit: &mut Option<Units> = self.get_mut_cell(row, col).get_mut_option_unit();
unit.set_task(task);
self.marked_cells.selected_unit = None;
self.marked_cells.marked_cells.clear(); self.marked_cells.marked_cells.clear();
} }
+12 -2
View File
@@ -7,16 +7,18 @@ pub struct MinerUnit {
hp: u16, hp: u16,
can_dig: bool, can_dig: bool,
digging_power: f32, digging_power: f32,
pub tasks: VecDeque<Tasks>, coords: (usize, usize),
tasks: VecDeque<Tasks>,
} }
impl MinerUnit { impl MinerUnit {
pub fn new(owner: Players) -> Self { pub fn new(owner: Players, coords: (usize, usize)) -> Self {
Self { Self {
owner, owner,
hp: 100, hp: 100,
can_dig: true, can_dig: true,
digging_power: 0.55, digging_power: 0.55,
coords,
tasks: VecDeque::new(), tasks: VecDeque::new(),
} }
} }
@@ -50,4 +52,12 @@ impl Unit for MinerUnit {
fn get_digging_power(&self) -> f32 { fn get_digging_power(&self) -> f32 {
self.digging_power 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::{ use ratatui::{
style::Stylize, style::Stylize,
text::{Line, Span}, text::{Line, Span},
@@ -23,6 +23,10 @@ pub trait Unit {
fn get_digging_power(&self) -> f32; 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<'_>> { 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();
@@ -1,5 +1,6 @@
use crate::app::states::skirmish_states::{ use crate::app::states::skirmish_states::{
Players, Players,
tasks::Tasks,
units::{MinerUnit, Unit}, units::{MinerUnit, Unit},
}; };
@@ -15,6 +16,13 @@ impl Units {
Self::Miner(m) => m, Self::Miner(m) => m,
} }
} }
#[inline]
fn u_mut(&mut self) -> &mut dyn Unit {
match self {
Self::Miner(m) => m,
}
}
} }
impl Unit for Units { impl Unit for Units {
@@ -45,6 +53,14 @@ impl Unit for Units {
fn get_digging_power(&self) -> f32 { fn get_digging_power(&self) -> f32 {
self.u().get_digging_power() 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> { impl Unit for Option<Units> {
@@ -79,4 +95,12 @@ impl Unit for Option<Units> {
fn get_digging_power(&self) -> f32 { fn get_digging_power(&self) -> f32 {
self.as_ref().map_or(0.0, |u| u.get_digging_power()) 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))
}
} }
+8
View File
@@ -60,6 +60,14 @@ impl CellWidget {
self.unit.clone() self.unit.clone()
} }
pub fn get_ref_option_unit(&self) -> &Option<Units> {
&self.unit
}
pub fn get_mut_option_unit(&mut self) -> &mut Option<Units> {
&mut self.unit
}
pub fn set_structure(&mut self, structure: Structures) -> &mut Self { pub fn set_structure(&mut self, structure: Structures) -> &mut Self {
self.structure = structure; self.structure = structure;
self self