Skip to content

Commit

Permalink
Worked torches
Browse files Browse the repository at this point in the history
  • Loading branch information
rewin123 committed Dec 9, 2023
1 parent 6a3e244 commit a3e120e
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 34 deletions.
72 changes: 59 additions & 13 deletions src/safe_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

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

use bevy::prelude::*;
use bevy::{prelude::*, transform::commands};
use rand::Rng;

use crate::sheep::Sheep;
use crate::{sheep::Sheep, storyteller::Storyteller};

pub struct SafeAreaPlugin;

Expand All @@ -19,12 +19,18 @@ impl Plugin for SafeAreaPlugin {
}
}

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

#[derive(Component)]
pub struct LandSafeArea {
pub start_area: SafeArea,
} //Mark for day safe area

impl SafeArea {
pub fn in_area(&self, sheep_pos: Vec2) -> bool {
match self {
Expand All @@ -43,6 +49,9 @@ impl SafeArea {
let r = (*pos1 - sheep_pos).length();
r * r <= d * d
}
SafeArea::Circle { pos, radius } => {
(*pos - sheep_pos).length() < *radius
}
}
}

Expand Down Expand Up @@ -70,11 +79,41 @@ 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)
}
}
}

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::Circle { pos, radius } => {
SafeArea::Circle {
pos: *pos,
radius: *radius * scale,
}
},
}
}
}

fn draw_safe_area(mut gizmos: Gizmos, query: Query<&SafeArea>) {
#[derive(Component)]
pub struct HiddenSafeArea;

fn draw_safe_area(mut gizmos: Gizmos, query: Query<&SafeArea, Without<HiddenSafeArea>>) {
for safe_area in query.iter() {
match safe_area {
SafeArea::Rect { pos, size } => {
Expand All @@ -90,6 +129,9 @@ fn draw_safe_area(mut gizmos: Gizmos, query: Query<&SafeArea>) {
pos2: _,
radius: _,
} => {}
SafeArea::Circle { pos, radius } => {
gizmos.circle(Vec3::new(pos.x, 0.001, pos.y), Vec3::Y, *radius, Color::ORANGE);
}
}
}
}
Expand All @@ -109,15 +151,19 @@ fn count_sheeps(
mut counter: ResMut<SheepCounter>,
) {
let mut count = 0;
for safe_area in safe_areas.iter() {
for (e, sheep) in sheep.iter() {
for (e, sheep) in sheep.iter() {
let mut in_safe = false;
for safe_area in safe_areas.iter() {
if safe_area.in_area(Vec2::new(sheep.translation.x, sheep.translation.z)) {
count += 1;
commands.entity(e).remove::<OutOfSafeArea>();
} else {
commands.entity(e).insert(OutOfSafeArea);
in_safe = true;
}
}
if in_safe {
count += 1;
commands.entity(e).remove::<OutOfSafeArea>();
} else {
commands.entity(e).insert(OutOfSafeArea);
}
}
counter.count = count;
}
}
30 changes: 29 additions & 1 deletion src/sunday.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::f32::consts::PI;

use bevy::prelude::*;

use crate::{storyteller::Storyteller, GameSet};
use crate::{storyteller::Storyteller, GameSet, safe_area::{SafeArea, LandSafeArea}};

pub struct SundayPlugin;

Expand Down Expand Up @@ -33,6 +33,9 @@ impl Plugin for SundayPlugin {
app.add_systems(Update, sunday_system.in_set(GameSet::Playing));
app.add_systems(Update, set_day_state.in_set(GameSet::Playing));
app.add_state::<DayState>();

app.add_systems(Update, safe_area_evening_decrease.in_set(GameSet::Playing).run_if(in_state(DayState::Evening)));
app.add_systems(OnEnter(DayState::Night), delete_land_area_at_night);
}
}

Expand Down Expand Up @@ -116,4 +119,29 @@ fn sunday_system(
} else {

}
}



fn safe_area_evening_decrease(
mut areas : Query<(&mut SafeArea, &LandSafeArea)>,
time : Res<Time>,
teller : Res<Storyteller>
) {
let uniform_time = teller.get_level_unfirom_time(&time);
let evening_time = (uniform_time - DAY_TIME) / (EVENING_TIME - DAY_TIME);
let scale = 1.0 - evening_time;
for (mut area, land_area) in areas.iter_mut() {
*area = land_area.start_area.get_scaled(scale);
}
}


fn delete_land_area_at_night(
mut commands: Commands,
mut areas : Query<Entity, With<LandSafeArea>>,
) {
for entity in areas.iter_mut() {
commands.entity(entity).despawn_recursive();
}
}
18 changes: 11 additions & 7 deletions src/test_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rand::prelude::*;
use std::f32::consts::PI;

use crate::{
get_sprite_rotation, level_ui::CreateLevelUi, player::SpawnPlayer, safe_area::SafeArea,
get_sprite_rotation, level_ui::CreateLevelUi, player::SpawnPlayer, safe_area::{SafeArea, LandSafeArea},
sprite_material::create_plane_mesh, torch::SpawnTorch, GameStuff, sunday::{DAY_SUN_COLOR, SUN_BASE_ILLUMINANCE, AMBIENT_BASE_ILLUMINANCE}, shepherd::SpawnShepherd,
};

Expand Down Expand Up @@ -117,18 +117,22 @@ pub fn setup(
position: Vec3::new(-r - 2.0, 0.0, 0.0),
});

let num_of_torchs = 10;

let num_of_torchs = 20;
let torch_r = r / 2.0;
for _ in 0..num_of_torchs {
let pos = Vec3::new(rng.gen_range(-r..r), 0.0, rng.gen_range(-r..r));
let pos = Vec3::new(rng.gen_range(-torch_r..torch_r), 0.0, rng.gen_range(-torch_r..torch_r));

spawn_torch.send(SpawnTorch { position: pos });
}

let safe_area = SafeArea::Rect {
pos: Vec2::ZERO,
size: Vec2::new(r * 1.5, r * 1.5),
};
commands
.spawn(SafeArea::Rect {
pos: Vec2::ZERO,
size: Vec2::new(r * 1.5, r * 1.5),
.spawn(safe_area.clone())
.insert(LandSafeArea {
start_area : safe_area.clone()
})
.insert(GameStuff);

Expand Down
41 changes: 28 additions & 13 deletions src/torch.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use bevy::prelude::*;

use crate::{common_storage::CommonStorage, get_sprite_rotation, GameStuff, GameSet};
use crate::{common_storage::CommonStorage, get_sprite_rotation, GameStuff, GameSet, safe_area::{SafeArea, HiddenSafeArea}};

const TORCH_PATH: &str = "test/torch.png";

const TORCH_ILLUMINATION: f32 = 1000.0;
const TORCH_ILLUMINATION: f32 = 10000.0;


pub struct TorchPlugin;
Expand Down Expand Up @@ -62,25 +62,35 @@ fn spawn_torch(
) {
for event in events.read() {

let light_id = commands.spawn(PointLightBundle {
point_light: PointLight {
let torch_radius = 10.0;
let smooth_radius = torch_radius + 0.01;
let light_height = torch_radius * 0.5;

let outer_spot_angle = ((torch_radius / light_height) as f32).atan();
let inner_spot_angle = outer_spot_angle * 0.95;

let light_id = commands.spawn(SpotLightBundle {
spot_light: SpotLight {
color: Color::ORANGE,
intensity: 0.0,
range: 20.0,
radius: 0.3,
radius: 0.0,
range: torch_radius * 10.0,
shadows_enabled: false,
inner_angle: inner_spot_angle,
outer_angle: outer_spot_angle,
..default()
},
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)),
transform: Transform::from_translation(event.position + Vec3::Y * light_height).looking_at(event.position, Vec3::Z),
..default()
}).insert(TorchLight).id();
}).insert(TorchLight)
.insert(GameStuff).id();

let torch = TorchBase {
lit: false,
fuel: 0.0,
color: Color::ORANGE,
max_fuel: 1.0,
radius: 20.0,
radius: torch_radius,
light: light_id,
};

Expand All @@ -96,7 +106,7 @@ fn spawn_torch(
..default()
},
GameStuff,
)).add_child(light_id);
));
}
events.clear();
}
Expand All @@ -108,17 +118,22 @@ pub struct IgniteTorch {
}

fn ignite_torch(
mut commands : Commands,
mut events: EventReader<IgniteTorch>,
mut query: Query<(&mut TorchBase, &Transform)>,
mut lights : Query<&mut PointLight, With<TorchLight>>
mut query: Query<(Entity, &mut TorchBase, &Transform)>,
mut lights : Query<&mut SpotLight, With<TorchLight>>
) {
for event in events.read() {
for (mut torch, transform) in &mut query {
for (torch_e, mut torch, transform) in &mut query {
if (transform.translation - event.position).length() < event.radius {
torch.lit = true;
torch.fuel = torch.max_fuel;
if let Ok(mut light) = lights.get_mut(torch.light) {
light.intensity = TORCH_ILLUMINATION;
commands.entity(torch_e).insert(SafeArea::Circle {
pos: Vec2::new(transform.translation.x, transform.translation.z),
radius: torch.radius
}).insert(HiddenSafeArea);
};
}
}
Expand Down

0 comments on commit a3e120e

Please sign in to comment.