rot-js
Version:
A roguelike toolkit in JavaScript
52 lines (51 loc) • 1.36 kB
JavaScript
import Scheduler from "./scheduler.js";
/**
* @class Action-based scheduler
* @augments ROT.Scheduler
*/
export default class Action extends Scheduler {
constructor() {
super();
this._defaultDuration = 1; /* for newly added */
this._duration = this._defaultDuration; /* for this._current */
}
/**
* @param {object} item
* @param {bool} repeat
* @param {number} [time=1]
* @see ROT.Scheduler#add
*/
add(item, repeat, time) {
this._queue.add(item, time || this._defaultDuration);
return super.add(item, repeat);
}
clear() {
this._duration = this._defaultDuration;
return super.clear();
}
remove(item) {
if (item == this._current) {
this._duration = this._defaultDuration;
}
return super.remove(item);
}
/**
* @see ROT.Scheduler#next
*/
next() {
if (this._current !== null && this._repeat.indexOf(this._current) != -1) {
this._queue.add(this._current, this._duration || this._defaultDuration);
this._duration = this._defaultDuration;
}
return super.next();
}
/**
* Set duration for the active item
*/
setDuration(time) {
if (this._current) {
this._duration = time;
}
return this;
}
}