Skip to content

Commit

Permalink
Before new nearest logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ayamaev-se committed Dec 11, 2023
1 parent f1c33ee commit 31e4bdf
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 20 deletions.
10 changes: 5 additions & 5 deletions src/global_task/sheep_escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fn generate_new_wave(

if *day_state == DayState::Day {
let sheep_count = sheep.iter().count() as f32;
let c = sheep_count * episode_time * 0.2 + 10.0;
let c = (sheep_count * episode_time * 0.2 + 10.0).max(10.0);
let mut dt = 5.0 - 1.0 * episode_time;
let n = 1.0 + 3.0 * episode_time;

Expand All @@ -127,13 +127,13 @@ fn generate_new_wave(
});
} else if *day_state == DayState::Night {
let sheep_count = sheep.iter().count() as f32;
let c = sheep_count * episode_time * 0.1 + 1.0;
let dt = 15.0 - 3.0 * episode_time;
let n = 1;
let c = (sheep_count * episode_time * 0.2 + 10.0).max(10.0);
let dt = 5.0 - 1.0 * episode_time;
let n = 1.0 + 3.0 * episode_time;

next_wave.0 = Some(SheepWave {
count: c as usize,
beams: n,
beams: n.round() as usize,
time: time.elapsed_seconds() + dt,
});
}
Expand Down
21 changes: 11 additions & 10 deletions src/global_task/torch_blinking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn start_fire_problems(
mut commands: Commands,
torches: Query<(Entity, &SafeArea), With<TorchBase>>,
episode_time: Res<EpisodeTime>,
sheep: Query<&Transform, With<Sheep>>,
sheep: Query<(Entity,&Transform), With<Sheep>>,
mut global_task: ResMut<NextState<GlobalTask>>,
) {
let torch_count = torches.iter().count();
Expand All @@ -65,20 +65,21 @@ fn start_fire_problems(

let mut problem_torches = torches
.iter()
.map(|(a, b)| (a, b, 0_usize))
.map(|(a, b)| (a, b, 0_usize, HashSet::new()))
.collect::<Vec<_>>();

for (e, safe_area, count) in problem_torches.iter_mut() {
for sheep in sheep.iter() {
for (e, safe_area, count, set) in problem_torches.iter_mut() {
for (sheep_e, sheep) in sheep.iter() {
if safe_area.in_area(Vec2::new(sheep.translation.x, sheep.translation.z)) {
*count = *count + 1;
set.insert(sheep_e);
}
}
}

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

if (problem_torches.len() == 0) {
Expand All @@ -91,25 +92,25 @@ fn start_fire_problems(
problem_torches.remove(i);
}

let mut sheep_in_torches = 0;
let mut sheep_in_torches : HashSet<Entity> = HashSet::new();

for (idx, (e, safe_area, count)) in problem_torches.iter().enumerate() {
for (idx, (e, safe_area, count, set)) 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,
});

sheep_in_torches += *count;
sheep_in_torches.extend(set.iter());
}

commands.insert_resource(TorchDelightStatus {
time_for_mission: 40.0,
start_sheep_count: sheep.iter().count(),
max_dead_sheep: (sheep_in_torches / 2).max(10),
max_dead_sheep: (sheep_in_torches.len() / 2).max(10),
torches_to_lit: problem_torches
.iter()
.map(|(e, _, _)| *e)
.map(|(e, _, _, _)| *e)
.collect::<Vec<_>>(),
});

Expand Down
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ fn main() {
}),
..default()
})
.set(ImagePlugin::default_nearest()),
.set(ImagePlugin::default_nearest())
.set(TaskPoolPlugin {
task_pool_options: TaskPoolOptions {
max_total_threads: 1,
..default()
},
..default()
}),
)
.insert_resource(DirectionalLightShadowMap { size: 4096 })
.add_plugins(GamePlugin)
Expand Down
40 changes: 37 additions & 3 deletions src/sheep.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{f32::consts::PI, time::Duration, fmt::format};

use bevy::prelude::*;
use bevy::{prelude::*, utils::HashSet};
use rand::{rngs::ThreadRng, Rng};

use crate::{
Expand Down Expand Up @@ -79,7 +79,9 @@ impl Plugin for SheepPlugin {
.add_systems(Update, collect_field)
.add_plugins(AutoAnimPlugin::<SheepAnim>::default())

.add_systems(Update, set_anim_state.in_set(GameSet::Playing));
.add_systems(Update, set_anim_state.in_set(GameSet::Playing))

.add_systems(Update, update_nearest);
}
}

Expand Down Expand Up @@ -497,7 +499,8 @@ pub fn setup(
set: SheepAnim::Idle,
timer: Timer::from_seconds(0.1 + rng.gen_range(-0.01..=0.01), TimerMode::Repeating),
current_frame: 0
}
},
NearestSheep::default()
));
exact_sheep_count += 1;
}
Expand Down Expand Up @@ -529,6 +532,37 @@ type NNTree = KDTree3<Sheep>;
const PREFERED_DISTANCE: f32 = 1.0;
const PREFERED_DY: f32 = 0.1;

#[derive(Component, Default)]
pub struct NearestSheep(pub Vec<(Vec3, Option<Entity>)>);

#[derive(Component)]
pub struct UpdatedSheep;

fn update_nearest(
mut commands: Commands,
mut sheep : Query<(Entity, &Transform, &mut NearestSheep), (With<Sheep>, Without<UpdatedSheep>)>,
mut updated_sheep : Query<Entity, With<UpdatedSheep>>,
field: ResMut<NNTree>,
) {
let max_sheep_count = 100;
let mut idx = 0;
for (e, t, mut nearest) in sheep.iter_mut() {
idx += 1;
if idx > max_sheep_count {
break;
}

nearest.0 = field.k_nearest_neighbour(t.translation, 7);
commands.entity(e).insert(UpdatedSheep);
}

if sheep.is_empty() {
for e in updated_sheep.iter() {
commands.entity(e).remove::<UpdatedSheep>();
}
}
}

fn collect_field(
mut sheep: Query<
(
Expand Down
2 changes: 1 addition & 1 deletion src/test_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn setup(

cascade_shadow_config: cascades.build(),
..default()
});
}).insert(GameStuff);

//ambient ligjt
commands.insert_resource(AmbientLight {
Expand Down

0 comments on commit 31e4bdf

Please sign in to comment.