Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
rewin123 committed Dec 7, 2023
1 parent 5ee1532 commit 3c226bb
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 74 deletions.
44 changes: 22 additions & 22 deletions src/level_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,30 @@ fn create_level_ui_system(
text_style.font_size = 24.0;

//Spawn top info bar
commands.spawn((NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Px(50.0),
flex_direction: FlexDirection::Row,
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,

align_self: AlignSelf::Stretch,
commands
.spawn((
NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Px(50.0),
flex_direction: FlexDirection::Row,
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,

align_self: AlignSelf::Stretch,
..default()
},
..default()
},
..default()
},
LevelUi
)).with_children(|parent| {
parent.spawn((
TextBundle::from_section(
"",
text_style.clone()
),
LevelUi,
LevelTimer
));
});
))
.with_children(|parent| {
parent.spawn((
TextBundle::from_section("", text_style.clone()),
LevelUi,
LevelTimer,
));
});

ev_create_level_ui.clear();
}
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

pub mod common_storage;
pub mod debug_diagnostic;
pub mod level_ui;
pub mod physics;
pub mod player;
pub mod safe_area;
pub mod sheep;
pub mod sprite_material;
pub mod storyteller;
pub mod test_level;
pub mod torch;
pub mod storyteller;
pub mod level_ui;

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

Expand Down
2 changes: 1 addition & 1 deletion src/sheep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Plugin for SheepPlugin {

//random walk
app.add_event::<InitRandomWalk>()
.add_systems(Update, (init_random_walk, ));
.add_systems(Update, (init_random_walk,));

app.add_systems(Update, (goto_system,));

Expand Down
90 changes: 44 additions & 46 deletions src/storyteller.rs
Original file line number Diff line number Diff line change
@@ -1,74 +1,73 @@
//This global AI is responsible for creating problems for player
//This module will be determine where and how sheep will be try to escape from safe zone

use std::{f32::consts::{PI, E}, time::Duration};
use std::{
f32::consts::{E, PI},
time::Duration,
};

use bevy::prelude::*;
use rand::Rng;

use crate::{sheep::{Sheep, SHEEP_SPEED, RANDOM_WALK_SPEED_MULTIPLIER, GoTo}, test_level::LevelSize, player::DOG_SPEED};
use crate::{
player::DOG_SPEED,
sheep::{GoTo, Sheep, RANDOM_WALK_SPEED_MULTIPLIER, SHEEP_SPEED},
test_level::LevelSize,
};

pub struct StorytellerPlugin;

impl Plugin for StorytellerPlugin {
fn build(&self, app: &mut App) {
app
.insert_resource(Storyteller {
level_start_time : 0.0,
level_duration : 4.0 * 60.0,
next_wave : None
})
.add_systems(Update, (
storyteller_system,
level_timer
))
.add_systems(PostStartup, setup_start_time);
app.insert_resource(Storyteller {
level_start_time: 0.0,
level_duration: 4.0 * 60.0,
next_wave: None,
})
.add_systems(Update, (storyteller_system, level_timer))
.add_systems(PostStartup, setup_start_time);
}
}

#[derive(Debug, Clone)]
pub struct SheepWave {
pub count : usize,
pub beams : usize,
pub time : f32
pub count: usize,
pub beams: usize,
pub time: f32,
}

#[derive(Resource)]
pub struct Storyteller {
pub level_start_time : f32,
pub level_duration : f32,
pub next_wave : Option<SheepWave>
pub level_start_time: f32,
pub level_duration: f32,
pub next_wave: Option<SheepWave>,
}

fn setup_start_time(
mut teller : ResMut<Storyteller>,
time : Res<Time>
) {
fn setup_start_time(mut teller: ResMut<Storyteller>, time: Res<Time>) {
teller.level_start_time = time.elapsed_seconds();
}

fn storyteller_system(
mut commands: Commands,
sheep : Query<(Entity, &Transform), With<Sheep>>,
mut teller : ResMut<Storyteller>,
time : Res<Time>,
level_size : Res<LevelSize>
sheep: Query<(Entity, &Transform), With<Sheep>>,
mut teller: ResMut<Storyteller>,
time: Res<Time>,
level_size: Res<LevelSize>,
) {
if teller.next_wave.is_none() {
let level_time = time.elapsed_seconds() - teller.level_start_time;
let unfiorm_time = level_time / teller.level_duration;


let sheep_count = sheep.iter().count() as f32;

let c = sheep_count * unfiorm_time * 0.2 + 1.0;
let dt = 15.0 - 10.0 * unfiorm_time;
let n = 1.0 + 2.0 * unfiorm_time;

teller.next_wave = Some(SheepWave {
count : c as usize,
beams : n as usize,
time : time.elapsed_seconds() + dt
count: c as usize,
beams: n as usize,
time: time.elapsed_seconds() + dt,
});

info!("Next wave: {:?}", teller.next_wave);
Expand All @@ -82,14 +81,16 @@ fn storyteller_system(

let split_c = (wave.count / wave.beams).max(1);
for _ in 0..wave.beams {
let random_dir = Vec3::new(rand.gen_range(-1.0..1.0), 0.0, rand.gen_range(-1.0..1.0)).normalize();
let random_dir =
Vec3::new(rand.gen_range(-1.0..1.0), 0.0, rand.gen_range(-1.0..1.0))
.normalize();
//create a sorted list of sheep which far in direction
let mut sorted_sheep = sheep
.iter()
.map(|(e, t)| {
let dir = t.translation;
let proj_dir = dir.dot(random_dir);
(e, dir, proj_dir)
(e, dir, proj_dir)
})
.collect::<Vec<_>>();
sorted_sheep.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap());
Expand All @@ -98,36 +99,33 @@ fn storyteller_system(
for i in 0..split_c {
if let Some((e, pos, dist)) = sorted_sheep.get(i) {
info!("Sending {:?} with {:?}", e, dist);
commands.entity(*e).insert(
GoTo {
target: *pos + level_size.0 * random_dir,
}
);
commands.entity(*e).insert(GoTo {
target: *pos + level_size.0 * random_dir,
});
}
}
}
}
}


}

#[derive(Component)]
pub struct LevelTimer;

fn level_timer(
mut timers : Query<&mut Text, With<LevelTimer>>,
teller : Res<Storyteller>,
time : Res<Time>
mut timers: Query<&mut Text, With<LevelTimer>>,
teller: Res<Storyteller>,
time: Res<Time>,
) {
for mut timer in timers.iter_mut() {
let level_time = time.elapsed_seconds() - teller.level_start_time;
if (level_time > 0.0) {
let dur = Duration::from_secs_f32(teller.level_duration - level_time);

timer.sections[0].value = format!("{:02}:{:02}", dur.as_secs() / 60, dur.as_secs() % 60);
timer.sections[0].value =
format!("{:02}:{:02}", dur.as_secs() / 60, dur.as_secs() % 60);
} else {
timer.sections[0].value = format!("{:02}:{:02}", 0, 0);
}
}
}
}
6 changes: 3 additions & 3 deletions src/test_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use rand::prelude::*;
use std::f32::consts::PI;

use crate::{
get_sprite_rotation, player::SpawnPlayer, safe_area::SafeArea,
sprite_material::create_plane_mesh, torch::SpawnTorch, level_ui::CreateLevelUi,
get_sprite_rotation, level_ui::CreateLevelUi, player::SpawnPlayer, safe_area::SafeArea,
sprite_material::create_plane_mesh, torch::SpawnTorch,
};

const TREE_PATH: &str = "test/pine.png";
Expand All @@ -33,7 +33,7 @@ pub fn setup(
mut spawn_player_event: EventWriter<SpawnPlayer>,
mut spawn_torch: EventWriter<SpawnTorch>,
level_size: Res<LevelSize>,
mut create_level_ui : EventWriter<CreateLevelUi>,
mut create_level_ui: EventWriter<CreateLevelUi>,
) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0.0, 30.0, 30.0).looking_at(Vec3::ZERO, Vec3::Y),
Expand Down

0 comments on commit 3c226bb

Please sign in to comment.