use crate::config::GameConfig; use crate::constants::TILE_SIZE; use crate::constants::*; use crate::entities::item::{spawn_prefab, MiscPrefab}; use crate::entities::shared_components::Ambulatory; use crate::game::SpawnDelay; use crate::world::tiles::tilemap; use crate::world::VisibleGameEntity; use bevy::prelude::*; use bevy_rand::prelude::*; use rand::RngExt; #[derive(Bundle)] pub struct Pig { ambulatory: Ambulatory, sprite: Sprite, transform: Transform, visibility: Visibility, drop_timer: PigDropTimer, } impl Pig { pub fn new(asset_server: &Res, position: Vec3) -> Self { Pig { ambulatory: Ambulatory { walk_speed: 25., run_speed: 6., target: None, current_path: None, path_index: 0, step_recovery: 0, validation_cooldown: 0, move_direction: Vec2::ZERO, step_history: [0i16; 4], }, sprite: Sprite { image: asset_server.load("pig.png"), ..Default::default() }, transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)), visibility: Visibility::Hidden, drop_timer: PigDropTimer(Timer::from_seconds(5.0, TimerMode::Repeating)), } } } #[derive(Component, Deref, DerefMut)] pub struct PigDropTimer(pub Timer); pub fn spawn_pigs( mut commands: Commands, asset_server: Res, mut rng_q: Query<&mut WyRand, With>, config: Res, mut delay: ResMut, mut has_spawned: Local, ) { if *has_spawned { return; } if delay.0 < 60 { delay.0 += 1; return; } *has_spawned = true; if let Ok(mut rng) = rng_q.single_mut() { for _ in 0..config.spawn_counts.pigs { let raw_x = rng.random_range(-32.0f32..32.0f32); let raw_y = rng.random_range(-32.0f32..32.0f32); let grid_x = (raw_x / TILE_SIZE).round() * TILE_SIZE; let grid_y = (raw_y / TILE_SIZE).round() * TILE_SIZE; let grid_z = 35.0 * TILE_SIZE; let pig = commands .spawn(Pig::new(&asset_server, Vec3::new(grid_x, grid_y, grid_z))) .id(); commands.entity(pig).insert(VisibleGameEntity); } } } pub fn pig_drop_system( mut commands: Commands, asset_server: Res, time: Res