Skip to content

Commit

Permalink
Merge pull request #4 from rewin123/move-to-safezone
Browse files Browse the repository at this point in the history
sheeps-move-to-inside-safearea
  • Loading branch information
naomijub authored Dec 7, 2023
2 parents bc3a16a + 908a3f3 commit 5b4d825
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 114 deletions.
5 changes: 3 additions & 2 deletions src/debug_diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ impl Plugin for DiagnosticPlugin {
.chain(),
)
.add_systems(Update, (fps_counting, sheep_counter_text));

#[cfg(debug_assertions)] {

#[cfg(debug_assertions)]
{
app.add_plugins(bevy_inspector_egui::quick::WorldInspectorPlugin::default());
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ impl Plugin for GamePlugin {

#[cfg(debug_assertions)]
{

app.add_plugins((FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin::default()));
}

Expand All @@ -60,7 +59,6 @@ impl Plugin for GamePlugin {
app.insert_resource(Time::<Fixed>::from_seconds(1.0));

app.add_systems(Startup, (test_level::setup, sheep::setup));

}
}

Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use bevy::DefaultPlugins;
use bevy_game::GamePlugin;
// ToDo: Replace bevy_game with your new crate name.
use bevy::asset::AssetMetaCheck;
use bevy_game::test_level::LevelSize;
use std::io::Cursor;
use winit::window::Icon;

Expand All @@ -17,6 +18,7 @@ fn main() {
.insert_resource(Msaa::Off)
.insert_resource(AssetMetaCheck::Never)
.insert_resource(ClearColor(Color::rgb(0.4, 0.4, 0.4)))
.init_resource::<LevelSize>()
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
Expand Down
16 changes: 5 additions & 11 deletions src/physics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ pub struct PhysicsPlugin;

impl Plugin for PhysicsPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, (
walk_system,
apply_velocity,
).chain());
app.add_systems(Update, (walk_system, apply_velocity).chain());
}
}

Expand All @@ -27,15 +24,12 @@ pub struct WalkController {
pub max_speed: f32,
}

fn walk_system(
time: Res<Time>,
mut query: Query<(&mut Velocity, &mut WalkController)>,
) {
for (mut velocity, mut controller) in query.iter_mut() {
fn walk_system(time: Res<Time>, mut query: Query<(&mut Velocity, &mut WalkController)>) {
for (mut velocity, controller) in query.iter_mut() {
let dspeed = controller.target_velocity - velocity.0;
let accel = controller.acceleration.min(dspeed.length() * 100.0);

velocity.0 += dspeed.normalize_or_zero() * accel * time.delta_seconds();
velocity.0 = velocity.0.clamp_length_max(controller.max_speed);
}
}
}
17 changes: 17 additions & 0 deletions src/safe_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::f32::consts::PI;

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

use crate::sheep::Sheep;

Expand Down Expand Up @@ -45,6 +46,22 @@ impl SafeArea {
}
}

pub fn get_random_point_inside(&self, level_size: f32) -> Vec3 {
let mut rng = rand::thread_rng();
let v2 = (0..)
.map(|_| Vec2 {
x: rng.gen_range(-level_size..level_size),
y: rng.gen_range(-level_size..level_size),
})
.find(|point| self.in_area(*point))
.unwrap();
Vec3 {
x: v2.x,
y: 0.0,
z: v2.y,
}
}

pub fn get_center(&self) -> Vec3 {
match self {
SafeArea::Rect { pos, size: _ } => Vec3::new(pos.x, 0.0, pos.y),
Expand Down
Loading

0 comments on commit 5b4d825

Please sign in to comment.