Initial commit
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
const EventEmitter = require("events");
|
||||
|
||||
const Game = require("./Game");
|
||||
|
||||
class EventScheduler extends EventEmitter {
|
||||
constructor(typeID, id, mapID) {
|
||||
super();
|
||||
|
||||
this.entity = {
|
||||
typeID,
|
||||
id
|
||||
};
|
||||
|
||||
this.events = {};
|
||||
this.mapID = mapID;
|
||||
};
|
||||
|
||||
setLocalization(mapID) {
|
||||
this.mapID = mapID;
|
||||
}
|
||||
|
||||
getLocalization = () => this.mapID;
|
||||
|
||||
setReusableSingleEvent(id, time, data) {
|
||||
if (!this.events[id] || this.events[id][0].isOver) {
|
||||
const previousTick = this.getEventTick(id);
|
||||
const relativeTo = previousTick ? previousTick : performance.now();
|
||||
const tickAt = relativeTo + (time > 0 ? time : 0);
|
||||
|
||||
const event = {
|
||||
id,
|
||||
entity: this.entity,
|
||||
isCancelled: false,
|
||||
index: 0,
|
||||
tickAt,
|
||||
data,
|
||||
getLocalization: this.getLocalization,
|
||||
isSingle: true,
|
||||
isOver: false
|
||||
};
|
||||
|
||||
Game.events.add(tickAt, event);
|
||||
this.events[id] = [event];
|
||||
} else {
|
||||
this.events[id][0].isCancelled = false;
|
||||
};
|
||||
}
|
||||
|
||||
setSingleEvent(id, time, data) {
|
||||
const tickAt = performance.now() + time;
|
||||
|
||||
const event = {
|
||||
id,
|
||||
entity: this.entity,
|
||||
isCancelled: false,
|
||||
index: 0,
|
||||
tickAt,
|
||||
data,
|
||||
getLocalization: this.getLocalization
|
||||
};
|
||||
|
||||
if (!this.events[id]) {
|
||||
Game.events.add(tickAt, event);
|
||||
this.events[id] = [event];
|
||||
};
|
||||
}
|
||||
|
||||
setEventTest2(id, time, data) {
|
||||
const previousTick = this.getEventTick(id);
|
||||
const relativeTo = previousTick ? previousTick : performance.now();
|
||||
const tickAt = relativeTo + time;
|
||||
|
||||
const event = {
|
||||
id,
|
||||
entity: this.entity,
|
||||
isCancelled: false,
|
||||
index: this.events[id] ? this.events[id].length - 1 : 0,
|
||||
tickAt,
|
||||
data,
|
||||
getLocalization: this.getLocalization
|
||||
};
|
||||
|
||||
Game.events.add(tickAt, event);
|
||||
this.events[id] = [event];
|
||||
}
|
||||
|
||||
setEvent(id, time, data) {
|
||||
const previousTick = this.getEventTick(id);
|
||||
const relativeTo = previousTick ? previousTick : performance.now();
|
||||
const tickAt = relativeTo + time;
|
||||
|
||||
const event = {
|
||||
id,
|
||||
entity: this.entity,
|
||||
isCancelled: false,
|
||||
index: this.events[id] ? this.events[id].length - 1 : 0,
|
||||
tickAt,
|
||||
data,
|
||||
getLocalization: this.getLocalization
|
||||
};
|
||||
|
||||
Game.events.add(tickAt, event);
|
||||
|
||||
if (this.events[id]) this.events[id].push(event);
|
||||
else this.events[id] = [event];
|
||||
}
|
||||
|
||||
setIndependentEvent(id, time, data) {
|
||||
const tickAt = performance.now() + time;
|
||||
|
||||
const event = {
|
||||
id,
|
||||
entity: this.entity,
|
||||
isCancelled: false,
|
||||
index: this.events[id] ? this.events[id].length : 0,
|
||||
tickAt,
|
||||
data,
|
||||
getLocalization: this.getLocalization
|
||||
};
|
||||
|
||||
Game.events.add(tickAt, event);
|
||||
|
||||
if (this.events[id]) this.events[id].push(event);
|
||||
else this.events[id] = [event];
|
||||
}
|
||||
|
||||
getEventTick(id, index) {
|
||||
const event = this.events[id];
|
||||
if (!event) return 0;
|
||||
|
||||
if (!index) index = event.length - 1;
|
||||
|
||||
const element = event[index];
|
||||
return element && !element.isOver && element.tickAt;
|
||||
}
|
||||
|
||||
removeEvent(id) {
|
||||
delete this.events[id];
|
||||
}
|
||||
|
||||
cancelEvent(id, index) {
|
||||
if (this.events[id]) {
|
||||
if (index == null) {
|
||||
for (let i = 0; i < this.events[id].length; i++) this.events[id][i].isCancelled = true;
|
||||
} else if (index === -1) {
|
||||
for (let i = 0; i < this.events[id].length - 1; i++) this.events[id][i].isCancelled = true;
|
||||
} else {
|
||||
this.events[id][index].isCancelled = true;
|
||||
};
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = EventScheduler;
|
||||
Reference in New Issue
Block a user