Skip to content

Commit

Permalink
Sheep counter
Browse files Browse the repository at this point in the history
  • Loading branch information
ayamaev-se committed Dec 6, 2023
1 parent 1375eeb commit c685177
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 6 deletions.
45 changes: 39 additions & 6 deletions src/debug_diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
use bevy::prelude::*;

use crate::safe_area::SheepCounter;

pub struct DiagnosticPlugin;

impl Plugin for DiagnosticPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Startup,
(setup_diagnostic_panel, apply_deferred, setup_counter),
(setup_diagnostic_panel, apply_deferred, setup_counter, setup_sheep_counter).chain(),
)
.add_systems(Update, (fps_counting,))
.add_systems(Update, (
fps_counting,
sheep_counter_text,
))
.add_plugins(bevy_inspector_egui::quick::WorldInspectorPlugin::default());
}
}
Expand All @@ -22,16 +27,18 @@ pub fn setup_diagnostic_panel(mut commands: Commands) {
top: Val::Px(0.0),
left: Val::Px(0.0),
width: Val::Px(100.0),
height: Val::Percent(100.0),
position_type: PositionType::Absolute,
flex_direction: FlexDirection::Row,
align_items: AlignItems::Center,
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()
});
}).insert(DiagnosticPanel);
}

#[derive(Component)]
Expand All @@ -53,3 +60,29 @@ fn fps_counting(mut query: Query<&mut Text, With<FrameCounter>>, time: Res<Time>
text.sections[0].value = format!("FPS: {:.0}", 1.0 / time.delta_seconds());
}
}

#[derive(Component)]
pub struct ShipDebugCounter;

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

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

pub fn sheep_counter_text(
mut query: Query<&mut Text, With<ShipDebugCounter>>,
sheep_counter : Res<SheepCounter>,
) {
for mut text in &mut query {
text.sections[0].value = format!("Sheep in safe area: {}", sheep_counter.count);
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ impl Plugin for GamePlugin {
sprite_material::SpriteMaterialPlugin,
));

//For long term updates
app.insert_resource(Time::<Fixed>::from_seconds(1.0));

app.add_systems(Startup, (test_level::setup, sheep::setup));
// TODO: Move to plugin
app.add_systems(
Expand Down
45 changes: 45 additions & 0 deletions src/safe_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ use std::f32::consts::PI;

use bevy::prelude::*;

use crate::sheep::Sheep;

pub struct SafeAreaPlugin;

impl Plugin for SafeAreaPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, draw_safe_area);

app.add_systems(FixedUpdate, count_sheeps);

app.init_resource::<SheepCounter>();
}
}

Expand All @@ -18,6 +24,24 @@ pub enum SafeArea {
Ellipse { pos1: Vec2, pos2: Vec2, radius: f32 },
}

impl SafeArea {
fn in_area(&self, sheep_pos: Vec2) -> bool {
match self {
SafeArea::Rect { pos, size } => {
let dx = sheep_pos.x - pos.x;
let dy = sheep_pos.y - pos.y;

dx.abs() < size.x / 2.0 && dy.abs() < size.y / 2.0
},
SafeArea::Ellipse { pos1, pos2, radius } => {
let d = (*pos1 - *pos2).length();
let r = (*pos1 - sheep_pos).length();
r * r <= d * d
},
}
}
}

fn draw_safe_area(mut gizmos: Gizmos, query: Query<&SafeArea>) {
for safe_area in query.iter() {
match safe_area {
Expand All @@ -33,3 +57,24 @@ fn draw_safe_area(mut gizmos: Gizmos, query: Query<&SafeArea>) {
}
}
}

#[derive(Resource, Default)]
pub struct SheepCounter {
pub count: u32,
}

fn count_sheeps(
mut safe_areas : Query<&SafeArea>,
mut sheep : Query<&Transform, With<Sheep>>,
mut counter : ResMut<SheepCounter>,
) {
let mut count = 0;
for safe_area in safe_areas.iter() {
for sheep in sheep.iter() {
if safe_area.in_area(Vec2::new(sheep.translation.x, sheep.translation.z)) {
count += 1;
}
}
}
counter.count = count;
}
1 change: 1 addition & 0 deletions src/sheep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub fn scared_sheeps(
if sheep.1.translation.distance(bark_origin) <= bark.radius {
sheep.3 .0 = true;
sheep.2 .0 = sheep.1.translation - bark_origin;
sheep.2 .0.y = 0.0; //sheep must not fly and be in fixed height
}
}
}
Expand Down

0 comments on commit c685177

Please sign in to comment.