use crate::game::*; use crate::iso::*; use crate::player::{facing_of, MainCamera, Mover, Pad, Player}; use crate::sheets::CharSheets; use crate::sprites::{self, Spr}; use crate::ui::Fade; use crate::world::{EventSpot, Glow}; use bevy::prelude::*; use bevy::sprite::Anchor; const DAY: f32 = 1440.0; const FLAMES: [&Spr; 4] = [&sprites::FLAME0, &sprites::FLAME1, &sprites::FLAME2, &sprites::FLAME3]; pub const BASE_CLEAR: Vec3 = Vec3::new(0.11, 0.16, 0.10); const DUSK: Vec3 = Vec3::new(0.9, 0.78, 0.72); pub const NIGHT: Vec3 = Vec3::new(0.42, 0.48, 0.72); const RAIN_COUNT: usize = 120; pub struct TravelPlugin; impl Plugin for TravelPlugin { fn build(&self, app: &mut App) { app.init_resource::().add_systems( Update, (requests, clock, travel, needs, eat, camp_key, roadside, camp_flame, camp_light, rain).chain(), ); } } #[derive(Clone, Copy, Default)] enum Phase { #[default] Idle, Wait(f32), Sleep { left: f32, camp: bool }, } #[derive(Resource, Default)] pub struct Travel { phase: Phase, linger: f32, rain_since: Option, } impl Travel { pub fn busy(&self) -> bool { !matches!(self.phase, Phase::Idle) } } #[derive(Component)] struct CampPart; #[derive(Component)] pub struct CampLight(pub Vec2); #[derive(Component)] pub struct CampFlame { frames: [Handle; 4], idx: usize, timer: f32, } #[derive(Component)] pub struct Bandit; #[derive(Component)] pub struct Rain; fn total(g: &Game) -> f32 { g.day as f32 * DAY + g.minutes } pub fn day_tint(h: f32) -> Vec3 { let day = Vec3::ONE; if (7.0..18.0).contains(&h) { day } else if (18.0..19.0).contains(&h) { day.lerp(DUSK, h - 18.0) } else if (19.0..21.0).contains(&h) { DUSK.lerp(NIGHT, (h - 19.0) / 2.0) } else if h >= 21.0 || h < 5.0 { NIGHT } else if h < 6.0 { NIGHT.lerp(DUSK, h - 5.0) } else { DUSK.lerp(day, h - 6.0) } } pub fn night_amount(h: f32) -> f32 { ((1.0 - day_tint(h).y) / (1.0 - NIGHT.y)).clamp(0.0, 1.0) } pub fn rnd(seed: &mut u32) -> f32 { let mut x = (*seed).max(1); x ^= x << 13; x ^= x >> 17; x ^= x << 5; *seed = x; x as f32 / u32::MAX as f32 } fn requests( mut commands: Commands, assets: Res, mut g: ResMut, mut t: ResMut, pq: Query<&Mover, With>, camp_q: Query>, ) { if !matches!(t.phase, Phase::Idle) { return; } let Some(req) = g.request.take() else { return }; match req { Req::Wait { minutes } => t.phase = Phase::Wait(minutes), Req::Sleep { until, camp } => { if camp { for e in &camp_q { commands.entity(e).despawn(); } if let Ok(m) = pq.single() { spawn_camp(&mut commands, &assets, m.pos, g.has(Item::Firewood)); } g.camping = true; t.linger = 0.0; } let left = (until - g.minutes).rem_euclid(DAY); t.phase = Phase::Sleep { left, camp }; } Req::BreakCamp => { for e in &camp_q { commands.entity(e).despawn(); } g.camping = false; t.linger = 0.0; } } } fn spawn_camp(commands: &mut Commands, assets: &AssetServer, p: Vec2, fire: bool) { let tent = p + Vec2::new(1.0, -1.0); commands.spawn(( Sprite::from_image(assets.load(sprites::TENT.path)), anchor(&sprites::TENT), Transform::from_translation(to_screen(tent).extend(depth(tent + Vec2::splat(0.5)))).with_scale(Vec3::splat(PX)), CampPart, )); let fire_at = p + Vec2::new(1.0, 1.0); let z = depth(fire_at + Vec2::splat(0.5)); commands.spawn(( Sprite::from_image(assets.load(sprites::CAMPFIRE.path)), anchor(&sprites::CAMPFIRE), Transform::from_translation(to_screen(fire_at).extend(z)).with_scale(Vec3::splat(PX)), CampPart, )); if !fire { return; } commands.spawn(( Sprite::from_image(assets.load(FLAMES[0].path)), anchor(FLAMES[0]), Transform::from_translation(to_screen(fire_at).extend(z + 0.002)).with_scale(Vec3::splat(PX)), CampFlame { frames: FLAMES.map(|s| assets.load(s.path)), idx: 0, timer: 0.0 }, CampPart, )); commands.spawn(( Sprite { image: assets.load(sprites::LIGHT.path), color: Color::srgba(1.0, 0.7, 0.35, 0.6), ..default() }, Transform::from_translation((to_screen(fire_at) + Vec2::new(0.0, 40.0)).extend(9.5)).with_scale(Vec3::splat(PX * 2.5)), CampLight(fire_at), CampPart, )); } fn clock(time: Res