forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_script.js
More file actions
117 lines (109 loc) · 4.47 KB
/
Copy pathscript_script.js
File metadata and controls
117 lines (109 loc) · 4.47 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
/**
* @name pc.script
* @namespace User Scripts
*/
pc.script = (function () {
var _main = null;
var _loader = null;
var script = {
/**
* Register the main game script resource, this is executed by called pc.script.start()
* @function
* @name pc.script.main
*/
main: function (callback) {
if(_main) {
throw new Error("'main' Object already registered");
}
_main = callback;
},
setLoader: function(loader) {
if(loader && _loader) {
throw new Error("pc.script already has loader object.");
}
_loader = loader;
},
/**
* @function
* @name pc.script.create
* @description Create a script resource object. A script file should contain a single call to pc.script.create and the callback should return a script object which will be
* instanciated when attached to Entities.
* @param {Object} name The name of the script object.
* @param {Object} callback The callback function which is passed an {pc.fw.ApplicationContext} object,
* which is used to access Entities and Components, and should return the Type of the script resource
* to be instanced for each Entity.
* @example
* pc.script.create( function (context) {
* var Scriptable = function (entity) {
* // store entity
* this.entity = entity;
*
* // use context
* context.components.model.addComponent(entity, {...});
* };
*
* return Scriptable;
* }
*/
create: function (name, callback) {
if (typeof(callback) === 'undefined') {
callback = attributes;
}
//_loader.add(name, callback);
this.fire("created", name, callback);
},
/**
* @function
* @name pc.script.attribute
* @description Creates a script attribute for the current script. The script attribute can be accessed
* inside the script instance like so 'this.attributeName' or outside a script instance like so 'entity.script.attributeName'.
* Script attributes can be edited from the Attribute Editor of the designer like normal Components.
* @param {string} name The name of the attribute
* @param {string} type The type of the attribute. Can be one of the following: 'number', 'string', 'boolean', 'asset', 'rgb', 'rgba', 'vector', 'enumeration'
* @param {Object} defaultValue The default value of the attribute
* @param {Object} options Optional parameters for the attribute. Valid values are:
* <ul>
* <li>{Number} min: The minimum value of the attribute</li>
* <li>{Number} max: The maximum value of the attribute</li>
* <li>{Number} step: The step that will be used when changing the attribute value in the designer</li>
* <li>{Number} decimalPrecision: A number that specifies the number of decimal digits allowed for the value</li>
* <li>{Array} enumerations: An array of name, value pairs from which the user can select one if the attribute type is an enumeration</li>
* </ul>
* @example
* pc.script.attribute('speed', 'number', 5);
* pc.script.attribute('message', 'string', "My message");
* pc.script.attribute('enemyPosition', 'vector', [1,0,0]);
* pc.script.attribute('spellType', 'enumeration', 0, {
* enumerations: [{
* name: "Fire",
* value: 0
* }, {
* name: "Ice",
* value: 1
* }]
* });
*
* pc.script.create('scriptable', function (context) {
* var Scriptable = function (entity) {
* // store entity
* this.entity = entity;
* };
*
* return Scriptable;
* }
*/
attribute: function (name, type, defaultValue, options) {
// only works when parsing the script...
},
/**
* Begin the scripted application by calling the function passed in to pc.script.main()
* @function
* @name pc.script.start
*/
start: function () {
_main();
}
};
pc.events.attach(script);
return script;
}());