Skip to content

Commit

Permalink
Telegraph moving
Browse files Browse the repository at this point in the history
  • Loading branch information
Nertsal committed Aug 14, 2023
1 parent 9509eab commit acefeef
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion assets/level.ron
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Level(
),
telegraph: (
precede_time: 1.0,
duration: 2.0,
speed: 2.0,
),
)),
),
Expand Down
6 changes: 3 additions & 3 deletions src/model/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ pub struct LightEvent {
pub struct Telegraph {
/// How long before the event should the telegraph occur (in beats).
pub precede_time: Time,
/// How long will the telegraph be visible (in beats).
pub duration: Time,
/// How fast the telegraph is.
pub speed: Coord,
}

impl Level {
Expand Down Expand Up @@ -79,7 +79,7 @@ impl Default for Telegraph {
fn default() -> Self {
Self {
precede_time: Time::new(1.0),
duration: Time::new(2.0),
speed: Coord::new(1.0),
}
}
}
9 changes: 6 additions & 3 deletions src/model/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ pub struct Light {
pub struct LightTelegraph {
/// The light to telegraph.
pub light: Light,
/// Lifetime of the telegraph.
pub lifetime: Lifetime,
/// How fast the telegraph is.
pub speed: Coord,
/// Time since creation.
pub lifetime: Time,
/// The time until the actual light is spawned.
pub spawn_timer: Time,
}
Expand All @@ -25,7 +27,8 @@ impl Light {
pub fn into_telegraph(self, telegraph: Telegraph, beat_time: Time) -> LightTelegraph {
LightTelegraph {
light: self,
lifetime: Lifetime::new_max(telegraph.duration * beat_time),
speed: telegraph.speed,
lifetime: Time::ZERO,
spawn_timer: telegraph.precede_time * beat_time,
}
}
Expand Down
26 changes: 17 additions & 9 deletions src/model/logic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,30 @@ impl Model {

// Update telegraphs
for tele in &mut self.telegraphs {
tele.lifetime.change(-delta_time);
tele.lifetime += delta_time;
if tele.spawn_timer > Time::ZERO {
tele.spawn_timer -= delta_time;
if tele.spawn_timer <= Time::ZERO {
self.lights.push(tele.light.clone());
continue;
}
}

let t = 1.0 - tele.lifetime.get_ratio().as_f32(); // 0 to 1
let t = crate::util::smoothstep(t);
tele.light.collider = tele.light.base_collider.transformed(Transform {
scale: r32(t),
..default()
});
let t = tele.lifetime * tele.speed;
let transform = if t > tele.light.movement.duration() {
Transform {
scale: Coord::ZERO,
..default()
}
} else {
tele.light.movement.get(t)
};
tele.light.collider = tele.light.base_collider.transformed(transform);
}
self.telegraphs.retain(|tele| tele.lifetime.is_above_min());
self.telegraphs.retain(|tele| {
tele.spawn_timer > Time::ZERO
|| tele.lifetime * tele.speed < tele.light.movement.duration()
});

// Update lights
for light in &mut self.lights {
Expand Down Expand Up @@ -99,7 +107,7 @@ impl Model {
self.random_light().into_telegraph(
Telegraph {
precede_time: r32(1.0),
duration: r32(2.0),
speed: r32(1.0),
},
self.level.beat_time(),
)
Expand Down

0 comments on commit acefeef

Please sign in to comment.