diff --git a/src/initializations/startup.rs b/src/initializations/startup.rs index c943b11..24b07ed 100644 --- a/src/initializations/startup.rs +++ b/src/initializations/startup.rs @@ -84,6 +84,7 @@ pub fn startup( .insert(position) .insert(position.to_transform_layer(0.5)) .insert(Plant { growth, plant_type }) + .insert( Object { itemtype: plant_type, ..default() } ) .id() ; if plant_type.is_forageable().0.is_some() && growth > 0.5 { diff --git a/src/task_system/personality.rs b/src/task_system/personality.rs index 9433d0b..15d95ce 100644 --- a/src/task_system/personality.rs +++ b/src/task_system/personality.rs @@ -46,7 +46,7 @@ pub fn personalities( territorial::territorial(entity, brain, physical_body, position, nest, &potential_targets); }, Some(PersonalityTrait::Human) => { - human::human(entity, brain, physical_body, position, nest, &objects, &already_targeted, &obstacles, &tiletypes); + human::human(entity, brain, physical_body, position, &objects, &already_targeted, &obstacles, &tiletypes); }, _ => { nopersonality::nopersonality(entity, brain, physical_body, position, nest); diff --git a/src/task_system/personality/human.rs b/src/task_system/personality/human.rs index b460623..bd1ef86 100644 --- a/src/task_system/personality/human.rs +++ b/src/task_system/personality/human.rs @@ -5,7 +5,6 @@ pub fn human( mut brain: Mut, mut physical_body: Mut, position: &Position, - nest: Option<&Nest>, potential_targets: &Query<(Entity, Option<&Object>, Option<&Zone>, Option<&WorkTarget>, &Position)>, already_targeted: &Vec, obstacles: &std::collections::HashSet, @@ -19,28 +18,30 @@ pub fn human( .map(|(_, _, _, _, position)| *position) .collect(); // Anything to DO? + let mut i = 0; for (target_entity, target_object, target_zone, target_worktarget, target_position) in potential_targets.iter() { + // i += 1; + // println!("Target {} of {}", i, potential_targets.iter().count()); + if already_targeted.contains(&target_entity) { continue; } if target_entity == entity { continue; } let distance = position.distance(&target_position); - if distance > 100 { continue; } - if target_object.is_none() && target_zone.is_none() { continue; } - if ! crate::is_position_reachable(position, target_position, obstacles, tiletypes) { continue; } - + if distance > 50 { continue; } + let is_reachable = || crate::is_position_reachable(position, target_position, obstacles, tiletypes); + // TARGET OBJECTS if target_object.is_some() { let target_object = target_object.unwrap(); - if target_object.itemtype.is_choppable().0.is_some() && target_worktarget.is_some() { - brain.task = Some(Task::Chop); - return; - } - if target_object.itemtype.is_forageable().0.is_some() { - brain.task = Some(Task::Forage); - return; + if target_worktarget.is_some() { + if target_object.itemtype.is_choppable().0.is_some() { + if set_task(&mut brain, Task::Chop, is_reachable) { return; } + } + if target_object.itemtype.is_forageable().0.is_some() { + if set_task(&mut brain, Task::Forage, is_reachable) { return; } + } } if target_object.under_construction { - brain.task = Some(Task::Construct); - return; + if set_task(&mut brain, Task::Construct, is_reachable) { return; } } // if target_object.itemtype.is_harvestable().0.is_some() { // brain.task = Some(Task::Harvest); @@ -52,18 +53,24 @@ pub fn human( let target_zone = target_zone.unwrap(); if target_zone.zone_type == ZoneType::Farm { if ! object_positions.contains(&target_position) { // Ensure nothing is already planted there. - println!("Planting!"); - brain.task = Some(Task::Plant); - return; + if set_task(&mut brain, Task::Plant, is_reachable) { return; } } } if target_zone.zone_type == ZoneType::Construction { - brain.task = Some(Task::Construct); - return; + if set_task(&mut brain, Task::Construct, is_reachable) { return; } } } - } brain.motivation = Some(Motivation::Meander); - brain.task = None; + brain.task = Some(Task::Meander); +} + +fn set_task( + brain: &mut Mut, + task: Task, + is_reachable: impl FnOnce() -> bool, +) -> bool { + if !is_reachable() { return false; } + brain.task = Some(task); + return true; } \ No newline at end of file diff --git a/src/thinking_system.rs b/src/thinking_system.rs index 3595f37..1b2ae74 100644 --- a/src/thinking_system.rs +++ b/src/thinking_system.rs @@ -14,7 +14,7 @@ impl Plugin for ThinkingPlugin { .run_if(in_state(GameState::InGame)) , remotivate_system - .run_if(bevy::time::common_conditions::on_timer(bevy::utils::Duration::from_secs_f32(0.5))) + .run_if(bevy::time::common_conditions::on_timer(bevy::utils::Duration::from_secs_f32(2.5))) .run_if(in_state(GameState::InGame)) ) )