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
175 lines (151 loc) · 5.67 KB
/
system.js
File metadata and controls
175 lines (151 loc) · 5.67 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
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
pc.extend(pc, function () {
/**
* @name pc.ComponentSystem
* @class Component Systems contain the logic and functionality to update all Components of a particular type
* @param {pc.Application} app The running Application
*/
var ComponentSystem = function (app) {
this.app = app;
this.dataStore = {};
this.schema = [];
pc.events.attach(this);
};
// Class methods
pc.extend(ComponentSystem, {
initialize: function (root) {
ComponentSystem.fire('initialize', root);
},
postInitialize: function (root) {
ComponentSystem.fire('postInitialize', root);
},
/**
* Update all ComponentSystems
*/
update: function (dt, inTools) {
if (inTools) {
ComponentSystem.fire('toolsUpdate', dt);
} else {
ComponentSystem.fire('update', dt);
}
},
/**
* Update all ComponentSystems
*/
fixedUpdate: function (dt, inTools) {
ComponentSystem.fire('fixedUpdate', dt);
},
/**
* Update all ComponentSystems
*/
postUpdate: function (dt, inTools) {
ComponentSystem.fire('postUpdate', dt);
}
});
// Instance methods
ComponentSystem.prototype = {
/**
* @private
* @field
* @type Array
* @name pc.ComponentSystem#store
* @description The store where all {@link pc.ComponentData} objects are kept
*/
get store() {
return this.dataStore;
},
/**
* @private
* @function
* @name pc.ComponentSystem#addComponent
* @description Create new {@link pc.Component} and {@link pc.ComponentData} instances and attach them to the entity
* @param {pc.Entity} entity The Entity to attach this component to
* @param {Object} data The source data with which to create the component
* @returns {pc.Component} Returns a Component of type defined by the component system
* @example
* var entity = new pc.Entity(app);
* app.systems.model.addComponent(entity, { type: 'box' });
* // entity.model is now set to a pc.ModelComponent
*/
addComponent: function (entity, data) {
var component = new this.ComponentType(this, entity);
var componentData = new this.DataType();
data = data || {};
this.dataStore[entity._guid] = {
entity: entity,
data: componentData
};
entity[this.id] = component;
entity.c[this.id] = component;
this.initializeComponentData(component, data, []);
this.fire('add', entity, component);
return component;
},
/**
* @private
* @function
* @name pc.ComponentSystem#removeComponent
* @description Remove the {@link pc.Component} from the entity and delete the associated {@link pc.ComponentData}
* @param {pc.Entity} entity The entity to remove the component from
* @example
* app.systems.model.removeComponent(entity);
* // entity.model === undefined
*/
removeComponent: function (entity) {
var record = this.dataStore[entity._guid];
var component = entity.c[this.id];
this.fire('beforeremove', entity, component);
delete this.dataStore[entity._guid];
delete entity[this.id];
delete entity.c[this.id];
this.fire('remove', entity, record.data);
},
/**
* @private
* @function
* @name pc.ComponentSystem#cloneComponent
* @description Create a clone of component. This creates a copy all ComponentData variables.
* @param {pc.Entity} entity The entity to clone the component from
* @param {pc.Entity} clone The entity to clone the component into
*/
cloneComponent: function (entity, clone) {
// default clone is just to add a new component with existing data
var src = this.dataStore[entity._guid];
return this.addComponent(clone, src.data);
},
/**
* @private
* @function
* @name pc.ComponentSystem#initializeComponentData
* @description Called during {@link pc.ComponentSystem#addComponent} to initialize the {@link pc.ComponentData} in the store
* This can be overridden by derived Component Systems and either called by the derived System or replaced entirely
*/
initializeComponentData: function (component, data, properties) {
data = data || {};
// initialize
properties.forEach(function(value) {
if (data[value] !== undefined) {
component[value] = data[value];
} else {
component[value] = component.data[value];
}
}, this);
// after component is initialized call onEnable
if (component.enabled && component.entity.enabled) {
component.onEnable();
}
}
};
// Add event support
pc.events.attach(ComponentSystem);
ComponentSystem.destroy = function () {
ComponentSystem.off('initialize');
ComponentSystem.off('postInitialize');
ComponentSystem.off('toolsUpdate');
ComponentSystem.off('update');
ComponentSystem.off('fixedUpdate');
ComponentSystem.off('postUpdate');
};
return {
ComponentSystem: ComponentSystem
};
}());