feat: Implement crop withering mechanics (#28)

This commit is contained in:
demenik
2025-12-02 19:05:11 +01:00
parent 6b28ce7ab1
commit d2db146eb6
7 changed files with 43 additions and 2 deletions

View File

@@ -22,6 +22,10 @@ pub enum TileState {
seed: ItemType, seed: ItemType,
watered: bool, watered: bool,
growth_stage: u32, growth_stage: u32,
#[serde(default)]
withered: bool,
#[serde(default)]
dry_counter: u8,
}, },
} }

View File

@@ -127,6 +127,8 @@ fn debug_click(
}, },
watered: false, watered: false,
growth_stage: 0, growth_stage: 0,
withered: false,
dry_counter: 0,
}, },
TileState::Occupied { .. } => TileState::Unclaimed, TileState::Occupied { .. } => TileState::Unclaimed,
}, },

View File

@@ -176,21 +176,34 @@ pub fn handle_continue(
seed, seed,
watered, watered,
growth_stage, growth_stage,
withered,
dry_counter,
} = &*state } = &*state
{ {
let mut new_stage = *growth_stage; let mut new_stage = *growth_stage;
let mut new_withered = *withered;
let mut new_dry_counter = *dry_counter;
if *watered { if *watered {
new_dry_counter = 0;
if let Some(config) = seed.get_seed_config(&game_config) { if let Some(config) = seed.get_seed_config(&game_config) {
if new_stage < config.growth_stages { if new_stage < config.growth_stages && !new_withered {
new_stage += 1; new_stage += 1;
} }
} }
} else {
new_dry_counter += 1;
if new_dry_counter >= 2 {
new_withered = true;
}
} }
*state = TileState::Occupied { *state = TileState::Occupied {
seed: seed.clone(), seed: seed.clone(),
watered: false, watered: false,
growth_stage: new_stage, growth_stage: new_stage,
withered: new_withered,
dry_counter: new_dry_counter,
}; };
} }
} }

View File

@@ -93,6 +93,8 @@ impl InteractionAction {
seed: seed_type.clone(), seed: seed_type.clone(),
watered: false, watered: false,
growth_stage: 0, growth_stage: 0,
withered: false,
dry_counter: 0,
}; };
} else { } else {
println!("No {:?} in inventory!", seed_type); println!("No {:?} in inventory!", seed_type);
@@ -102,12 +104,20 @@ impl InteractionAction {
} }
} }
InteractionAction::Water => { InteractionAction::Water => {
if let TileState::Occupied { seed, growth_stage, .. } = &*tile_state { if let TileState::Occupied {
seed,
growth_stage,
withered,
..
} = &*tile_state
{
println!("Watering {:?}", seed); println!("Watering {:?}", seed);
*tile_state = TileState::Occupied { *tile_state = TileState::Occupied {
seed: seed.clone(), seed: seed.clone(),
watered: true, watered: true,
growth_stage: *growth_stage, growth_stage: *growth_stage,
withered: *withered,
dry_counter: 0,
}; };
} else { } else {
println!("Tile is not occupied, cannot water."); println!("Tile is not occupied, cannot water.");

View File

@@ -86,6 +86,8 @@ fn test_crop_growth_logic() {
seed: seed_type.clone(), seed: seed_type.clone(),
watered: true, watered: true,
growth_stage: 0, growth_stage: 0,
withered: false,
dry_counter: 0,
}, },
), ),
( (
@@ -95,6 +97,8 @@ fn test_crop_growth_logic() {
seed: seed_type.clone(), seed: seed_type.clone(),
watered: false, watered: false,
growth_stage: 0, growth_stage: 0,
withered: false,
dry_counter: 0,
}, },
), ),
( (
@@ -104,6 +108,8 @@ fn test_crop_growth_logic() {
seed: seed_type.clone(), seed: seed_type.clone(),
watered: true, watered: true,
growth_stage: 2, // Max growth_stage: 2, // Max
withered: false,
dry_counter: 0,
}, },
), ),
]; ];

View File

@@ -180,6 +180,8 @@ fn test_water_crop() {
seed: seed_type.clone(), seed: seed_type.clone(),
watered: false, watered: false,
growth_stage: 0, growth_stage: 0,
withered: false,
dry_counter: 0,
}, },
)], )],
vec![], vec![],

View File

@@ -95,6 +95,8 @@ fn test_find_path_around_obstacle() {
}, },
watered: false, watered: false,
growth_stage: 0, growth_stage: 0,
withered: false,
dry_counter: 0,
}; };
let obstacles = vec![ let obstacles = vec![
(2, 2, obstacle.clone()), (2, 2, obstacle.clone()),
@@ -148,6 +150,8 @@ fn test_find_path_no_path() {
}, },
watered: false, watered: false,
growth_stage: 0, growth_stage: 0,
withered: false,
dry_counter: 0,
}; };
let obstacles = vec![ let obstacles = vec![
(2, 0, obstacle.clone()), (2, 0, obstacle.clone()),