bevy_flights/flights/
circle.rs

1use bevy_math::Vec2;
2
3use super::fixed::FixedTranslation2d;
4
5use crate::prelude::{Accelerate, TimeOffset};
6use crate::traits::Translation2dDescriptor;
7use crate::composites::{TranslationSum2d, Scale2d, Feed};
8
9/// Corresponds to spinning in circles
10/// around the origin
11/// but only one the horizontal axis
12pub struct HorizontalCircleFlight;
13impl Translation2dDescriptor for HorizontalCircleFlight {
14    fn translation(&self, t: f32) -> bevy_math::Vec2 {
15        Vec2::X * (std::f32::consts::TAU * t).cos()
16    }
17}
18
19// corresponds to spinning on circles,
20// around the origin
21// but only one the vertical axis.
22pub struct VerticalUnitCircleFlight;
23impl Translation2dDescriptor for VerticalUnitCircleFlight {
24    fn translation(&self, t: f32) -> bevy_math::Vec2 {
25        Vec2::Y * (std::f32::consts::TAU * t).sin()
26    }
27}
28
29/// Describe a complete circle flight around the origin
30/// time = 0 corresponds to being at the right of the circle
31pub type UnitCircleFlight = TranslationSum2d<HorizontalCircleFlight, VerticalUnitCircleFlight>;
32impl UnitCircleFlight { 
33    const VALUE: Self = TranslationSum2d::sum(HorizontalCircleFlight, VerticalUnitCircleFlight);
34}
35
36/// A circle flight not centered around the origin
37pub type CircleFlight = 
38    TranslationSum2d<
39        FixedTranslation2d, 
40        Feed< 
41            TimeOffset, 
42            Feed<
43                Accelerate,
44                Scale2d<UnitCircleFlight>
45            >
46        >
47    >
48;
49impl CircleFlight {
50    pub fn new(center: Vec2, radius: f32, frequency: f32, time_offset: f32) -> Self {
51        Self::sum(
52            FixedTranslation2d::new(center),
53            Feed::new(
54                TimeOffset(time_offset),
55                Feed::new(
56                    Accelerate(frequency),
57                    Scale2d::new(
58                        radius,
59                        UnitCircleFlight::VALUE
60                    )))
61        )
62    }
63}