Skip to content

Commit 313b1da

Browse files
author
Travis Scholtens
committed
Allow Speed scheduler to accept an initial time
Adds an additional optional time argument to Scheduler.Speed.add(), which defines an initial time to schedule the item. If the item is repeated then subsequent scheduling will use 1/item.getSpeed() as before. Also exposes an item's time through a Scheduler.getTimeOf(item) method. In combination, these two changes allow clients to completely recreate the state of the speed scheduler at a given point, useful when saving and restoring a game state.
1 parent a0345cb commit 313b1da

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

src/eventqueue.js

+11
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ ROT.EventQueue.prototype.get = function() {
5656
return this._events.splice(0, 1)[0];
5757
}
5858

59+
/**
60+
* Get the time associated with the given event
61+
* @param {?} event
62+
* @returns {number} time
63+
*/
64+
ROT.EventQueue.prototype.getEventTime = function(event) {
65+
var index = this._events.indexOf(event);
66+
if (index == -1) { return undefined }
67+
return this._eventTimes[index];
68+
}
69+
5970
/**
6071
* Remove an event from the queue
6172
* @param {?} event

src/scheduler/scheduler-speed.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ ROT.Scheduler.Speed.extend(ROT.Scheduler);
1010
/**
1111
* @param {object} item anything with "getSpeed" method
1212
* @param {bool} repeat
13+
* @param {number} [time=1/item.getSpeed()]
1314
* @see ROT.Scheduler#add
1415
*/
15-
ROT.Scheduler.Speed.prototype.add = function(item, repeat) {
16-
this._queue.add(item, 1/item.getSpeed());
16+
ROT.Scheduler.Speed.prototype.add = function(item, repeat, time) {
17+
this._queue.add(item, time !== undefined ? time : 1/item.getSpeed());
1718
return ROT.Scheduler.prototype.add.call(this, item, repeat);
1819
}
1920

src/scheduler/scheduler.js

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ ROT.Scheduler.prototype.add = function(item, repeat) {
2323
return this;
2424
}
2525

26+
/**
27+
* Get the time the given item is scheduled for
28+
* @param {?} item
29+
* @returns {number} time
30+
*/
31+
ROT.Scheduler.prototype.getTimeOf = function(item) {
32+
return this._queue.getEventTime(item);
33+
}
34+
2635
/**
2736
* Clear all items
2837
*/

tests/spec/eventqueue.js

+19
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ describe("EventQueue", function() {
1717
expect(q.get()).toEqual(null);
1818
});
1919

20+
it("should look up time of events", function() {
21+
var q = new ROT.EventQueue();
22+
q.add(123, 187);
23+
q.add(456, 42);
24+
expect(q.getEventTime(123)).toEqual(187);
25+
expect(q.getEventTime(456)).toEqual(42);
26+
});
27+
28+
it("should look up correct times after events removed", function() {
29+
var q = new ROT.EventQueue();
30+
q.add(123, 187);
31+
q.add(456, 42);
32+
q.add(789, 411);
33+
q.get();
34+
expect(q.getEventTime(456)).toBeUndefined();
35+
expect(q.getEventTime(123)).toEqual(187 - 42);
36+
expect(q.getEventTime(789)).toEqual(411 - 42);
37+
});
38+
2039
it("should remove events", function() {
2140
var q = new ROT.EventQueue();
2241
q.add(123, 0);

tests/spec/scheduler.js

+17
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@ describe("Scheduler", function() {
8989
for (var i=0;i<7;i++) { result.push(S.next()); }
9090
expect(result).toSchedule([A200, A100a, A200, A200, A50, A100a, A200]);
9191
});
92+
93+
it("should schedule with initial offsets", function() {
94+
S.add(A50, true, 1/300);
95+
S.add(A100a, true, 0);
96+
S.add(A200, true);
97+
var result = [];
98+
for (var i=0;i<9;i++) { result.push(S.next()); }
99+
expect(result).toSchedule([A100a, A50, A200, A100a, A200, A200, A100a, A200, A50]);
100+
});
101+
102+
it("should look up the time of an event", function() {
103+
S.add(A100a, true);
104+
S.add(A50, true, 1/200);
105+
expect(S.getTimeOf(A50)).toEqual(1/200);
106+
expect(S.getTimeOf(A100a)).toEqual(1/100);
107+
});
108+
92109
});
93110

94111
describe("Action", function() {

0 commit comments

Comments
 (0)