forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildGameObject.js
More file actions
147 lines (113 loc) · 4.48 KB
/
Copy pathBuildGameObject.js
File metadata and controls
147 lines (113 loc) · 4.48 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
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var BlendModes = require('../renderer/BlendModes');
var GetAdvancedValue = require('../utils/object/GetAdvancedValue');
var ScaleModes = require('../renderer/ScaleModes');
/**
* @typedef {object} GameObjectConfig
*
* @property {number} [x=0] - The x position of the Game Object.
* @property {number} [y=0] - The y position of the Game Object.
* @property {number} [depth=0] - The depth of the GameObject.
* @property {boolean} [flipX=false] - The horizontally flipped state of the Game Object.
* @property {boolean} [flipY=false] - The vertically flipped state of the Game Object.
* @property {?(number|object)} [scale=null] - The scale of the GameObject.
* @property {?(number|object)} [scrollFactor=null] - The scroll factor of the GameObject.
* @property {number} [rotation=0] - The rotation angle of the Game Object, in radians.
* @property {?number} [angle=null] - The rotation angle of the Game Object, in degrees.
* @property {number} [alpha=1] - The alpha (opacity) of the Game Object.
* @property {?(number|object)} [origin=null] - The origin of the Game Object.
* @property {number} [scaleMode=ScaleModes.DEFAULT] - The scale mode of the GameObject.
* @property {number} [blendMode=BlendModes.DEFAULT] - The blend mode of the GameObject.
* @property {boolean} [visible=true] - The visible state of the Game Object.
* @property {boolean} [add=true] - Add the GameObject to the scene.
*/
/**
* Builds a Game Object using the provided configuration object.
*
* @function Phaser.GameObjects.BuildGameObject
* @since 3.0.0
*
* @param {Phaser.Scene} scene - A reference to the Scene.
* @param {Phaser.GameObjects.GameObject} gameObject - The initial GameObject.
* @param {GameObjectConfig} config - The config to build the GameObject with.
*
* @return {Phaser.GameObjects.GameObject} The built Game Object.
*/
var BuildGameObject = function (scene, gameObject, config)
{
// Position
gameObject.x = GetAdvancedValue(config, 'x', 0);
gameObject.y = GetAdvancedValue(config, 'y', 0);
gameObject.depth = GetAdvancedValue(config, 'depth', 0);
// Flip
gameObject.flipX = GetAdvancedValue(config, 'flipX', false);
gameObject.flipY = GetAdvancedValue(config, 'flipY', false);
// Scale
// Either: { scale: 2 } or { scale: { x: 2, y: 2 }}
var scale = GetAdvancedValue(config, 'scale', null);
if (typeof scale === 'number')
{
gameObject.setScale(scale);
}
else if (scale !== null)
{
gameObject.scaleX = GetAdvancedValue(scale, 'x', 1);
gameObject.scaleY = GetAdvancedValue(scale, 'y', 1);
}
// ScrollFactor
// Either: { scrollFactor: 2 } or { scrollFactor: { x: 2, y: 2 }}
var scrollFactor = GetAdvancedValue(config, 'scrollFactor', null);
if (typeof scrollFactor === 'number')
{
gameObject.setScrollFactor(scrollFactor);
}
else if (scrollFactor !== null)
{
gameObject.scrollFactorX = GetAdvancedValue(scrollFactor, 'x', 1);
gameObject.scrollFactorY = GetAdvancedValue(scrollFactor, 'y', 1);
}
// Rotation
gameObject.rotation = GetAdvancedValue(config, 'rotation', 0);
var angle = GetAdvancedValue(config, 'angle', null);
if (angle !== null)
{
gameObject.angle = angle;
}
// Alpha
gameObject.alpha = GetAdvancedValue(config, 'alpha', 1);
// Origin
// Either: { origin: 0.5 } or { origin: { x: 0.5, y: 0.5 }}
var origin = GetAdvancedValue(config, 'origin', null);
if (typeof origin === 'number')
{
gameObject.setOrigin(origin);
}
else if (origin !== null)
{
var ox = GetAdvancedValue(origin, 'x', 0.5);
var oy = GetAdvancedValue(origin, 'y', 0.5);
gameObject.setOrigin(ox, oy);
}
// ScaleMode
gameObject.scaleMode = GetAdvancedValue(config, 'scaleMode', ScaleModes.DEFAULT);
// BlendMode
gameObject.blendMode = GetAdvancedValue(config, 'blendMode', BlendModes.NORMAL);
// Visible
gameObject.visible = GetAdvancedValue(config, 'visible', true);
// Add to Scene
var add = GetAdvancedValue(config, 'add', true);
if (add)
{
scene.sys.displayList.add(gameObject);
}
if (gameObject.preUpdate)
{
scene.sys.updateList.add(gameObject);
}
return gameObject;
};
module.exports = BuildGameObject;