Files
pomomon-garden/src/features/pom/actions.rs
2025-12-02 14:02:10 +01:00

95 lines
2.8 KiB
Rust

use crate::prelude::*;
#[derive(Clone, Debug, PartialEq)]
pub enum InteractionAction {
Plant(ItemType),
}
impl InteractionAction {
pub fn get_name(&self, game_config: &GameConfig) -> String {
match self {
InteractionAction::Plant(item) => format!("Pflanze {}", item.singular(game_config)),
}
}
pub fn get_sprite(
&self,
asset_server: &Res<AssetServer>,
game_config: &GameConfig,
) -> Option<AseSlice> {
match self {
InteractionAction::Plant(item) => Some(item.get_sprite(asset_server, game_config)),
}
}
pub fn list_options(
tile_state: &TileState,
inventory: &Inventory,
item_query: Query<&ItemStack>,
) -> Vec<InteractionAction> {
let mut options: Vec<InteractionAction> = vec![];
for &entity in &inventory.items {
let Ok(stack) = item_query.get(entity) else {
continue;
};
if stack.amount <= 0 {
continue;
}
match tile_state {
TileState::Empty => match &stack.item_type {
ItemType::BerrySeed { .. } => {
options.push(InteractionAction::Plant(stack.item_type.clone()));
}
_ => (),
},
_ => (),
}
}
options
}
pub fn execute(
&self,
pos: (u32, u32),
grid: &Grid,
tile_query: &mut Query<&mut TileState>,
inventory: &mut Inventory,
item_stack_query: &mut Query<&mut ItemStack>,
commands: &mut Commands,
) {
let Ok(tile_entity) = grid.get_tile(pos) else {
println!("Error during interaction: Couldn't get tile_entity");
return;
};
let Ok(mut tile_state) = tile_query.get_mut(tile_entity) else {
println!("Error during interaction: Couldn't get mut tile_state");
return;
};
match self {
InteractionAction::Plant(seed_type) => {
if let TileState::Empty = *tile_state {
if inventory.update_item_stack(
commands,
item_stack_query,
seed_type.clone(),
-1,
) {
println!("Planting {:?}", seed_type);
*tile_state = TileState::Occupied {
seed: seed_type.clone(),
};
} else {
println!("No {:?} in inventory!", seed_type);
}
} else {
println!("Tile is not empty, cannot plant.");
}
}
}
}
}