use bevy::prelude::*; use crate::{ sim::{SimSystems, SimTick, TICKS_PER_HOUR, hour_of_day}, structures::{Structure, StructureKind}, world::{TilePos, WorldMap}, }; pub struct LightPlugin; impl Plugin for LightPlugin { fn build(&self, app: &mut App) { app.init_resource::() .add_systems(FixedUpdate, update_light_map.in_set(SimSystems::Decide)); } } pub const LIT_BRIGHTNESS: f32 = 0.5; pub const LIGHT_LOOKAHEAD_TICKS: u64 = TICKS_PER_HOUR * 3 / 2; const AMBIENT_STEPS: f32 = 16.0; const DAY_COLOR: Vec3 = Vec3::ONE; const NIGHT_COLOR: Vec3 = Vec3::new(0.2, 0.24, 0.4); pub fn daylight(hour: f32) -> f32 { match hour { h if h < 5.0 => 0.0, h if h < 7.0 => (h - 5.0) / 2.0, h if h < 18.0 => 1.0, h if h < 20.0 => 1.0 - (h - 18.0) / 2.0, _ => 0.0, } } pub fn ambient_color(daylight: f32) -> Vec3 { NIGHT_COLOR.lerp(DAY_COLOR, daylight) } pub fn brightness(color: Vec3) -> f32 { (color.x + color.y + color.z) / 3.0 } pub fn dark_soon(tick: SimTick) -> bool { let later = SimTick(tick.0 + LIGHT_LOOKAHEAD_TICKS); daylight(hour_of_day(tick)) < LIT_BRIGHTNESS || daylight(hour_of_day(later)) < LIT_BRIGHTNESS } pub fn light_at(kind: StructureKind, source: UVec2, tile: UVec2) -> Vec3 { let Some((radius, color)) = kind.light() else { return Vec3::ZERO; }; let d = source.as_vec2().distance(tile.as_vec2()) / radius; if d >= 1.0 { return Vec3::ZERO; } color * (1.0 - d * d).powi(2) } pub fn light_from(tile: UVec2, sources: impl IntoIterator) -> Vec3 { sources .into_iter() .map(|(pos, kind)| light_at(kind, pos, tile)) .sum() } #[derive(Resource, Default, Debug, Clone, PartialEq)] pub struct LightMap { size: UVec2, ambient_step: u32, ambient: Vec3, sources: Vec, } impl LightMap { pub fn color(&self, tile: UVec2) -> Vec3 { if self.sources.is_empty() || tile.x >= self.size.x || tile.y >= self.size.y { return Vec3::ONE; } (self.ambient + self.sources[(tile.y * self.size.x + tile.x) as usize]).min(Vec3::ONE) } pub fn is_lit(&self, tile: UVec2) -> bool { brightness(self.color(tile)) >= LIT_BRIGHTNESS } pub fn compute( size: UVec2, daylight: f32, sources: impl IntoIterator, ) -> Self { let ambient_step = (daylight * AMBIENT_STEPS).round() as u32; let mut light = vec![Vec3::ZERO; size.element_product() as usize]; for (pos, kind) in sources { let Some((radius, _)) = kind.light() else { continue; }; let r = radius.ceil() as i32; for dy in -r..=r { for dx in -r..=r { let tile = pos.as_ivec2() + IVec2::new(dx, dy); if tile.x < 0 || tile.y < 0 || tile.x >= size.x as i32 || tile.y >= size.y as i32 { continue; } let tile = tile.as_uvec2(); light[(tile.y * size.x + tile.x) as usize] += light_at(kind, pos, tile); } } } Self { size, ambient_step, ambient: ambient_color(ambient_step as f32 / AMBIENT_STEPS), sources: light, } } } fn update_light_map( tick: Res, map: Res, structures: Query<(&TilePos, &Structure)>, added: Query<(), Added>, mut removed: RemovedComponents, mut light: ResMut, ) { let day = daylight(hour_of_day(*tick)); let step = (day * AMBIENT_STEPS).round() as u32; let sources_changed = !added.is_empty() || removed.read().count() > 0; if !sources_changed && step == light.ambient_step && light.size == map.size { return; } *light = LightMap::compute( map.size, day, structures.iter().map(|(pos, s)| (pos.0, s.kind)), ); } #[cfg(test)] mod tests { use super::*; use crate::sim::TICKS_PER_HOUR; #[test] fn days_are_bright_and_nights_dark_with_gradual_dusk_and_dawn() { assert_eq!(daylight(12.0), 1.0); assert_eq!(daylight(23.0), 0.0); assert_eq!(daylight(2.0), 0.0); assert_eq!(daylight(19.0), 0.5); assert_eq!(daylight(6.0), 0.5); } // Building a fire takes time, so colonists start before it actually gets dark. #[test] fn darkness_is_anticipated_before_dusk() { let at = |hour: u64| SimTick((hour + 24 - 6) % 24 * TICKS_PER_HOUR); assert!(!dark_soon(at(12))); assert!(dark_soon(at(18))); assert!(dark_soon(at(23))); assert!(!dark_soon(at(8))); } #[test] fn fires_light_their_surroundings_with_falloff() { let fire = UVec2::new(10, 10); let near = light_at(StructureKind::Campfire, fire, UVec2::new(11, 10)); let far = light_at(StructureKind::Campfire, fire, UVec2::new(15, 10)); assert!(brightness(near) > brightness(far)); assert_eq!( light_at(StructureKind::Campfire, fire, UVec2::new(17, 10)), Vec3::ZERO ); } #[test] fn light_map_combines_ambient_and_fires() { let fire = (UVec2::new(5, 5), StructureKind::Campfire); let night = LightMap::compute(UVec2::splat(20), 0.0, [fire]); assert!(night.is_lit(UVec2::new(5, 6))); assert!(!night.is_lit(UVec2::new(18, 18))); let day = LightMap::compute(UVec2::splat(20), 1.0, []); assert!(day.is_lit(UVec2::new(18, 18))); } #[test] fn an_empty_light_map_counts_as_daylight() { assert!(LightMap::default().is_lit(UVec2::new(3, 3))); } #[test] fn beds_give_no_light() { let bed = UVec2::new(4, 4); assert_eq!(light_at(StructureKind::Bed, bed, bed), Vec3::ZERO); } }