Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
rewin123 committed Dec 10, 2023
1 parent 98abb5b commit 2ef8e19
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/global_task/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down
138 changes: 79 additions & 59 deletions src/global_task/torch_blinking.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,59 @@
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);

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<Entity>
pub time_for_mission: f32,
pub start_sheep_count: usize,
pub max_dead_sheep: usize,
pub torches_to_lit: Vec<Entity>,
}

fn start_fire_problems(
mut commands: Commands,
torches : Query<(Entity, &SafeArea), With<TorchBase>>,
episode_time : Res<EpisodeTime>,
sheep : Query<&Transform, With<Sheep>>,
mut global_task : ResMut<NextState<GlobalTask>>
torches: Query<(Entity, &SafeArea), With<TorchBase>>,
episode_time: Res<EpisodeTime>,
sheep: Query<&Transform, With<Sheep>>,
mut global_task: ResMut<NextState<GlobalTask>>,
) {
let torch_count = torches.iter().count();

Expand All @@ -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::<Vec<_>>();
let mut problem_torches = torches
.iter()
.map(|(a, b)| (a, b, 0_usize))
.collect::<Vec<_>>();

for (e, safe_area, count) in problem_torches.iter_mut() {
for sheep in sheep.iter() {
Expand All @@ -61,7 +76,10 @@ fn start_fire_problems(
}
}

let mut problem_torches = problem_torches.iter().filter(|(_, _, count)| *count > 0).collect::<Vec<_>>();
let mut problem_torches = problem_torches
.iter()
.filter(|(_, _, count)| *count > 0)
.collect::<Vec<_>>();

if (problem_torches.len() == 0) {
global_task.set(GlobalTask::None);
Expand All @@ -76,44 +94,45 @@ 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::<Vec<_>>(),
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::<Vec<_>>(),
});

info!("Start torch problem with {} torches", problem_torches.len());
}

fn update_delight_system(
mut commands: Commands,
mut status : ResMut<TorchDelightStatus>,
mut texts : Query<&mut Text, With<TaskText>>,
torches : Query<(&TorchBase, Option<&TorchDelight>)>,
time : Res<Time>,
mut gamestate : ResMut<NextState<GameState>>,
mut global_task : ResMut<NextState<GlobalTask>>,
sheep : Query<&Sheep>
mut status: ResMut<TorchDelightStatus>,
mut texts: Query<&mut Text, With<TaskText>>,
torches: Query<(&TorchBase, Option<&TorchDelight>)>,
time: Res<Time>,
mut gamestate: ResMut<NextState<GameState>>,
mut global_task: ResMut<NextState<GlobalTask>>,
sheep: Query<&Sheep>,
) {
if !status.torches_to_lit.is_empty() {
status.time_for_mission -= time.delta_seconds();
if status.time_for_mission < 0.0 {
gamestate.set(GameState::Finish);
commands.insert_resource(FailReason::TaskFailed(
format!("Not all the torches were lit. You should be better at waking up ancient vampires.")
));
commands.insert_resource(FailReason::TaskFailed(format!(
"Not all the torches were lit. You should be better at waking up ancient vampires."
)));
return;
}

Expand All @@ -132,9 +151,9 @@ fn update_delight_system(
let lived_sheep_count = sheep.iter().count();
if status.start_sheep_count - lived_sheep_count > status.max_dead_sheep {
gamestate.set(GameState::Finish);
commands.insert_resource(FailReason::TaskFailed(
format!("Too many sheep was eaten. You should be better at waking up ancient vampires.")
));
commands.insert_resource(FailReason::TaskFailed(format!(
"Too many sheep was eaten. You should be better at waking up ancient vampires."
)));
return;
}
}
Expand All @@ -147,12 +166,11 @@ fn update_delight_system(

fn delight(
mut commands: Commands,
mut torches : Query<(Entity, &Transform, &mut TorchDelight, &mut TorchBase)>,
mut torches: Query<(Entity, &Transform, &mut TorchDelight, &mut TorchBase)>,
mut lights: Query<&mut SpotLight, With<TorchLight>>,
time : Res<Time>,
time: Res<Time>,
) {
for (e, tr, mut delight, mut base) in &mut torches {

delight.be_scared_time -= time.delta_seconds();
if delight.be_scared_time > 0.0 {
if let Ok(mut light) = lights.get_mut(base.light) {
Expand All @@ -168,8 +186,10 @@ fn delight(
light.intensity = 0.0;
}
base.lit = false;
commands.entity(e).remove::<TorchDelight>().remove::<SafeArea>();

commands
.entity(e)
.remove::<TorchDelight>()
.remove::<SafeArea>();
} else if let Ok(mut light) = lights.get_mut(base.light) {
light.color = BAD_TORCH_COLOR;
light.intensity = TORCH_ILLUMINATION * delight.rest_time / delight.change_duration;
Expand All @@ -183,10 +203,10 @@ fn delight(
light.inner_angle = inner_angle;
light.outer_angle = outer_angle;

commands.entity(e).insert(SafeArea::Circle {
pos: Vec2::new(tr.translation.x, tr.translation.z),
radius: new_r
commands.entity(e).insert(SafeArea::Circle {
pos: Vec2::new(tr.translation.x, tr.translation.z),
radius: new_r,
});
}
}
}
}
9 changes: 5 additions & 4 deletions src/shepherd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use bevy::prelude::*;
use crate::{
common_storage::CommonStorage,
get_sprite_rotation,
global_task::torch_blinking::TorchDelight,
physics::{Velocity, WalkController},
player::{DOG_ACCELERATION, DOG_SPEED, Bark},
player::{Bark, DOG_ACCELERATION, DOG_SPEED},
sunday::DayState,
torch::{IgniteTorch, TorchBase},
GameSet, global_task::torch_blinking::TorchDelight, GameStuff,
GameSet, GameStuff,
};

const SHEPHERD_PATH: &str = "test/Knight.png";
Expand Down Expand Up @@ -113,7 +114,7 @@ fn spawn_shepherd_system(
acceleration: SHEPHERD_ACCEL,
target_velocity: Vec3::ZERO,
},
GameStuff
GameStuff,
));
}
events.clear();
Expand All @@ -133,4 +134,4 @@ fn bark_system(
commands.entity(entity).insert(IgniteAllTorhes);
}
}
}
}
9 changes: 3 additions & 6 deletions src/storyteller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ impl Default for NextTaskDelay {
}
}

fn setup_delay(
mut delay : ResMut<NextTaskDelay>,
) {
fn setup_delay(mut delay: ResMut<NextTaskDelay>) {
*delay = NextTaskDelay(10.0);
}

Expand All @@ -89,9 +87,8 @@ fn storyteller_system(
mut next_task: ResMut<NextState<GlobalTask>>,
day_state: Res<State<DayState>>,
episode_time: Res<EpisodeTime>,
mut delay : ResMut<NextTaskDelay>
mut delay: ResMut<NextTaskDelay>,
) {

if *current_task != GlobalTask::None {
return;
}
Expand Down Expand Up @@ -122,7 +119,7 @@ fn storyteller_system(
} else if rand_choise == 1 {
next_task.set(GlobalTask::TorchProblem);
}
},
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/torch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use bevy::prelude::*;
use crate::{
common_storage::CommonStorage,
get_sprite_rotation,
global_task::torch_blinking::TorchDelight,
safe_area::{HiddenSafeArea, SafeArea},
GameSet, GameStuff, global_task::torch_blinking::TorchDelight,
GameSet, GameStuff,
};

const TORCH_PATH: &str = "test/torch.png";
Expand Down
16 changes: 14 additions & 2 deletions src/wolf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,15 @@ fn go_out_system(

fn run_out_system(
mut commands: Commands,
mut wolfs: Query<(Entity, &Transform, &mut WalkController, Option<&TryToCatchSheep>), (With<Wolf>, Without<GoOut>)>,
mut wolfs: Query<
(
Entity,
&Transform,
&mut WalkController,
Option<&TryToCatchSheep>,
),
(With<Wolf>, Without<GoOut>),
>,
safearea: Query<&SafeArea>,
) {
for (wolf, wolf_transform, mut walk_controller, catch) in wolfs.iter_mut() {
Expand All @@ -190,7 +198,11 @@ fn run_out_system(
if let Some(area) = in_safe_area.last() {
walk_controller.target_velocity =
(wolf_transform.translation - area.get_center()).normalize() * WOLF_SPEED;
commands.entity(wolf).insert(GoOut).remove::<TryToCatchSheep>().remove::<Eating>();
commands
.entity(wolf)
.insert(GoOut)
.remove::<TryToCatchSheep>()
.remove::<Eating>();

if let Some(catch) = catch {
commands.entity(catch.target).remove::<UnderHunting>();
Expand Down

0 comments on commit 2ef8e19

Please sign in to comment.