-
Notifications
You must be signed in to change notification settings - Fork 4
/
oncetriggers.js
52 lines (47 loc) · 1.46 KB
/
oncetriggers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* GDevelop JS Platform
* Copyright 2013-2016 Florian Rival ([email protected]). All rights reserved.
* This project is released under the MIT License.
*/
/**
* OnceTriggers is used to store the status of the conditions "Trigger once",
* that are used in events to have conditions that are only valid for one frame in a row.
*
* @memberof gdjs
* @class OnceTriggers
* @constructor
*/
gdjs.OnceTriggers = function()
{
this._onceTriggers = {};
this._lastFrameOnceTrigger = {};
};
/**
* To be called when events begin so that "Trigger once" conditions
* are properly handled.
*/
gdjs.OnceTriggers.prototype.startNewFrame = function() {
this._clearObject(this._lastFrameOnceTrigger);
for (var k in this._onceTriggers) {
if (this._onceTriggers.hasOwnProperty(k)) {
this._lastFrameOnceTrigger[k] = this._onceTriggers[k];
delete this._onceTriggers[k];
}
}
};
/**
* Used by "Trigger once" conditions: return true only if
* this method was not called with the same identifier during the last frame.
* @param triggerId The identifier of the "Trigger once" condition.
*/
gdjs.OnceTriggers.prototype.triggerOnce = function(triggerId) {
this._onceTriggers[triggerId] = true;
return !this._lastFrameOnceTrigger.hasOwnProperty(triggerId);
};
gdjs.OnceTriggers.prototype._clearObject = function(obj) {
for (var k in obj) {
if (obj.hasOwnProperty(k)) {
delete obj[k];
}
}
};