Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
rewin123 committed Dec 9, 2023
1 parent a3e120e commit f554f7f
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 146 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ pub mod physics;
pub mod player;
pub mod safe_area;
pub mod sheep;
pub mod shepherd;
pub mod sprite_material;
pub mod storyteller;
pub mod sunday;
pub mod test_level;
pub mod torch;
pub mod wolf;
pub mod sunday;
pub mod shepherd;

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

Expand Down
3 changes: 2 additions & 1 deletion src/physics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ fn walk_system(time: Res<Time>, mut query: Query<(&mut Velocity, &mut WalkContro
let dspeed = controller.target_velocity - velocity.0;
let accel = controller.acceleration.min(dspeed.length() * 100.0);
let cur_vel = velocity.0;
velocity.0 += (dspeed.normalize_or_zero() * accel - cur_vel * AIR_RESISTANCE) * time.delta_seconds();
velocity.0 +=
(dspeed.normalize_or_zero() * accel - cur_vel * AIR_RESISTANCE) * time.delta_seconds();
velocity.0 = velocity.0.clamp_length_max(controller.max_speed);
}
}
13 changes: 8 additions & 5 deletions src/player.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use bevy::{input::mouse::MouseWheel, prelude::*, window::PrimaryWindow, pbr::{CascadeShadowConfig, CascadeShadowConfigBuilder}};
use bevy::{
input::mouse::MouseWheel,
pbr::{CascadeShadowConfig, CascadeShadowConfigBuilder},
prelude::*,
window::PrimaryWindow,
};

use crate::{
get_sprite_rotation,
Expand Down Expand Up @@ -224,7 +229,7 @@ fn camera_movement(
player_query: Query<&Transform, (With<Player>, Without<Camera>)>,
time: Res<Time>,
mut scroll_evr: EventReader<MouseWheel>,
mut sun : Query<&mut CascadeShadowConfig>
mut sun: Query<&mut CascadeShadowConfig>,
) {
let Ok((mut camera, mut distance)) = camera_query.get_single_mut() else {
return;
Expand All @@ -233,7 +238,6 @@ fn camera_movement(
return;
};


let Ok(mut sun) = sun.get_single_mut() else {
return;
};
Expand All @@ -247,7 +251,6 @@ fn camera_movement(
}

distance.0 = distance.0.clamp(10.0, 150.0);


let mut cascade = CascadeShadowConfigBuilder::default();
cascade.maximum_distance = distance.0 * 2.0;
Expand All @@ -267,7 +270,7 @@ fn set_cam_distance(
mut commands: Commands,
camera_without_dist: Query<(Entity, &Transform), (With<Camera>, Without<CameraDistance>)>,
player_query: Query<&Transform, With<Player>>,
mut sun : Query<&mut CascadeShadowConfig>
mut sun: Query<&mut CascadeShadowConfig>,
) {
let Ok(player) = player_query.get_single() else {
return;
Expand Down
51 changes: 23 additions & 28 deletions src/safe_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ impl Plugin for SafeAreaPlugin {

#[derive(Component, Clone)]
pub enum SafeArea {
Rect { pos: Vec2, size: Vec2},
Ellipse { pos1: Vec2, pos2: Vec2, radius: f32},
Circle { pos: Vec2, radius: f32},
Rect { pos: Vec2, size: Vec2 },
Ellipse { pos1: Vec2, pos2: Vec2, radius: f32 },
Circle { pos: Vec2, radius: f32 },
}

#[derive(Component)]
Expand All @@ -49,9 +49,7 @@ impl SafeArea {
let r = (*pos1 - sheep_pos).length();
r * r <= d * d
}
SafeArea::Circle { pos, radius } => {
(*pos - sheep_pos).length() < *radius
}
SafeArea::Circle { pos, radius } => (*pos - sheep_pos).length() < *radius,
}
}

Expand Down Expand Up @@ -79,32 +77,24 @@ impl SafeArea {
pos2,
radius: _,
} => Vec3::new((pos1.x + pos2.x) / 2.0, 0.0, (pos1.y + pos2.y) / 2.0),
SafeArea::Circle { pos, radius } => {
Vec3::new(pos.x, 0.0, pos.y)
}
SafeArea::Circle { pos, radius } => Vec3::new(pos.x, 0.0, pos.y),
}
}

pub fn get_scaled(&self, scale: f32) -> SafeArea {
match self {
SafeArea::Rect { pos, size } => {
SafeArea::Rect {
pos: *pos,
size: *size * scale,
}
}
SafeArea::Ellipse { pos1, pos2, radius } => {
SafeArea::Ellipse {
pos1: *pos1,
pos2: *pos2,
radius: *radius * scale,
}
SafeArea::Rect { pos, size } => SafeArea::Rect {
pos: *pos,
size: *size * scale,
},
SafeArea::Circle { pos, radius } => {
SafeArea::Circle {
pos: *pos,
radius: *radius * scale,
}
SafeArea::Ellipse { pos1, pos2, radius } => SafeArea::Ellipse {
pos1: *pos1,
pos2: *pos2,
radius: *radius * scale,
},
SafeArea::Circle { pos, radius } => SafeArea::Circle {
pos: *pos,
radius: *radius * scale,
},
}
}
Expand All @@ -130,7 +120,12 @@ fn draw_safe_area(mut gizmos: Gizmos, query: Query<&SafeArea, Without<HiddenSafe
radius: _,
} => {}
SafeArea::Circle { pos, radius } => {
gizmos.circle(Vec3::new(pos.x, 0.001, pos.y), Vec3::Y, *radius, Color::ORANGE);
gizmos.circle(
Vec3::new(pos.x, 0.001, pos.y),
Vec3::Y,
*radius,
Color::ORANGE,
);
}
}
}
Expand Down Expand Up @@ -166,4 +161,4 @@ fn count_sheeps(
}
}
counter.count = count;
}
}
56 changes: 31 additions & 25 deletions src/shepherd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
use bevy::{prelude::*, reflect::EnumInfo};

use crate::{player::{DOG_SPEED, DOG_ACCELERATION}, common_storage::CommonStorage, get_sprite_rotation, physics::{Velocity, WalkController}, GameSet, torch::{TorchBase, IgniteTorch}, sunday::DayState};
use crate::{
common_storage::CommonStorage,
get_sprite_rotation,
physics::{Velocity, WalkController},
player::{DOG_ACCELERATION, DOG_SPEED},
sunday::DayState,
torch::{IgniteTorch, TorchBase},
GameSet,
};

const SHEPHERD_PATH: &str = "test/Knight.png";

Expand All @@ -13,12 +21,10 @@ pub struct ShepherdPlugin;

impl Plugin for ShepherdPlugin {
fn build(&self, app: &mut App) {
app
.add_event::<SpawnShepherd>()
.add_systems(Update, (
spawn_shepherd_system,
ignite_all_torhes,
).in_set(GameSet::Playing)
app.add_event::<SpawnShepherd>()
.add_systems(
Update,
(spawn_shepherd_system, ignite_all_torhes).in_set(GameSet::Playing),
)
.add_systems(OnEnter(DayState::Evening), start_ignite_torches);
}
Expand All @@ -35,28 +41,25 @@ pub struct Shepherd;
#[derive(Component)]
pub struct IgniteAllTorhes;

fn start_ignite_torches(
mut commands: Commands,
query : Query<Entity, With<Shepherd>>
) {
fn start_ignite_torches(mut commands: Commands, query: Query<Entity, With<Shepherd>>) {
if let Ok(entity) = query.get_single() {
commands.entity(entity).insert(IgniteAllTorhes);
}
}

fn ignite_all_torhes(
mut commands: Commands,
mut query : Query<(Entity, &mut WalkController, &Transform), With<IgniteAllTorhes>>,
torches : Query<(&Transform, &TorchBase)>,
mut ignite : EventWriter<IgniteTorch>
mut query: Query<(Entity, &mut WalkController, &Transform), With<IgniteAllTorhes>>,
torches: Query<(&Transform, &TorchBase)>,
mut ignite: EventWriter<IgniteTorch>,
) {
let Ok((herd_entity, mut walk_controller, transform)) = query.get_single_mut() else {
return;
};

//find nearest torch
let mut nearest_torch : Option<Vec3> = None;
let mut nearest_torch_data : Option<&TorchBase> = None;
let mut nearest_torch: Option<Vec3> = None;
let mut nearest_torch_data: Option<&TorchBase> = None;
let mut dist = f32::MAX;
for (torch_transform, torch) in torches.iter() {
let dist_to_torch = (torch_transform.translation - transform.translation).length();
Expand All @@ -69,12 +72,13 @@ fn ignite_all_torhes(

if let Some(nearest_torch) = nearest_torch {
if dist < IGNITE_RADIUS {
ignite.send(IgniteTorch {
ignite.send(IgniteTorch {
position: transform.translation,
radius: IGNITE_RADIUS
});
radius: IGNITE_RADIUS,
});
} else {
walk_controller.target_velocity = (nearest_torch - transform.translation).normalize() * SHEPHERD_SPEED;
walk_controller.target_velocity =
(nearest_torch - transform.translation).normalize() * SHEPHERD_SPEED;
}
} else {
commands.entity(herd_entity).remove::<IgniteAllTorhes>();
Expand All @@ -86,14 +90,16 @@ fn spawn_shepherd_system(
mut commands: Commands,
mut events: EventReader<SpawnShepherd>,
asset_server: Res<AssetServer>,
common_storage : Res<CommonStorage>,
mut materials : ResMut<Assets<StandardMaterial>>,
common_storage: Res<CommonStorage>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
for event in events.read() {
commands.spawn((
Shepherd::default(),
PbrBundle {
transform: Transform::from_translation(event.pos).with_rotation(get_sprite_rotation()).with_scale(Vec3::new(1.0, 1.0, 2.0)),
transform: Transform::from_translation(event.pos)
.with_rotation(get_sprite_rotation())
.with_scale(Vec3::new(1.0, 1.0, 2.0)),
material: materials.add(StandardMaterial {
base_color_texture: Some(asset_server.load(SHEPHERD_PATH)),
..default()
Expand All @@ -106,8 +112,8 @@ fn spawn_shepherd_system(
max_speed: SHEPHERD_SPEED,
acceleration: SHEPHERD_ACCEL,
target_velocity: Vec3::ZERO,
}
},
));
}
events.clear();
}
}
9 changes: 4 additions & 5 deletions src/storyteller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ pub struct Storyteller {
}

impl Storyteller {
pub fn get_level_time(&self, time : &Time) -> f32 {
pub fn get_level_time(&self, time: &Time) -> f32 {
time.elapsed_seconds() - self.level_start_time
}

pub fn get_level_unfirom_time(&self, time : &Time) -> f32 {
pub fn get_level_unfirom_time(&self, time: &Time) -> f32 {
self.get_level_time(time) / self.level_duration
}
}
Expand Down Expand Up @@ -200,11 +200,10 @@ pub enum FailReason {
TaskFailed,
}


pub enum TaskStatus {
Active,
Done,
Failed
Failed,
}

#[derive(States, Debug, Clone, Eq, PartialEq, Hash, Default)]
Expand All @@ -214,4 +213,4 @@ pub enum GlobalTask {
SheepEscape,
WolfAttack,
CollectSheepInArea,
}
}
Loading

0 comments on commit f554f7f

Please sign in to comment.