use crate::GameState; use avian3d::prelude::{LinearVelocity, Position}; use bevy::{math::ops::sin, prelude::*}; use lightyear::prelude::LocalTimeline; use serde::{Deserialize, Serialize}; #[derive(Component, Reflect, Default, Debug, Serialize, Deserialize, PartialEq)] #[reflect(Component)] pub struct ActivePlatform { pub start: Vec3, pub target: Vec3, } pub fn plugin(app: &mut App) { app.register_type::(); app.add_systems( FixedUpdate, move_active.run_if(in_state(GameState::Playing)), ); } fn move_active( #[cfg(not(feature = "server"))] time: Single<&LocalTimeline>, #[cfg(feature = "server")] time: Single<&LocalTimeline, With>, bevy_time: Res>, mut platforms: Query<(&Position, &ActivePlatform, &mut LinearVelocity)>, ) { for (position, active, mut velocity) in platforms.iter_mut() { let now = time.now.as_duration(bevy_time.delta()).as_secs_f32(); let t = (sin(now * 0.4) + 1.) / 2.; let target = active.start.lerp(active.target, t); let prev = position.0; velocity.0 = (target - prev) / bevy_time.delta_secs(); } }