-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtimebehavior.js
177 lines (149 loc) · 5.28 KB
/
runtimebehavior.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* GDevelop JS Platform
* Copyright 2013-2016 Florian Rival (Florian.Rival@gmail.com). All rights reserved.
* This project is released under the MIT License.
*/
/**
* @typedef BehaviorData Properties to set up a behavior.
* @property {string} name The name of the behavior (for getting from an object (object.getBehavior) for example)
* @property {string} type The behavior type. Used by GDJS to find the proper behavior to construct.
*/
/**
* RuntimeBehavior represents a behavior being used by a RuntimeObject.
*
* @class RuntimeBehavior
* @memberof gdjs
* @param {gdjs.RuntimeScene} runtimeScene The scene owning the object of the behavior
* @param {BehaviorData} behaviorData The properties used to setup the behavior
* @param {gdjs.RuntimeObject} owner The object owning the behavior
*/
gdjs.RuntimeBehavior = function(runtimeScene, behaviorData, owner)
{
this.name = behaviorData.name || "";
this.type = behaviorData.type || "";
this._nameId = gdjs.RuntimeObject.getNameIdentifier(this.name);
this._activated = true;
this.owner = owner;
};
/**
* Called when the behavior must be updated using the specified behaviorData. This is the
* case during hot-reload, and is only called if the behavior was modified.
*
* @see gdjs.RuntimeBehavior#onObjectHotReloaded
*
* @param {BehaviorData} oldBehaviorData The previous data for the behavior.
* @param {BehaviorData} newBehaviorData The new data for the behavior.
* @returns {boolean} true if the behavior was updated, false if it could not (i.e: hot-reload is not supported).
*/
gdjs.RuntimeBehavior.prototype.updateFromBehaviorData = function(oldBehaviorData, newBehaviorData) {
// If not redefined, mark by default the hot-reload as failed.
return false;
}
/**
* Get the name of the behavior.
* @return {string} The behavior's name.
*/
gdjs.RuntimeBehavior.prototype.getName = function() {
return this.name;
};
/**
* Get the name identifier of the behavior.
* @return {number} The behavior's name identifier.
*/
gdjs.RuntimeBehavior.prototype.getNameId = function() {
return this._nameId;
};
/**
* Called at each frame before events. Call doStepPreEvents.<br>
* Behaviors writers: Please do not redefine this method. Redefine doStepPreEvents instead.
* @param {gdjs.RuntimeScene} runtimeScene The runtimeScene owning the object
*/
gdjs.RuntimeBehavior.prototype.stepPreEvents = function(runtimeScene) {
if ( this._activated ) {
var profiler = runtimeScene.getProfiler();
if (profiler) profiler.begin(this.name);
this.doStepPreEvents(runtimeScene);
if (profiler) profiler.end(this.name);
}
};
/**
* Called at each frame after events. Call doStepPostEvents.<br>
* Behaviors writers: Please do not redefine this method. Redefine doStepPreEvents instead.
* @param {gdjs.RuntimeScene} runtimeScene The runtimeScene owning the object
*/
gdjs.RuntimeBehavior.prototype.stepPostEvents = function(runtimeScene) {
if ( this._activated ) {
var profiler = runtimeScene.getProfiler();
if (profiler) profiler.begin(this.name);
this.doStepPostEvents(runtimeScene);
if (profiler) profiler.end(this.name);
}
};
/**
* De/Activate the behavior
* @param {boolean} enable true to enable the behavior, false to disable it
*/
gdjs.RuntimeBehavior.prototype.activate = function(enable) {
if ( enable === undefined ) enable = true;
if ( !this._activated && enable ) {
this._activated = true;
this.onActivate();
}
else if ( this._activated && !enable ) {
this._activated = false;
this.onDeActivate();
}
};
/**
* Reimplement this to do extra work when the behavior is created (i.e: an
* object using it was created), after the object is fully initialized (so
* you can use `this.owner` without risk).
*/
gdjs.RuntimeBehavior.prototype.onCreated = function() {
};
/**
* Return true if the behavior is activated
*/
gdjs.RuntimeBehavior.prototype.activated = function() {
return this._activated;
};
/**
* Reimplement this method to do extra work when the behavior is activated (after
* it has been deactivated, see `onDeActivate`).
*/
gdjs.RuntimeBehavior.prototype.onActivate = function() {
};
/**
* Reimplement this method to do extra work when the behavior is deactivated.
*/
gdjs.RuntimeBehavior.prototype.onDeActivate = function() {
};
/**
* This method is called each tick before events are done.
* @param {gdjs.RuntimeScene} runtimeScene The runtimeScene owning the object
*/
gdjs.RuntimeBehavior.prototype.doStepPreEvents = function(runtimeScene) {
};
/**
* This method is called each tick after events are done.
* @param {gdjs.RuntimeScene} runtimeScene The runtimeScene owning the object
*/
gdjs.RuntimeBehavior.prototype.doStepPostEvents = function(runtimeScene) {
}
/**
* This method is called when the owner of the behavior
* is being removed from the scene and is about to be destroyed/reused later
* or when the behavior is removed from an object (can happen in case of
* hot-reloading only. Otherwise, behaviors are just de-activated,
* not removed. See `onDeActivate`).
*/
gdjs.RuntimeBehavior.prototype.onDestroy = function() {
};
/**
* This method is called when the owner of the behavior
* was hot reloaded, so its position, angle, size can have been changed outside
* of events.
*/
gdjs.RuntimeBehavior.prototype.onObjectHotReloaded = function() {
};
gdjs.registerBehavior("", gdjs.RuntimeBehavior);