Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feedbvack #5

Merged
merged 2 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dev = [
"bevy/dynamic_linking",
"dep:bevy-inspector-egui"
]
default = ["dev"]

# All of Bevy's default features exept for the audio related ones (bevy_audio, vorbis), since they clash with bevy_kira_audio
# and android_shared_stdcxx, since that is covered in `mobile`
Expand Down
2 changes: 1 addition & 1 deletion examples/simple_3d_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn setup(
..default()
});

//spawn ships
//spawn sheeps
let r = 50.0;
let mut rng = rand::thread_rng();
let sheep_count = 2000;
Expand Down
50 changes: 44 additions & 6 deletions src/debug_diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use bevy::prelude::*;

use crate::{safe_area::SheepCounter, sheep::StartSheepCount, GameSet, GameState, GameStuff};
use crate::{
safe_area::SheepCounter,
sheep::{Sheep, StartSheepCount},
GameSet, GameState, GameStuff,
};

const FONT_SIZE: f32 = 24.0;

Expand All @@ -15,15 +19,16 @@ impl Plugin for DiagnosticPlugin {
apply_deferred,
setup_counter,
setup_sheep_counter,
setup_alive_sheep_counter,
)
.chain(),
)
.add_systems(
Update,
(fps_counting, sheep_counter_text).in_set(GameSet::Playing),
(fps_counting, sheep_counter_text, alive_sheep_counter).in_set(GameSet::Playing),
);

#[cfg(debug_assertions)]
#[cfg(feature = "dev")]
{
app.add_plugins(bevy_inspector_egui::quick::WorldInspectorPlugin::default());
}
Expand Down Expand Up @@ -80,14 +85,14 @@ fn fps_counting(mut query: Query<&mut Text, With<FrameCounter>>, time: Res<Time>
}

#[derive(Component)]
pub struct ShipDebugCounter;
pub struct SheepDebugCounter;

pub fn setup_sheep_counter(mut commands: Commands, panels: Query<Entity, With<DiagnosticPanel>>) {
let mut text_style = TextStyle::default();
text_style.font_size = FONT_SIZE;
let sheep_counter = commands
.spawn(TextBundle::from_section("Sheep in safe area: ", text_style))
.insert(ShipDebugCounter)
.insert(SheepDebugCounter)
.id();

if let Ok(panel) = panels.get_single() {
Expand All @@ -96,7 +101,7 @@ pub fn setup_sheep_counter(mut commands: Commands, panels: Query<Entity, With<Di
}

pub fn sheep_counter_text(
mut query: Query<&mut Text, With<ShipDebugCounter>>,
mut query: Query<&mut Text, With<SheepDebugCounter>>,
sheep_counter: Res<SheepCounter>,
start_sheep_count: Res<StartSheepCount>,
) {
Expand All @@ -107,3 +112,36 @@ pub fn sheep_counter_text(
);
}
}

#[derive(Component)]
pub struct SheepAliveDebugCounter;

pub fn setup_alive_sheep_counter(
mut commands: Commands,
panels: Query<Entity, With<DiagnosticPanel>>,
) {
let mut text_style = TextStyle::default();
text_style.font_size = FONT_SIZE;
let sheep_counter = commands
.spawn(TextBundle::from_section("Alive sheeps: ", text_style))
.insert(SheepAliveDebugCounter)
.id();

if let Ok(panel) = panels.get_single() {
commands.entity(panel).add_child(sheep_counter);
}
}

pub fn alive_sheep_counter(
mut query: Query<&mut Text, With<SheepAliveDebugCounter>>,
sheeps: Query<&Sheep>,
start_sheep_count: Res<StartSheepCount>,
) {
let alive_sheep_count = sheeps.iter().count();
for mut text in &mut query {
text.sections[0].value = format!(
"Alive sheeps: {}/{}",
alive_sheep_count, start_sheep_count.0
);
}
}
2 changes: 1 addition & 1 deletion src/finish_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn setup_finish_screen(mut commands: Commands, score: Res<Score>, fail: Option<R
text_style.font_size = 24.0;

commands.spawn((FinishScreen, NodeBundle {
style : Style {
style: Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
justify_content: JustifyContent::Center,
Expand Down
2 changes: 1 addition & 1 deletion src/level_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct LevelUi;

fn create_level_ui_system(
mut commands: Commands,
asset_server: Res<AssetServer>,
_asset_server: Res<AssetServer>,
mut ev_create_level_ui: EventReader<CreateLevelUi>,
) {
if ev_create_level_ui.is_empty() {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub mod wolf;

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

#[cfg(debug_assertions)]
#[cfg(feature = "dev")]
use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin};
use bevy::prelude::*;
use bevy::{app::App, core_pipeline::clear_color::ClearColorConfig};
Expand Down Expand Up @@ -79,7 +79,7 @@ impl Plugin for GamePlugin {
GameSet::Finish.run_if(in_state(GameState::Finish)),
);

#[cfg(debug_assertions)]
#[cfg(feature = "dev")]
{
app.add_plugins((FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin::default()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub fn bark(
return;
};

if input.pressed(KeyCode::Space) {
if input.just_pressed(KeyCode::Space) {
event_writer.send(Bark {
radius: 10.,
position: bark.translation,
Expand Down
9 changes: 4 additions & 5 deletions src/sheep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
time::Duration,
};

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

use crate::{
Expand Down Expand Up @@ -156,7 +156,7 @@ impl StateChance {
return *d;
}
}
return Decision::Idle;
Decision::Idle
}
}

Expand Down Expand Up @@ -268,14 +268,13 @@ fn init_safeareawalk_walk(
pub fn sheep_state(
mut commands: Commands,
state_matrix: Res<StateChance>,
time: Res<Time>,
mut sheeps: Query<(Entity, &mut Decision, &mut Sheep), Without<IsScared>>,
mut sheeps: Query<(Entity, &mut Decision, &Sheep), Without<IsScared>>,
mut init_random_walk: EventWriter<InitRandomWalk>,
mut init_safe_walk: EventWriter<SafeAreaWalk>,
mut init_escape_walk: EventWriter<EscapeWalk>,
) {
let mut rand = rand::thread_rng();
for (e, mut dec, mut sheep) in &mut sheeps.iter_mut() {
for (e, mut dec, _sheep) in &mut sheeps.iter_mut() {
if *dec == Decision::Idle {
let next_dec = state_matrix.select_next(&mut rand);

Expand Down
2 changes: 1 addition & 1 deletion src/storyteller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ fn level_timer(
) {
for mut timer in timers.iter_mut() {
let level_time = time.elapsed_seconds() - teller.level_start_time;
if (teller.level_duration - level_time > 0.0) {
if teller.level_duration - level_time > 0.0 {
let dur = Duration::from_secs_f32(teller.level_duration - level_time);

let time = format!("{:02}:{:02}", dur.as_secs() / 60, dur.as_secs() % 60);
Expand Down
4 changes: 1 addition & 3 deletions src/test_level.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use bevy::{
core_pipeline::clear_color::ClearColorConfig, pbr::CascadeShadowConfigBuilder, prelude::*,
};
use bevy::{pbr::CascadeShadowConfigBuilder, prelude::*};
use rand::prelude::*;
use std::f32::consts::PI;

Expand Down
42 changes: 36 additions & 6 deletions src/wolf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
get_sprite_rotation,
physics::{Velocity, WalkController},
player::{Bark, DOG_SPEED},
safe_area::OutOfSafeArea,
safe_area::{OutOfSafeArea, SafeArea},
test_level::LevelSize,
GameStuff,
};
Expand All @@ -25,6 +25,7 @@ impl Plugin for WolfPlugin {
catch_system,
eating_system,
go_out_system,
run_out_system,
bark,
apply_deferred,
)
Expand Down Expand Up @@ -137,9 +138,9 @@ fn catch_system(
fn eating_system(
mut commands: Commands,
time: Res<Time>,
mut wolfs: Query<(Entity, &mut Eating, &mut WalkController)>,
mut wolfs: Query<(Entity, &Transform, &mut Eating, &mut WalkController)>,
) {
for (wolf, mut eating, mut walk_controller) in wolfs.iter_mut() {
for (wolf, wolf_transform, mut eating, mut walk_controller) in wolfs.iter_mut() {
eating.time -= time.delta_seconds();
if eating.time <= 0.0 {
commands.entity(wolf).remove::<Eating>().insert(GoOut);
Expand All @@ -151,16 +152,45 @@ fn eating_system(

fn go_out_system(
mut commands: Commands,
mut wolfs: Query<(Entity, &mut Transform, &mut WalkController, &GoOut)>,
mut wolfs: Query<(Entity, &Transform, &mut WalkController, &GoOut)>,
safearea: Query<&SafeArea>,
level_size: Res<LevelSize>,
) {
for (wolf, mut wolf_transform, mut walk_controller, go_out) in wolfs.iter_mut() {
for (wolf, wolf_transform, mut walk_controller, _go_out) in wolfs.iter_mut() {
let dir = wolf_transform.translation.normalize();
walk_controller.target_velocity = dir * WOLF_SPEED;

if wolf_transform.translation.distance(Vec3::ZERO) > level_size.0 * 3.0 {
commands.entity(wolf).despawn_recursive();
}
if safearea.iter().any(|area| {
area.in_area(Vec2 {
x: wolf_transform.translation.x,
y: wolf_transform.translation.z,
})
}) {
commands.entity(wolf).insert(GoOut);
}
}
}

fn run_out_system(
mut commands: Commands,
mut wolfs: Query<(Entity, &Transform, &mut WalkController), (With<Wolf>, Without<GoOut>)>,
safearea: Query<&SafeArea>,
) {
for (wolf, wolf_transform, mut walk_controller) in wolfs.iter_mut() {
let in_safe_area = safearea.iter().filter(|area| {
area.in_area(Vec2 {
x: wolf_transform.translation.x,
y: wolf_transform.translation.z,
})
});
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);
}
}
}

Expand All @@ -169,7 +199,7 @@ fn bark(
mut wolfs: Query<(Entity, &Transform), With<Wolf>>,
mut barks: EventReader<Bark>,
) {
let Some(bark) = barks.iter().next() else {
let Some(bark) = barks.read().next() else {
return;
};

Expand Down