forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.js
More file actions
95 lines (83 loc) · 2.87 KB
/
registry.js
File metadata and controls
95 lines (83 loc) · 2.87 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
pc.extend(pc, function () {
/**
* @name pc.ComponentSystemRegistry
* @class Store, access and delete instances of the various ComponentSystems
* @description Create a new ComponentSystemRegistry
*/
var ComponentSystemRegistry = function () {
};
ComponentSystemRegistry.prototype = {
/**
* @private
* @function
* @name pc.ComponentSystemRegistry#add
* @description Add a new Component type
* @param {Object} name The name of the Component
* @param {Object} component The {pc.ComponentSystem} instance
*/
add: function (name, system) {
if(!this[name]) {
this[name] = system;
system.name = name;
} else {
throw new Error(pc.string.format("ComponentSystem name '{0}' already registered or not allowed", name));
}
},
/**
* @private
* @function
* @name pc.ComponentSystemRegistry#remove
* @description Remove a Component type
* @param {Object} name The name of the Component remove
*/
remove: function(name) {
if(!this[name]) {
throw new Error(pc.string.format("No ComponentSystem named '{0}' registered", name));
}
delete this[name];
},
/**
* @private
* @function
* @name pc.ComponentSystemRegistry#list
* @description Return the contents of the registry as an array, this order of the array
* is the order in which the ComponentSystems must be initialized.
* @returns {pc.ComponentSystem[]} An array of component systems.
*/
list: function () {
var list = Object.keys(this);
var defaultPriority = 1;
var priorities = {
'collisionrect': 0.5,
'collisioncircle': 0.5
};
list.sort(function (a, b) {
var pa = priorities[a] || defaultPriority;
var pb = priorities[b] || defaultPriority;
if (pa < pb) {
return -1;
} else if (pa > pb) {
return 1;
}
return 0;
});
return list.map(function (key) {
return this[key];
}, this);
},
getComponentSystemOrder: function () {
var index;
var names = Object.keys(this);
index = names.indexOf('collisionrect');
names.splice(index, 1);
names.unshift('collisionrect');
index = names.indexOf('collisioncircle');
names.splice(index, 1);
names.unshift('collisioncircle');
return names;
}
};
return {
ComponentSystemRegistry: ComponentSystemRegistry
};
}());