Skip to content

Commit

Permalink
Implemented simple heartbeat light animation
Browse files Browse the repository at this point in the history
  • Loading branch information
norru committed Jan 1, 2018
1 parent 36cd8f9 commit 0614c47
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
18 changes: 17 additions & 1 deletion src/backend/systems/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ use super::*;
use std::rc::Rc;
use std::cell::RefCell;
use backend::world::WorldState;
use backend::world::agent;
use core::clock::*;
use num_traits::clamp;

#[allow(unused)]
pub struct AnimationSystem {
speed: SpeedFactor,
heartbeat_scale: SpeedFactor,
dt: Seconds,
animation_timer: SharedTimer<SimulationTimer>,
simulation_timer: SharedTimer<SimulationTimer>,
animation_clock: TimerStopwatch<SimulationTimer>,
Expand All @@ -15,19 +19,31 @@ pub struct AnimationSystem {

impl Updateable for AnimationSystem {
fn update(&mut self, _: &WorldState, dt: Seconds) {
self.dt = dt;
self.simulation_timer.borrow_mut().tick(dt);
self.animation_timer.borrow_mut().tick(dt * self.speed);
}
}

impl System for AnimationSystem {}
impl System for AnimationSystem {
fn put_to_world(&self, world: &mut world::World) {
for (_, agent) in &mut world.agents_mut(agent::AgentType::Minion).iter_mut() {
if agent.state.is_active() {
let energy = agent.state.energy();
agent.state.heartbeat((self.dt.get() * self.speed * self.heartbeat_scale) as f32 * clamp(energy, 50.0f32, 200.0f32))
}
}
}
}

impl Default for AnimationSystem {
fn default() -> Self {
let animation_timer = Rc::new(RefCell::new(SimulationTimer::new()));
let simulation_timer = Rc::new(RefCell::new(SimulationTimer::new()));
AnimationSystem {
speed: 1.0,
heartbeat_scale: 1.0 / 60.0,
dt: seconds(0.0),
simulation_clock: TimerStopwatch::new(animation_timer.clone()),
animation_clock: TimerStopwatch::new(simulation_timer.clone()),
animation_timer,
Expand Down
7 changes: 5 additions & 2 deletions src/backend/world/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,7 @@ impl State {
}

pub fn phase(&self) -> f32 {
let age = self.lifecycle.elapsed();
age.into()
self.phase
}

pub fn consume(&mut self, q: f32) -> bool {
Expand Down Expand Up @@ -359,6 +358,10 @@ impl State {
self.target_position = position;
}

pub fn heartbeat(&mut self, d: f32) {
self.phase = (self.phase + d) % (2.0 * f32::consts::PI)
}

pub fn track_position(&mut self, position: &Position) {
self.trajectory.push(position.clone())
}
Expand Down
1 change: 1 addition & 0 deletions src/core/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ impl<T> Hourglass<T> where T: Timer {
left
}

#[allow(unused)]
pub fn elapsed(&self) -> Seconds {
self.stopwatch.elapsed()
}
Expand Down
11 changes: 5 additions & 6 deletions src/frontend/audio/multiplexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,11 @@ impl<T, S> SignalBuilder<T, S>

fn build(&self) -> Signal<T, [S; CHANNELS]>
where T: FloatConst {
Signal::<T, [S; CHANNELS]>::new(self.sample_rate,
self.tone.duration,
self.oscillator.clone()
.signal_function(self.tone.clone(),
self.envelope.clone(),
self.pan))
let f = self.oscillator.clone().signal_function(
self.tone.clone(),
self.envelope.clone(),
self.pan);
Signal::<T, [S; CHANNELS]>::new(self.sample_rate, self.tone.duration, f)
.with_delay(self.delay.time, self.delay.tail, self.delay.wet_dry, self.delay.feedback)
}

Expand Down

0 comments on commit 0614c47

Please sign in to comment.