forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.js
More file actions
127 lines (101 loc) · 4.2 KB
/
system.js
File metadata and controls
127 lines (101 loc) · 4.2 KB
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
pc.extend(pc, function () {
var _schema = [ 'enabled' ];
/**
* @name pc.ScriptComponentSystem
* @description Create a new ScriptComponentSystem
* @class Allows scripts to be attached to an Entity and executed
* @param {pc.Application} app The application
* @extends pc.ComponentSystem
*/
var ScriptComponentSystem = function ScriptComponentSystem(app) {
this.id = 'script';
this.app = app;
app.systems.add(this.id, this);
this.ComponentType = pc.ScriptComponent;
this.DataType = pc.ScriptComponentData;
this.schema = _schema;
// list of all entities script components
this._components = [ ];
this.on('beforeremove', this._onBeforeRemove, this);
pc.ComponentSystem.on('initialize', this._onInitialize, this);
pc.ComponentSystem.on('postInitialize', this._onPostInitialize, this);
pc.ComponentSystem.on('update', this._onUpdate, this);
pc.ComponentSystem.on('postUpdate', this._onPostUpdate, this);
};
ScriptComponentSystem = pc.inherits(ScriptComponentSystem, pc.ComponentSystem);
pc.Component._buildAccessors(pc.ScriptComponent.prototype, _schema);
pc.extend(ScriptComponentSystem.prototype, {
initializeComponentData: function(component, data, properties) {
this._components.push(component);
component.enabled = data.hasOwnProperty('enabled') ? !!data.enabled : true;
if (data.hasOwnProperty('order') && data.hasOwnProperty('scripts')) {
component._scriptsData = data.scripts;
for(var i = 0; i < data.order.length; i++) {
component.create(data.order[i], {
enabled: data.scripts[data.order[i]].enabled,
attributes: data.scripts[data.order[i]].attributes,
preloading: true
});
}
}
},
cloneComponent: function(entity, clone) {
var i, key;
var order = [ ];
var scripts = { };
for (i = 0; i < entity.script._scripts.length; i++) {
var scriptInstance = entity.script._scripts[i];
var scriptName = scriptInstance.__scriptType.__name;
order.push(scriptName);
var attributes = { };
for (key in scriptInstance.__attributes)
attributes[key] = scriptInstance.__attributes[key];
scripts[scriptName] = {
enabled: scriptInstance._enabled,
attributes: attributes
};
}
for (key in entity.script._scriptsIndex) {
if (key.awayting)
order.splice(key.ind, 0, key);
}
var data = {
enabled: entity.script.enabled,
order: order,
scripts: scripts
};
return this.addComponent(clone, data);
},
_callComponentMethod: function(name, dt) {
for(var i = 0; i < this._components.length; i++) {
if (! this._components[i].entity.enabled || ! this._components[i].enabled)
continue;
this._components[i][name](dt);
}
},
_onInitialize: function() {
// initialize attributes
for(var i = 0; i < this._components.length; i++)
this._components[i]._onInitializeAttributes();
this._callComponentMethod('_onInitialize');
},
_onPostInitialize: function() {
this._callComponentMethod('_onPostInitialize');
},
_onUpdate: function(dt) {
this._callComponentMethod('_onUpdate', dt);
},
_onPostUpdate: function(dt) {
this._callComponentMethod('_onPostUpdate', dt);
},
_onBeforeRemove: function(entity, component) {
var ind = this._components.indexOf(component);
if (ind === -1) return;
component._onBeforeRemove();
this._components.splice(ind, 1);
}
});
return {
ScriptComponentSystem: ScriptComponentSystem
};
}());