diff --git a/assets/sprites/anthill.png b/assets/sprites/anthill.png new file mode 100644 index 0000000..822f273 Binary files /dev/null and b/assets/sprites/anthill.png differ diff --git a/src/components/anthill.rs b/src/components/anthill.rs new file mode 100644 index 0000000..475faa0 --- /dev/null +++ b/src/components/anthill.rs @@ -0,0 +1,45 @@ +use bevy::prelude::*; + +#[derive(Bundle)] +#[allow(clippy::module_name_repetitions)] +pub struct AnthillBundle { + pub capacity: AntCapacity, + pub group_size: GroupSize, + pub spawn_timer: SpawnTimer, + pub sprite: SpriteBundle, +} + +#[derive(Component)] +pub struct AntCapacity(pub u32); + +#[derive(Component)] +pub struct GroupSize(pub u32); + +#[derive(Component)] +pub struct SpawnTimer(pub Timer); + +impl AnthillBundle { + pub fn new( + ant_count: u32, + group_size: u32, + period: f32, + position: Vec2, + scale: Vec2, + texture: Handle, + ) -> Self { + Self { + capacity: AntCapacity(ant_count), + group_size: GroupSize(group_size), + spawn_timer: SpawnTimer(Timer::from_seconds(period, TimerMode::Repeating)), + sprite: SpriteBundle { + transform: Transform { + translation: position.extend(1.), // Make sure the anthill occludes the ants + scale: scale.extend(1.), + ..default() + }, + texture, + ..default() + }, + } + } +} diff --git a/src/components/mod.rs b/src/components/mod.rs index 0034e54..e8f6cdd 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -1,2 +1,3 @@ pub mod ant; +pub mod anthill; pub mod common; diff --git a/src/main.rs b/src/main.rs index f17eb30..aff9f53 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ use systems::{ animation_system, position_update_system, randomized_velocity_change_system, randomized_velocity_system, wall_avoidance_system, }, + anthill::leave_anthill_system, startup::{hello_ants_system, setup_system}, }; @@ -16,9 +17,9 @@ use systems::{ const MIN_POSITION: Vec2 = Vec2::splat(-450.); const MAX_POSITION: Vec2 = Vec2::splat(450.); -const ANT_COUNT: u32 = 200; +const ANT_COUNT: u32 = 20; const ANT_SPEED: f32 = 0.5; -const ANT_ANIMATION_SPEED: f32 = 1. / 62.; +const ANT_ANIMATION_SPEED: f32 = 1. / 82.; const ANT_SCALE: f32 = 0.15; const VELOCITY_CHANGE_SCALE: f32 = PI / 180. * 1.; // Single degree @@ -53,6 +54,7 @@ fn main() { app.add_systems( FixedUpdate, ( + leave_anthill_system, randomized_velocity_change_system, randomized_velocity_system, wall_avoidance_system, diff --git a/src/systems/anthill.rs b/src/systems/anthill.rs new file mode 100644 index 0000000..ef17e1d --- /dev/null +++ b/src/systems/anthill.rs @@ -0,0 +1,72 @@ +use bevy::prelude::*; +use rand::Rng; + +use crate::{ + components::{ + ant::AntBundle, + anthill::{AntCapacity, GroupSize, SpawnTimer}, + common::AnimationIndices, + }, + ANT_COUNT, ANT_SCALE, +}; + +#[allow(clippy::needless_pass_by_value)] +pub fn leave_anthill_system( + mut query: Query<(&mut AntCapacity, &GroupSize, &mut SpawnTimer)>, + mut commands: Commands, + time: Res