generated from GarandPLG/rust-flake-template
Add progress tracking to tasks and turn advancement
- Extend the `Task` trait with methods for progress handling and completion. - Update `DiggingTask` to store `progress` and `tick_increase`, and implement the new trait methods. - Implement the new methods in the `Tasks` enum, adding a mutable accessor. - Introduce `BoardState::get_active_tasks` and `BoardState::advance_turn` to iterate over active tasks and update their progress each turn. - Un-comment the call to `board.advance_turn()` in `SkirmishState::tick_update`.
This commit is contained in:
@@ -11,7 +11,7 @@ pub struct SkirmishState {
|
||||
|
||||
impl SkirmishState {
|
||||
pub fn tick_update(&mut self) {
|
||||
// self.board.advance_turn();
|
||||
self.board.advance_turn();
|
||||
|
||||
// if self.board.is_victory() {}
|
||||
|
||||
|
||||
@@ -211,6 +211,7 @@ impl BoardState {
|
||||
}
|
||||
|
||||
pub fn end_marking_cells(&mut self, del: bool) {
|
||||
// let todo_taks_tyle = Tasks::Digging(DiggingTask::);
|
||||
for (row, col) in self.marked_cells.marked_cells.clone() {
|
||||
self.get_mut_cell(row, col).set_marked(false);
|
||||
}
|
||||
@@ -218,8 +219,10 @@ impl BoardState {
|
||||
let (row, col) = self.marked_cells.selected_unit.get_coords();
|
||||
|
||||
if self.marked_cells.marked_cells.len() > 1 && !del {
|
||||
let task: Tasks =
|
||||
Tasks::Digging(DiggingTask::new(self.marked_cells.marked_cells.clone()));
|
||||
let path: VecDeque<(usize, usize)> = self.marked_cells.marked_cells.clone();
|
||||
let tick_increase: f32 = self.marked_cells.selected_unit.get_digging_power();
|
||||
|
||||
let task: Tasks = Tasks::Digging(DiggingTask::new(path, tick_increase));
|
||||
|
||||
self.get_mut_cell(row, col)
|
||||
.get_mut_option_unit()
|
||||
@@ -314,4 +317,22 @@ impl BoardState {
|
||||
self.push_marked_cell(new_cell);
|
||||
}
|
||||
}
|
||||
|
||||
fn get_active_tasks(&self) -> Vec<&Tasks> {
|
||||
self.cells
|
||||
.iter()
|
||||
.flat_map(|row| row.iter())
|
||||
.filter_map(|cell| {
|
||||
cell.get_ref_option_unit()
|
||||
.as_ref()
|
||||
.and_then(|unit| unit.get_tasks().front())
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn advance_turn(&mut self) {
|
||||
for task in self.get_active_tasks() {
|
||||
// task.progress()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use crate::app::states::skirmish_states::tasks::Task;
|
||||
use std::{collections::VecDeque, ops::Add};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct DiggingTask {
|
||||
path: VecDeque<(usize, usize)>,
|
||||
progress: f32,
|
||||
tick_increase: f32,
|
||||
}
|
||||
|
||||
impl DiggingTask {
|
||||
pub fn new(path: VecDeque<(usize, usize)>) -> Self {
|
||||
Self { path }
|
||||
pub fn new(path: VecDeque<(usize, usize)>, tick_increase: f32) -> Self {
|
||||
Self {
|
||||
path,
|
||||
progress: 0.0,
|
||||
tick_increase,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,4 +34,16 @@ impl Task for DiggingTask {
|
||||
fn get_path_back_coords(&self) -> &(usize, usize) {
|
||||
self.path.back().map_or(&(0, 0), |c| c)
|
||||
}
|
||||
|
||||
fn get_progress(&self) -> f32 {
|
||||
self.progress
|
||||
}
|
||||
|
||||
fn progress(&mut self) {
|
||||
let _ = self.progress.add(self.tick_increase).max(0.0).min(100.0);
|
||||
}
|
||||
|
||||
fn is_finished(&self) -> bool {
|
||||
self.progress >= 100.0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,4 +8,10 @@ pub trait Task {
|
||||
fn get_path_front_coords(&self) -> &(usize, usize);
|
||||
|
||||
fn get_path_back_coords(&self) -> &(usize, usize);
|
||||
|
||||
fn get_progress(&self) -> f32;
|
||||
|
||||
fn progress(&mut self);
|
||||
|
||||
fn is_finished(&self) -> bool;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,13 @@ impl Tasks {
|
||||
Self::Digging(d) => d,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn t_mut(&mut self) -> &mut dyn Task {
|
||||
match self {
|
||||
Self::Digging(d) => d,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Task for Tasks {
|
||||
@@ -31,4 +38,16 @@ impl Task for Tasks {
|
||||
fn get_path_back_coords(&self) -> &(usize, usize) {
|
||||
self.t().get_path_back_coords()
|
||||
}
|
||||
|
||||
fn get_progress(&self) -> f32 {
|
||||
self.t().get_progress()
|
||||
}
|
||||
|
||||
fn is_finished(&self) -> bool {
|
||||
self.t().is_finished()
|
||||
}
|
||||
|
||||
fn progress(&mut self) {
|
||||
self.t_mut().progress();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user