@@ -14,25 +14,78 @@ var Transform = {
1414 _scaleY : 1 ,
1515 _rotation : 0 ,
1616 _depth : 0 ,
17+ _dirty : false ,
18+ _world : { a : 1 , b : 0 , c : 0 , d : 1 , tx : 0 , ty : 0 , sr : 0 , cr : 0 } ,
1719
1820 // public properties / methods
1921
20- x : 0 ,
21- y : 0 ,
22- z : 0 ,
23- w : 0 ,
22+ // These are world coordinate values.
2423
25- depth : {
24+ // If Game Object is a child of a Container, then you can modify its local position (relative to the Container)
25+ // by setting `localX`, `localY`, etc (or changing x/y directly, but remember the values given here are world based).
26+ // Changes to the parent Container are instantly reflected in the world coords here (x,y, etc)
27+
28+ _x : 0 ,
29+ _y : 0 ,
30+ _z : 0 ,
31+ _w : 0 ,
32+
33+ x : {
2634
2735 get : function ( )
2836 {
29- return this . _depth ;
37+ return this . _x ;
3038 } ,
3139
3240 set : function ( value )
3341 {
34- this . scene . sys . sortChildrenFlag = true ;
35- this . _depth = value ;
42+ this . _x = value ;
43+ this . _dirty = true ;
44+ }
45+
46+ } ,
47+
48+ y : {
49+
50+ get : function ( )
51+ {
52+ return this . _y ;
53+ } ,
54+
55+ set : function ( value )
56+ {
57+ this . _y = value ;
58+ this . _dirty = true ;
59+ }
60+
61+ } ,
62+
63+ z : {
64+
65+ get : function ( )
66+ {
67+ return this . _z ;
68+ } ,
69+
70+ set : function ( value )
71+ {
72+ this . _z = value ;
73+ this . _dirty = true ;
74+ }
75+
76+ } ,
77+
78+ w : {
79+
80+ get : function ( )
81+ {
82+ return this . _w ;
83+ } ,
84+
85+ set : function ( value )
86+ {
87+ this . _w = value ;
88+ this . _dirty = true ;
3689 }
3790
3891 } ,
@@ -47,6 +100,7 @@ var Transform = {
47100 set : function ( value )
48101 {
49102 this . _scaleX = value ;
103+ this . _dirty = true ;
50104
51105 if ( this . _scaleX === 0 )
52106 {
@@ -70,6 +124,7 @@ var Transform = {
70124 set : function ( value )
71125 {
72126 this . _scaleY = value ;
127+ this . _dirty = true ;
73128
74129 if ( this . _scaleY === 0 )
75130 {
@@ -108,7 +163,27 @@ var Transform = {
108163 {
109164 // value is in radians
110165 this . _rotation = WrapAngle ( value ) ;
166+
167+ this . _world . sr = Math . sin ( this . _rotation ) ;
168+ this . _world . cr = Math . cos ( this . _rotation ) ;
169+
170+ this . _dirty = true ;
171+ }
172+ } ,
173+
174+ depth : {
175+
176+ get : function ( )
177+ {
178+ return this . _depth ;
179+ } ,
180+
181+ set : function ( value )
182+ {
183+ this . scene . sys . sortChildrenFlag = true ;
184+ this . _depth = value ;
111185 }
186+
112187 } ,
113188
114189 setPosition : function ( x , y , z , w )
@@ -118,10 +193,12 @@ var Transform = {
118193 if ( z === undefined ) { z = 0 ; }
119194 if ( w === undefined ) { w = 0 ; }
120195
121- this . x = x ;
122- this . y = y ;
123- this . z = z ;
124- this . w = w ;
196+ this . _x = x ;
197+ this . _y = y ;
198+ this . _z = z ;
199+ this . _w = w ;
200+
201+ this . _dirty = true ;
125202
126203 return this ;
127204 } ,
@@ -180,6 +257,38 @@ var Transform = {
180257 this . depth = value ;
181258
182259 return this ;
260+ } ,
261+
262+ updateTransform : function ( )
263+ {
264+ if ( ! this . parent || ! this . _dirty )
265+ {
266+ return ;
267+ }
268+
269+ var tx = this . _x ;
270+ var ty = this . _y ;
271+ var world = this . _world ;
272+
273+ var parent = this . parent . world ;
274+
275+ var a = world . cr * this . _scaleX ;
276+ var b = world . sr * this . _scaleX ;
277+ var c = - world . sr * this . _scaleY ;
278+ var d = world . cr * this . _scaleY ;
279+
280+ world . a = ( a * parent . a ) + ( b * parent . c ) ;
281+ world . b = ( a * parent . b ) + ( b * parent . d ) ;
282+ world . c = ( c * parent . a ) + ( d * parent . c ) ;
283+ world . d = ( c * parent . b ) + ( d * parent . d ) ;
284+
285+ // this._worldRotation = Math.atan2(-this.world.c, this.world.d);
286+
287+ world . tx = ( tx * parent . a ) + ( ty * parent . c ) + parent . tx ;
288+ world . ty = ( tx * parent . b ) + ( ty * parent . d ) + parent . ty ;
289+
290+ // this._worldScaleX = this._scaleX * Math.sqrt((world.a * world.a) + (world.c * world.c));
291+ // this._worldScaleY = this._scaleY * Math.sqrt((world.b * world.b) + (world.d * world.d));
183292 }
184293
185294} ;
0 commit comments