Skip to content

Commit

Permalink
harden-by-multiple-areas
Browse files Browse the repository at this point in the history
  • Loading branch information
naomijub committed Dec 10, 2023
1 parent 485e448 commit 358f31e
Show file tree
Hide file tree
Showing 16 changed files with 204 additions and 168 deletions.
1 change: 1 addition & 0 deletions src/global_task/collect_sheep_in_area.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

8 changes: 3 additions & 5 deletions src/global_task/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod sheep_escape;
pub mod collect_sheep_in_area;
pub mod sheep_escape;
pub mod wolf_attack;

use bevy::prelude::*;
Expand All @@ -8,8 +8,6 @@ pub struct GlobalTaskPlugin;

impl Plugin for GlobalTaskPlugin {
fn build(&self, app: &mut App) {
app.add_plugins((
sheep_escape::SheepEscapePlugin,
));
app.add_plugins((sheep_escape::SheepEscapePlugin,));
}
}
}
75 changes: 42 additions & 33 deletions src/global_task/sheep_escape.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
use bevy::prelude::*;
use rand::Rng;

use crate::{storyteller::{Storyteller, GlobalTask, FailReason}, sunday::{DayState, EpisodeTime}, sheep::{Sheep, IsScared, GoTo, IdleFeeding, Decision}, player::Dog, test_level::LevelSize, GameSet, GameState, level_ui::TaskText};
use crate::{
level_ui::TaskText,
player::Dog,
sheep::{Decision, GoTo, IdleFeeding, IsScared, Sheep},
storyteller::{FailReason, GlobalTask, Storyteller},
sunday::{DayState, EpisodeTime},
test_level::LevelSize,
GameSet, GameState,
};

pub struct SheepEscapePlugin;


impl Plugin for SheepEscapePlugin {
fn build(&self, app: &mut App) {
app
.add_systems(OnEnter(GlobalTask::SheepEscape), generate_new_wave)
.add_systems(Update,
(
apply_deferred,
check_wave_finish,
wave_executor,
).chain().run_if(in_state(GlobalTask::SheepEscape)).in_set(GameSet::Playing))

app.add_systems(OnEnter(GlobalTask::SheepEscape), generate_new_wave)
.add_systems(
Update,
(apply_deferred, check_wave_finish, wave_executor)
.chain()
.run_if(in_state(GlobalTask::SheepEscape))
.in_set(GameSet::Playing),
)
.init_resource::<NextWave>()
.init_resource::<SheepWaveStatus>();
}
Expand All @@ -37,23 +43,22 @@ pub struct SheepWave {

#[derive(Resource, Default)]
pub struct SheepWaveStatus {
pub start_count : usize,
pub sheep : Vec<Entity>
pub start_count: usize,
pub sheep: Vec<Entity>,
}

fn check_wave_finish(
mut commands: Commands,
escapers : Query<Entity, (With<ShawshankRedemption>, With<Sheep>)>,
sheep : Query<&Sheep>,
mut global_task : ResMut<NextState<GlobalTask>>,
mut sheep_wave_status : ResMut<SheepWaveStatus>,
mut game_state : ResMut<NextState<GameState>>,
next_wave : Res<NextWave>,
mut info_texts : Query<&mut Text, With<TaskText>>,
escapers: Query<Entity, (With<ShawshankRedemption>, With<Sheep>)>,
sheep: Query<&Sheep>,
mut global_task: ResMut<NextState<GlobalTask>>,
mut sheep_wave_status: ResMut<SheepWaveStatus>,
mut game_state: ResMut<NextState<GameState>>,
next_wave: Res<NextWave>,
mut info_texts: Query<&mut Text, With<TaskText>>,
) {
let loose_limit = (sheep_wave_status.start_count / 2).max(10);
if escapers.is_empty() && next_wave.0.is_none() && sheep_wave_status.start_count != 0 {

let mut alived_sheep = 0;
for e in &sheep_wave_status.sheep {
if sheep.get(*e).is_ok() {
Expand All @@ -63,7 +68,9 @@ fn check_wave_finish(
info!("WAVE FINISHED");

if sheep_wave_status.start_count - alived_sheep > loose_limit {
commands.insert_resource(FailReason::TaskFailed("Half of the runaway sheep were eaten :(".to_string()));
commands.insert_resource(FailReason::TaskFailed(
"Half of the runaway sheep were eaten :(".to_string(),
));
game_state.set(GameState::Finish);
global_task.set(GlobalTask::None);
} else {
Expand All @@ -82,22 +89,24 @@ fn check_wave_finish(
}
} else if sheep_wave_status.start_count != 0 {
for mut t in info_texts.iter_mut() {
t.sections[0].value = format!("{} sheep are trying to escape! Stop them! Dont lose more than {}", escapers.iter().count(), loose_limit);
t.sections[0].value = format!(
"{} sheep are trying to escape! Stop them! Dont lose more than {}",
escapers.iter().count(),
loose_limit
);
}
}
}

fn generate_new_wave(
mut commands: Commands,
time: Res<Time>,
mut next_wave: ResMut<NextWave>,
teller : ResMut<Storyteller>,
day_state : Res<State<DayState>>,
episode_time : Res<EpisodeTime>,
teller: ResMut<Storyteller>,
day_state: Res<State<DayState>>,
episode_time: Res<EpisodeTime>,
sheep: Query<(Entity, &Transform), (With<Sheep>, Without<IsScared>, Without<GoTo>)>,
) {
let level_time = time.elapsed_seconds() - teller.level_start_time;
let unfiorm_time = level_time / teller.level_duration;

let episode_time = episode_time.0;

Expand Down Expand Up @@ -134,17 +143,17 @@ fn generate_new_wave(

fn wave_executor(
mut commands: Commands,
mut next_wave : ResMut<NextWave>,
time : Res<Time>,
mut next_wave: ResMut<NextWave>,
time: Res<Time>,
sheep: Query<(Entity, &Transform), (With<Sheep>, Without<IsScared>, Without<GoTo>)>,
dog: Query<&Transform, With<Dog>>,
level_size: Res<LevelSize>,
mut sheep_wave_status : ResMut<SheepWaveStatus>,
mut sheep_wave_status: ResMut<SheepWaveStatus>,
) {
let Ok(dog_transform) = dog.get_single() else {
return;
};

if next_wave.0.is_some() {
let wave = next_wave.0.as_ref().unwrap().clone();
let cur_time = time.elapsed_seconds();
Expand Down Expand Up @@ -192,4 +201,4 @@ fn wave_executor(
sheep_wave_status.start_count = sheep_wave_status.sheep.len();
}
}
}
}
1 change: 1 addition & 0 deletions src/global_task/wolf_attack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

86 changes: 42 additions & 44 deletions src/level_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub struct CreateLevelUi;
#[derive(Component)]
pub struct LevelUi;


#[derive(Component)]
pub struct TaskText;

Expand All @@ -35,54 +34,53 @@ fn create_level_ui_system(

//Spawn top info bar
commands
.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Px(100.0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
align_self: AlignSelf::Stretch,
top: Val::Px(0.0),
left: Val::Px(0.0),
..default()
},
..default()
}).with_children(|parent| {
parent.spawn((
NodeBundle {
style: Style {
width: Val::Percent(50.0),
height: Val::Px(100.0),
flex_direction: FlexDirection::Column,
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,

align_self: AlignSelf::Stretch,
..default()
},
background_color: BackgroundColor(Color::rgba(0.0, 0.0, 0.0, 0.5)),
.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Px(100.0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
align_self: AlignSelf::Stretch,
top: Val::Px(0.0),
left: Val::Px(0.0),
..default()
},
LevelUi,
GameStuff,
))
..default()
})
.with_children(|parent| {
parent.spawn((
TextBundle::from_section("", text_style.clone()),
LevelUi,
LevelTimer,
));
parent
.spawn((
NodeBundle {
style: Style {
width: Val::Percent(50.0),
height: Val::Px(100.0),
flex_direction: FlexDirection::Column,
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,

align_self: AlignSelf::Stretch,
..default()
},
background_color: BackgroundColor(Color::rgba(0.0, 0.0, 0.0, 0.5)),
..default()
},
LevelUi,
GameStuff,
))
.with_children(|parent| {
parent.spawn((
TextBundle::from_section("", text_style.clone()),
LevelUi,
LevelTimer,
));

parent.spawn((
TextBundle::from_section(
"",
text_style.clone()
),
LevelUi,
TaskText
));
parent.spawn((
TextBundle::from_section("", text_style.clone()),
LevelUi,
TaskText,
));
});
});
});

ev_create_level_ui.clear();
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub mod common_storage;
pub mod debug_diagnostic;
pub mod finish_screen;
pub mod global_task;
pub mod level_ui;
pub mod menu;
pub mod physics;
Expand All @@ -16,7 +17,6 @@ pub mod sunday;
pub mod test_level;
pub mod torch;
pub mod wolf;
pub mod global_task;

use std::f32::consts::PI;

Expand Down
2 changes: 1 addition & 1 deletion src/physics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct WalkController {
}

fn walk_system(time: Res<Time>, mut query: Query<(&mut Velocity, &mut WalkController)>) {
for (mut velocity, mut controller) in query.iter_mut() {
for (mut velocity, controller) in query.iter_mut() {
let dspeed = controller.target_velocity - velocity.0;
let accel = controller.acceleration.min(dspeed.length() * 100.0);
let cur_vel = velocity.0;
Expand Down
2 changes: 1 addition & 1 deletion src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ fn set_cam_distance(
mut commands: Commands,
camera_without_dist: Query<(Entity, &Transform), (With<Camera>, Without<CameraDistance>)>,
player_query: Query<&Transform, With<Player>>,
mut sun: Query<&mut CascadeShadowConfig>,
_: Query<&CascadeShadowConfig>,
) {
let Ok(player) = player_query.get_single() else {
return;
Expand Down
Loading

0 comments on commit 358f31e

Please sign in to comment.