Gaussian idle
This commit is contained in:
@@ -6,14 +6,15 @@
|
||||
//! 3. On completion/failure, update TaskState, fire event, pop task
|
||||
//! 4. If queue empty after pop, assign default Task::Idle
|
||||
//!
|
||||
//! Uses Changed<TaskQueue> + Changed<TaskState> to minimize queries.
|
||||
//! Uses Changed<TaskQueue> + Changed<TaskState> to minimise queries.
|
||||
|
||||
use crate::constants::ITILE_SIZE;
|
||||
use crate::entities::behaviour::{EntityBehaviourRegistry, EntityType};
|
||||
use crate::entities::shared_components::Ambulatory;
|
||||
use crate::entities::tasks::components::{Task, TaskQueue, TaskState};
|
||||
use crate::entities::tasks::components::{IdleState, Task, TaskQueue, TaskState};
|
||||
use crate::entities::tasks::events::{TaskClaimed, TaskCompleted, TaskFailed};
|
||||
use crate::entities::tasks::idle::execute_idle;
|
||||
use crate::world::tiles::tilemap::TileMap;
|
||||
use crate::world::chunks::ChunkMap;
|
||||
use crate::world::tiles::TileMap;
|
||||
use bevy::prelude::*;
|
||||
use bevy_rand::prelude::*;
|
||||
use rand::{RngExt, SeedableRng};
|
||||
@@ -22,13 +23,18 @@ use rand::{RngExt, SeedableRng};
|
||||
pub fn task_executor_system(
|
||||
mut commands: Commands,
|
||||
tilemap: Res<TileMap>,
|
||||
chunk_map: Res<ChunkMap>,
|
||||
behaviour_registry: Res<EntityBehaviourRegistry>,
|
||||
mut rng_q: Query<&mut WyRand, With<GlobalRng>>,
|
||||
mut tick: Local<u32>,
|
||||
mut query: Query<(
|
||||
Entity,
|
||||
&mut TaskQueue,
|
||||
&mut TaskState,
|
||||
&mut Ambulatory,
|
||||
&Transform,
|
||||
&mut Sprite,
|
||||
&EntityType,
|
||||
)>,
|
||||
mut claimed_writer: MessageWriter<TaskClaimed>,
|
||||
mut completed_writer: MessageWriter<TaskCompleted>,
|
||||
@@ -38,14 +44,21 @@ pub fn task_executor_system(
|
||||
return;
|
||||
};
|
||||
|
||||
for (entity, mut queue, mut state, mut ambulatory, transform) in query.iter_mut() {
|
||||
// If queue empty, assign default Idle task
|
||||
*tick = tick.wrapping_add(1);
|
||||
let current_tick = *tick;
|
||||
|
||||
for (entity, mut queue, mut state, mut ambulatory, transform, mut sprite, entity_type) in
|
||||
query.iter_mut()
|
||||
{
|
||||
let origin = transform.translation.as_ivec3();
|
||||
let behaviour = EntityBehaviourRegistry::global_get(entity_type.0);
|
||||
|
||||
// If queue empty, assign Idle with Gaussian params from TOML
|
||||
if queue.is_empty() {
|
||||
let origin = transform.translation.as_ivec3();
|
||||
queue.push(Task::Idle {
|
||||
target: origin,
|
||||
wander_radius: 64,
|
||||
origin,
|
||||
sigma_world: behaviour.idle.sigma_world,
|
||||
state: IdleState::Picking,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -66,20 +79,22 @@ pub fn task_executor_system(
|
||||
match current_task {
|
||||
Task::Idle { .. } => {
|
||||
execute_idle(
|
||||
entity,
|
||||
current_task,
|
||||
transform,
|
||||
&mut ambulatory,
|
||||
&mut sprite,
|
||||
&tilemap,
|
||||
&chunk_map,
|
||||
&behaviour.idle,
|
||||
&mut rng,
|
||||
current_tick,
|
||||
);
|
||||
}
|
||||
Task::GoTo {
|
||||
target,
|
||||
threshold_tiles,
|
||||
} => {
|
||||
let entity_pos = (transform.translation / ITILE_SIZE as f32).as_ivec3();
|
||||
// Chebyshev distance (max of absolute differences)
|
||||
let entity_pos = (transform.translation / 16.0f32).as_ivec3();
|
||||
let distance = (entity_pos.x - target.x)
|
||||
.abs()
|
||||
.max((entity_pos.y - target.y).abs());
|
||||
@@ -87,7 +102,6 @@ pub fn task_executor_system(
|
||||
if distance <= *threshold_tiles || !tilemap.is_standable(*target) {
|
||||
*state = TaskState::Completed;
|
||||
} else {
|
||||
// Set target - pathfinding handles the rest
|
||||
ambulatory.target = Some(Vec3::new(
|
||||
target.x as f32,
|
||||
target.y as f32,
|
||||
@@ -96,7 +110,6 @@ pub fn task_executor_system(
|
||||
ambulatory.current_path = None;
|
||||
}
|
||||
}
|
||||
// Stage 3 placeholders - log and fail for now
|
||||
Task::ChopTree { .. } | Task::HaulObject { .. } | Task::DropHauled { .. } => {
|
||||
debug!(
|
||||
"Task {} unimplemented for entity {:?}",
|
||||
@@ -125,13 +138,12 @@ pub fn task_executor_system(
|
||||
});
|
||||
}
|
||||
|
||||
// If terminal task completed, ensure Idle is queued
|
||||
// If terminal task completed, push Idle with Gaussian params
|
||||
if completed_task.is_terminal() && queue.is_empty() {
|
||||
let origin = (transform.translation / ITILE_SIZE as f32).as_ivec3();
|
||||
queue.push(Task::Idle {
|
||||
target: origin,
|
||||
wander_radius: 64,
|
||||
origin,
|
||||
sigma_world: behaviour.idle.sigma_world,
|
||||
state: IdleState::Picking,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user