diff --git a/src/global_task/mod.rs b/src/global_task/mod.rs index e459849..a49be9c 100644 --- a/src/global_task/mod.rs +++ b/src/global_task/mod.rs @@ -1,7 +1,7 @@ pub mod collect_sheep_in_area; pub mod sheep_escape; -pub mod wolf_attack; pub mod torch_blinking; +pub mod wolf_attack; use bevy::prelude::*; diff --git a/src/global_task/torch_blinking.rs b/src/global_task/torch_blinking.rs index 1b231eb..e1842c4 100644 --- a/src/global_task/torch_blinking.rs +++ b/src/global_task/torch_blinking.rs @@ -1,7 +1,15 @@ use bevy::{prelude::*, transform::commands, utils::hashbrown::HashSet}; -use rand::{Rng, seq::SliceRandom}; - -use crate::{torch::{TorchBase, TorchLight, TORCH_ILLUMINATION, TORCH_BASE_RADIUS}, safe_area::SafeArea, sunday::EpisodeTime, storyteller::{GlobalTask, FailReason}, GameSet, sheep::Sheep, level_ui::TaskText, GameState}; +use rand::{seq::SliceRandom, Rng}; + +use crate::{ + level_ui::TaskText, + safe_area::SafeArea, + sheep::Sheep, + storyteller::{FailReason, GlobalTask}, + sunday::EpisodeTime, + torch::{TorchBase, TorchLight, TORCH_BASE_RADIUS, TORCH_ILLUMINATION}, + GameSet, GameState, +}; pub const BAD_TORCH_COLOR: Color = Color::rgb(1.0, 0.0, 0.0); @@ -9,39 +17,43 @@ pub struct TorchBlinkingPlugin; impl Plugin for TorchBlinkingPlugin { fn build(&self, app: &mut App) { - app. - add_systems(OnEnter(GlobalTask::TorchProblem), start_fire_problems) - .add_systems(Update, ( - apply_deferred, - delight, - update_delight_system, - apply_deferred, - ).chain().run_if(in_state(GlobalTask::TorchProblem)).in_set(GameSet::Playing)); + app.add_systems(OnEnter(GlobalTask::TorchProblem), start_fire_problems) + .add_systems( + Update, + ( + apply_deferred, + delight, + update_delight_system, + apply_deferred, + ) + .chain() + .run_if(in_state(GlobalTask::TorchProblem)) + .in_set(GameSet::Playing), + ); } } - #[derive(Default, Component)] pub struct TorchDelight { - pub be_scared_time : f32, - pub rest_time : f32, - pub change_duration : f32, + pub be_scared_time: f32, + pub rest_time: f32, + pub change_duration: f32, } #[derive(Resource)] pub struct TorchDelightStatus { - pub time_for_mission : f32, - pub start_sheep_count : usize, - pub max_dead_sheep : usize, - pub torches_to_lit : Vec + pub time_for_mission: f32, + pub start_sheep_count: usize, + pub max_dead_sheep: usize, + pub torches_to_lit: Vec, } fn start_fire_problems( mut commands: Commands, - torches : Query<(Entity, &SafeArea), With>, - episode_time : Res, - sheep : Query<&Transform, With>, - mut global_task : ResMut> + torches: Query<(Entity, &SafeArea), With>, + episode_time: Res, + sheep: Query<&Transform, With>, + mut global_task: ResMut>, ) { let torch_count = torches.iter().count(); @@ -51,7 +63,10 @@ fn start_fire_problems( let problem_torhes_count = (torch_count as f32 - 1.0).max(1.0); let change_time = 10.0; - let mut problem_torches = torches.iter().map(|(a,b)| (a,b,0_usize)).collect::>(); + let mut problem_torches = torches + .iter() + .map(|(a, b)| (a, b, 0_usize)) + .collect::>(); for (e, safe_area, count) in problem_torches.iter_mut() { for sheep in sheep.iter() { @@ -61,7 +76,10 @@ fn start_fire_problems( } } - let mut problem_torches = problem_torches.iter().filter(|(_, _, count)| *count > 0).collect::>(); + let mut problem_torches = problem_torches + .iter() + .filter(|(_, _, count)| *count > 0) + .collect::>(); if (problem_torches.len() == 0) { global_task.set(GlobalTask::None); @@ -76,22 +94,23 @@ fn start_fire_problems( let mut sheep_in_torches = 0; for (idx, (e, safe_area, count)) in problem_torches.iter().enumerate() { - commands - .entity(*e) - .insert(TorchDelight { - be_scared_time : idx as f32 * 5.0, - rest_time : change_time, - change_duration : change_time, - }); + commands.entity(*e).insert(TorchDelight { + be_scared_time: idx as f32 * 5.0, + rest_time: change_time, + change_duration: change_time, + }); - sheep_in_torches += *count; + sheep_in_torches += *count; } commands.insert_resource(TorchDelightStatus { - time_for_mission : 40.0, - start_sheep_count : sheep.iter().count(), - max_dead_sheep : (sheep_in_torches / 2).max(10), - torches_to_lit : problem_torches.iter().map(|(e, _, _)| *e).collect::>(), + time_for_mission: 40.0, + start_sheep_count: sheep.iter().count(), + max_dead_sheep: (sheep_in_torches / 2).max(10), + torches_to_lit: problem_torches + .iter() + .map(|(e, _, _)| *e) + .collect::>(), }); info!("Start torch problem with {} torches", problem_torches.len()); @@ -99,21 +118,21 @@ fn start_fire_problems( fn update_delight_system( mut commands: Commands, - mut status : ResMut, - mut texts : Query<&mut Text, With>, - torches : Query<(&TorchBase, Option<&TorchDelight>)>, - time : Res