Class: Spinoza::Timeline
- Inherits:
-
Object
- Object
- Spinoza::Timeline
- Defined in:
- lib/spinoza/system/timeline.rb
Overview
A timeline of events, future and past, and a current time.
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#future ⇒ Object
readonly
Returns the value of attribute future.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#now ⇒ Object
readonly
Returns the value of attribute now.
Instance Method Summary collapse
-
#evolve(dt) ⇒ Object
Dispatches all events in sequence for the next
dt
units of time, in other words, all events scheduled in the interval ‘now..now+dt`. -
#initialize ⇒ Timeline
constructor
A new instance of Timeline.
- #schedule(event) ⇒ Object (also: #<<)
-
#step ⇒ Object
Dispatches the next event.
Constructor Details
#initialize ⇒ Timeline
Returns a new instance of Timeline.
31 32 33 34 35 |
# File 'lib/spinoza/system/timeline.rb', line 31 def initialize @history = MultiRBTree.new @future = MultiRBTree.new @now = 0.0 end |
Instance Attribute Details
#future ⇒ Object (readonly)
Returns the value of attribute future.
29 30 31 |
# File 'lib/spinoza/system/timeline.rb', line 29 def future @future end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
29 30 31 |
# File 'lib/spinoza/system/timeline.rb', line 29 def history @history end |
#now ⇒ Object (readonly)
Returns the value of attribute now.
29 30 31 |
# File 'lib/spinoza/system/timeline.rb', line 29 def now @now end |
Instance Method Details
#evolve(dt) ⇒ Object
Dispatches all events in sequence for the next dt
units of time, in other words, all events scheduled in the interval ‘now..now+dt`. If several events are scheduled at the same time, dispatches the one that was scheduled first. Dispatched events are stored in a history timeline. Returns the time at the end of the interval, which may be later than the time of any dispatched event. Advances time even if no events exist in the interval.
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/spinoza/system/timeline.rb', line 60 def evolve dt t_end = now + dt loop do time, event = @future.first break if not time or time > t_end @future.shift dispatch_event event yield event if block_given? end @now = t_end end |
#schedule(event) ⇒ Object Also known as: <<
37 38 39 |
# File 'lib/spinoza/system/timeline.rb', line 37 def schedule event @future[event.time] = event end |
#step ⇒ Object
Dispatches the next event. If several events are scheduled at the same time, dispatches the one that was scheduled first. Returns nil if nothing is scheduled, otherwise returns current time, which is when the event was dispatched. Dispatched events are stored in a history timeline. Does not advance time if there is no event scheduled.
47 48 49 50 51 52 |
# File 'lib/spinoza/system/timeline.rb', line 47 def step time, event = @future.shift return nil unless time dispatch_event event return now end |