-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
script.js
179 lines (161 loc) · 6.18 KB
/
script.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
178
179
import { events } from '../core/events.js';
import { getApplication } from './globals.js';
import { ScriptTypes } from './script/script-types.js';
/**
* Callback used by {@link script.createLoadingScreen}.
*
* @callback CreateScreenCallback
* @param {import('./app-base.js').AppBase} app - The application.
*/
/**
* Callback used by {@link script.create}.
*
* @callback CreateScriptCallback
* @param {import('./app-base.js').AppBase} app - The application.
* @returns {object} Return the Type of the script resource to be instanced for each Entity.
* @ignore
*/
let _legacy = false;
// flag to avoid creating multiple loading screens e.g. when
// loading screen scripts are reloaded
let _createdLoadingScreen = false;
/**
* The script namespace holds the createLoadingScreen function that is used to override the default
* PlayCanvas loading screen.
*
* @namespace
* @category Script
*/
const script = {
// set during script load to be used for initializing script
app: null,
/**
* Create a script resource object. A script file should contain a single call to
* {@link script.create} and the callback should return a script object which will be
* instantiated when attached to Entities.
*
* @param {string} name - The name of the script object.
* @param {CreateScriptCallback} callback - The callback function which is passed an
* {@link AppBase} 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 (app) {
* var Scriptable = function (entity) {
* // store entity
* this.entity = entity;
*
* // use app
* app.components.model.addComponent(entity, {
* // component properties
* });
* };
*
* return Scriptable;
* });
* @ignore
*/
create(name, callback) {
if (!_legacy)
return;
// get the ScriptType from the callback
const ScriptType = callback(script.app);
// store the script name
ScriptType._pcScriptName = name;
// Push this onto loading stack
ScriptTypes.push(ScriptType, _legacy);
this.fire("created", name, callback);
},
/**
* 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 PlayCanvas Editor like normal Components.
*
* @param {string} name - The name of the attribute.
* @param {string} type - The type of the attribute. Can be: 'number', 'string', 'boolean',
* 'asset', 'entity', 'rgb', 'rgba', 'vector', 'enumeration', 'curve', 'colorcurve'.
* @param {object} defaultValue - The default value of the attribute.
* @param {object} options - Optional parameters for the attribute.
* @param {number} options.min - The minimum value of the attribute.
* @param {number} options.max - The maximum value of the attribute.
* @param {number} options.step - The step that will be used when changing the attribute value
* in the PlayCanvas Editor.
* @param {number} options.decimalPrecision - A number that specifies the number of decimal
* digits allowed for the value.
* @param {object[]} options.enumerations - An array of name, value pairs from which the user
* can select one if the attribute type is an enumeration.
* @param {string[]} options.curves - (For 'curve' attributes only) An array of strings that
* define the names of each curve in the curve editor.
* @param {boolean} options.color - (For 'curve' attributes only) If true then the curve
* attribute will be a color curve.
* @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.attribute('enemy', 'entity');
* pc.script.attribute('enemySpeed', 'curve');
* pc.script.attribute('enemyPosition', 'curve', null, {
* curves: ['x', 'y', 'z']
* });
* pc.script.attribute('color', 'colorcurve', null, {
* type: 'rgba'
* });
*
* pc.script.create('scriptable', function (app) {
* var Scriptable = function (entity) {
* // store entity
* this.entity = entity;
* };
*
* return Scriptable;
* });
* @ignore
*/
attribute(name, type, defaultValue, options) {
// only works when parsing the script...
},
/**
* Handles the creation of the loading screen of the application. A script can subscribe to the
* events of a {@link AppBase} to show a loading screen, progress bar etc. In order for
* this to work you need to set the project's loading screen script to the script that calls
* this method.
*
* @param {CreateScreenCallback} callback - A function which can set up and tear down a
* customized loading screen.
* @example
* pc.script.createLoadingScreen(function (app) {
* var showSplashScreen = function () {};
* var hideSplashScreen = function () {};
* var showProgress = function (progress) {};
* app.on("preload:start", showSplashScreen);
* app.on("preload:progress", showProgress);
* app.on("start", hideSplashScreen);
* });
*/
createLoadingScreen(callback) {
if (_createdLoadingScreen)
return;
_createdLoadingScreen = true;
const app = getApplication();
callback(app);
}
};
Object.defineProperty(script, 'legacy', {
get: function () {
return _legacy;
},
set: function (value) {
_legacy = value;
}
});
events.attach(script);
export { script };