forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-type.js
More file actions
125 lines (112 loc) · 3.66 KB
/
Copy pathscript-type.js
File metadata and controls
125 lines (112 loc) · 3.66 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
import { ScriptAttributes } from './script-attributes.js';
import { Script } from './script.js';
/**
* @import { AppBase } from '../app-base.js'
* @import { Entity } from '../entity.js'
*/
/**
* This is the legacy format for creating PlayCanvas script returned when calling `pc.createScript()`.
* You should not use this inherit from this class directly.
*
* @deprecated Use {@link Script} instead.
* @category Script
*/
class ScriptType extends Script {
/** @private */
__attributes;
/** @private */
__attributesRaw;
/**
* Create a new ScriptType instance.
*
* @param {object} args - The input arguments object.
* @param {AppBase} args.app - The {@link AppBase} that is running the script.
* @param {Entity} args.entity - The {@link Entity} that the script is attached to.
*/
constructor(args) {
super(args);
this.initScriptType(args);
}
/**
* The interface to define attributes for Script Types. Refer to {@link ScriptAttributes}.
*
* @type {ScriptAttributes}
* @example
* var PlayerController = pc.createScript('playerController');
*
* PlayerController.attributes.add('speed', {
* type: 'number',
* title: 'Speed',
* placeholder: 'km/h',
* default: 22.2
* });
*/
static get attributes() {
if (!this.hasOwnProperty('__attributes')) this.__attributes = new ScriptAttributes(this);
return this.__attributes;
}
/**
* @param {*} args - initialization arguments
* @protected
*/
initScript(args) {
// super does not exist due to the way the class is instantiated
Script.prototype.initScript.call(this, args);
this.__attributes = { };
this.__attributesRaw = args.attributes || { }; // need at least an empty object to make sure default attributes are initialized
}
/**
* Expose initScript as initScriptType for backwards compatibility
* @param {*} args - Initialization arguments
* @protected
*/
initScriptType(args) {
this.initScript(args);
}
/**
* @param {boolean} [force] - Set to true to force initialization of the attributes.
*/
__initializeAttributes(force) {
if (!force && !this.__attributesRaw) {
return;
}
// set attributes values
for (const key in this.__scriptType.attributes.index) {
if (this.__attributesRaw && this.__attributesRaw.hasOwnProperty(key)) {
this[key] = this.__attributesRaw[key];
} else if (!this.__attributes.hasOwnProperty(key)) {
if (this.__scriptType.attributes.index[key].hasOwnProperty('default')) {
this[key] = this.__scriptType.attributes.index[key].default;
} else {
this[key] = null;
}
}
}
this.__attributesRaw = null;
}
/**
* Shorthand function to extend Script Type prototype with list of methods.
*
* @param {object} methods - Object with methods, where key - is name of method, and value - is function.
* @example
* var PlayerController = pc.createScript('playerController');
*
* PlayerController.extend({
* initialize: function () {
* // called once on initialize
* },
* update: function (dt) {
* // called each tick
* }
* });
*/
static extend(methods) {
for (const key in methods) {
if (!methods.hasOwnProperty(key)) {
continue;
}
this.prototype[key] = methods[key];
}
}
}
export { ScriptType };