generated from NiklasEi/bevy_game_template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
231 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
level_time = 4 * 60 | ||
|
||
l = 20 | ||
s_d = 45.0 * 1000.0 / 3600.0 | ||
s_s = s_d * 0.5 * 0.2 | ||
|
||
print("Sheep time:", l / s_s) | ||
print("Dog max travel time:", 2 * np.pi * l / s_d) | ||
|
||
hardness = np.linspace(0.05, 1.0, 100) | ||
k_h = 1.5 | ||
|
||
param_curve = l / s_s - 2 * np.pi * l / s_d - 1 / k_h / hardness | ||
|
||
delta_t = 10 | ||
c = 10 | ||
|
||
n = (param_curve + delta_t) / np.sqrt(c) | ||
|
||
plt.plot(hardness, n) | ||
plt.grid() | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use bevy::prelude::*; | ||
|
||
use crate::storyteller::LevelTimer; | ||
|
||
pub struct LevelUiPlugin; | ||
|
||
impl Plugin for LevelUiPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_event::<CreateLevelUi>() | ||
.add_systems(Update, create_level_ui_system); | ||
} | ||
} | ||
|
||
#[derive(Event)] | ||
pub struct CreateLevelUi; | ||
|
||
#[derive(Component)] | ||
pub struct LevelUi; | ||
|
||
fn create_level_ui_system( | ||
mut commands: Commands, | ||
asset_server: Res<AssetServer>, | ||
mut ev_create_level_ui: EventReader<CreateLevelUi>, | ||
) { | ||
if ev_create_level_ui.is_empty() { | ||
return; | ||
} | ||
|
||
let mut text_style = TextStyle::default(); | ||
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, | ||
..default() | ||
}, | ||
..default() | ||
}, | ||
LevelUi | ||
)).with_children(|parent| { | ||
parent.spawn(( | ||
TextBundle::from_section( | ||
"", | ||
text_style.clone() | ||
), | ||
LevelUi, | ||
LevelTimer | ||
)); | ||
}); | ||
|
||
ev_create_level_ui.clear(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
//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 bevy::prelude::*; | ||
use rand::Rng; | ||
|
||
use crate::{sheep::{Sheep, SHEEP_SPEED, RANDOM_WALK_SPEED_MULTIPLIER, GoTo}, test_level::LevelSize, player::DOG_SPEED}; | ||
|
||
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); | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct SheepWave { | ||
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> | ||
} | ||
|
||
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> | ||
) { | ||
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 | ||
}); | ||
|
||
info!("Next wave: {:?}", teller.next_wave); | ||
} else { | ||
let wave = teller.next_wave.as_ref().unwrap().clone(); | ||
let cur_time = time.elapsed_seconds(); | ||
if wave.time <= cur_time { | ||
teller.next_wave = None; | ||
|
||
let mut rand = rand::thread_rng(); | ||
|
||
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(); | ||
//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) | ||
}) | ||
.collect::<Vec<_>>(); | ||
sorted_sheep.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap()); | ||
|
||
//send split_c sheep in that direction | ||
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, | ||
} | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
#[derive(Component)] | ||
pub struct LevelTimer; | ||
|
||
fn level_timer( | ||
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); | ||
} else { | ||
timer.sections[0].value = format!("{:02}:{:02}", 0, 0); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters