129 lines
3.8 KiB
Rust
129 lines
3.8 KiB
Rust
use bevy::ecs::system::RunSystemOnce;
|
|
use pomomon_garden::features::config::components::GameConfig;
|
|
use pomomon_garden::features::grid::components::{Grid, TileState};
|
|
use pomomon_garden::features::inventory::components::{Inventory, ItemStack, ItemType};
|
|
use pomomon_garden::features::pom::actions::InteractionAction;
|
|
use pomomon_garden::prelude::*;
|
|
|
|
mod common;
|
|
use common::setup_app;
|
|
|
|
#[test]
|
|
fn test_plant_seed_interaction() {
|
|
let seed_type = ItemType::BerrySeed {
|
|
name: "TestSeed".into(),
|
|
};
|
|
let mut app = setup_app(
|
|
3,
|
|
3,
|
|
&[(1, 1, TileState::Empty)],
|
|
vec![(seed_type.clone(), 1)],
|
|
None,
|
|
);
|
|
|
|
let _ = app.world_mut().run_system_once(
|
|
move |grid: Res<Grid>,
|
|
mut tile_query: Query<&mut TileState>,
|
|
mut inventory: ResMut<Inventory>,
|
|
mut item_stack_query: Query<&mut ItemStack>,
|
|
mut commands: Commands,
|
|
game_config: Res<GameConfig>| {
|
|
let action = InteractionAction::Plant(seed_type.clone());
|
|
action.execute(
|
|
(1, 1),
|
|
&grid,
|
|
&mut tile_query,
|
|
&mut inventory,
|
|
&mut item_stack_query,
|
|
&mut commands,
|
|
&game_config,
|
|
);
|
|
},
|
|
);
|
|
|
|
// Apply commands (despawns etc.)
|
|
app.update();
|
|
|
|
// Assert Tile State
|
|
let grid = app.world().resource::<Grid>();
|
|
let tile_entity = grid.get_tile((1, 1)).unwrap();
|
|
let tile_state = app.world().entity(tile_entity).get::<TileState>().unwrap();
|
|
|
|
if let TileState::Occupied { seed, .. } = tile_state {
|
|
assert_eq!(
|
|
*seed,
|
|
ItemType::BerrySeed {
|
|
name: "TestSeed".into()
|
|
}
|
|
);
|
|
} else {
|
|
panic!("Tile should be Occupied with seed, found {:?}", tile_state);
|
|
}
|
|
|
|
// Assert Inventory Empty
|
|
let inventory = app.world().resource::<Inventory>();
|
|
// Item stack entity should be despawned or amount 0
|
|
if !inventory.items.is_empty() {
|
|
// If the entity still exists, check amount
|
|
if let Some(entity) = inventory.items.first() {
|
|
if let Some(stack) = app.world().entity(*entity).get::<ItemStack>() {
|
|
assert_eq!(stack.amount, 0, "Item amount should be 0");
|
|
} else {
|
|
panic!(
|
|
"Inventory items list should be empty or point to valid 0 amount entities. Found phantom entity."
|
|
);
|
|
}
|
|
}
|
|
} else {
|
|
assert!(inventory.items.is_empty());
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_plant_seed_no_inventory() {
|
|
let seed_type = ItemType::BerrySeed {
|
|
name: "TestSeed".into(),
|
|
};
|
|
let mut app = setup_app(
|
|
3,
|
|
3,
|
|
&[(1, 1, TileState::Empty)],
|
|
vec![], // Empty inventory
|
|
None,
|
|
);
|
|
|
|
let _ = app.world_mut().run_system_once(
|
|
move |grid: Res<Grid>,
|
|
mut tile_query: Query<&mut TileState>,
|
|
mut inventory: ResMut<Inventory>,
|
|
mut item_stack_query: Query<&mut ItemStack>,
|
|
mut commands: Commands,
|
|
game_config: Res<GameConfig>| {
|
|
let action = InteractionAction::Plant(seed_type.clone());
|
|
action.execute(
|
|
(1, 1),
|
|
&grid,
|
|
&mut tile_query,
|
|
&mut inventory,
|
|
&mut item_stack_query,
|
|
&mut commands,
|
|
&game_config,
|
|
);
|
|
},
|
|
);
|
|
|
|
app.update();
|
|
|
|
// Assert Tile State Unchanged
|
|
let grid = app.world().resource::<Grid>();
|
|
let tile_entity = grid.get_tile((1, 1)).unwrap();
|
|
let tile_state = app.world().entity(tile_entity).get::<TileState>().unwrap();
|
|
|
|
if let TileState::Empty = tile_state {
|
|
// Correct
|
|
} else {
|
|
panic!("Tile should remain Empty, found {:?}", tile_state);
|
|
}
|
|
}
|
|
|