feat: Implement crop harvesting mechanics

This commit is contained in:
demenik
2025-12-03 22:33:36 +01:00
parent 5367971e1d
commit 8904177869
3 changed files with 61 additions and 4 deletions

View File

@@ -4,6 +4,7 @@ use crate::prelude::*;
pub enum InteractionAction {
Plant(ItemType),
Water,
Harvest,
}
impl InteractionAction {
@@ -11,6 +12,7 @@ impl InteractionAction {
match self {
InteractionAction::Plant(item) => format!("Pflanze {}", item.singular(game_config)),
InteractionAction::Water => "Gießen".into(),
InteractionAction::Harvest => "Ernten".into(),
}
}
@@ -22,6 +24,7 @@ impl InteractionAction {
match self {
InteractionAction::Plant(item) => Some(item.get_sprite(asset_server, game_config)),
InteractionAction::Water => None,
InteractionAction::Harvest => Some(ItemType::Berry.get_sprite(asset_server, game_config)),
}
}
@@ -29,13 +32,28 @@ impl InteractionAction {
tile_state: &TileState,
inventory: &Inventory,
item_query: Query<&ItemStack>,
game_config: &GameConfig,
) -> Vec<InteractionAction> {
let mut options: Vec<InteractionAction> = vec![];
match tile_state {
TileState::Occupied { watered, .. } => {
if !*watered {
options.push(InteractionAction::Water);
TileState::Occupied {
watered,
withered,
seed,
growth_stage,
..
} => {
if *withered {
options.push(InteractionAction::Harvest);
} else if let ItemType::BerrySeed { name } = seed {
if let Some(config) = game_config.berry_seeds.iter().find(|s| s.name == *name) {
if *growth_stage >= config.growth_stages {
options.push(InteractionAction::Harvest);
} else if !*watered {
options.push(InteractionAction::Water);
}
}
}
}
TileState::Empty => {
@@ -69,6 +87,7 @@ impl InteractionAction {
inventory: &mut Inventory,
item_stack_query: &mut Query<&mut ItemStack>,
commands: &mut Commands,
game_config: &GameConfig,
) {
let Ok(tile_entity) = grid.get_tile(pos) else {
println!("Error during interaction: Couldn't get tile_entity");
@@ -123,6 +142,41 @@ impl InteractionAction {
println!("Tile is not occupied, cannot water.");
}
}
InteractionAction::Harvest => {
if let TileState::Occupied {
seed,
withered,
growth_stage,
..
} = &*tile_state
{
let mut can_harvest = *withered;
if !can_harvest {
if let ItemType::BerrySeed { name } = seed {
if let Some(config) =
game_config.berry_seeds.iter().find(|s| s.name == *name)
{
if *growth_stage >= config.growth_stages {
can_harvest = true;
inventory.update_item_stack(
commands,
item_stack_query,
ItemType::Berry,
config.grants as i32,
);
}
}
}
}
if can_harvest {
*tile_state = TileState::Empty;
} else {
println!("Cannot harvest: not withered and not fully grown.");
}
}
}
}
}
}

View File

@@ -191,6 +191,7 @@ fn perform_interaction(
mut inventory: ResMut<Inventory>,
mut item_stack_query: Query<&mut ItemStack>,
mut commands: Commands,
config: Res<GameConfig>,
) {
for (pos, mut target_component, path_queue) in pom_query.iter_mut() {
if let Some(target) = target_component.target {
@@ -213,6 +214,7 @@ fn perform_interaction(
&mut inventory,
&mut item_stack_query,
&mut commands,
&config,
);
}
}

View File

@@ -53,7 +53,8 @@ pub fn spawn_context_menu(
let Ok(tile_state) = tile_query.get(tile_entity) else {
return;
};
let options = InteractionAction::list_options(tile_state, &inventory, item_query);
let options =
InteractionAction::list_options(tile_state, &inventory, item_query, &game_config);
commands
.spawn((