Skip to content

Commit

Permalink
#11 WIP z modifier on GameObject scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
Inateno committed Nov 4, 2017
1 parent 2521b8b commit ff6ae4a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions js/engine/classes/GameObject/GameObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ function(
}
, set: function( z ) {
this._z = z;
this._updateZScale();

if ( this.parent ) {
this.parent._shouldSortChildren = true;
Expand Down
25 changes: 25 additions & 0 deletions js/engine/classes/GameObject/GameObject.scale.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
define( [
'DE.GameObject'
, 'DE.Time'
, 'DE.config'
],
function(
GameObject
, Time
, config
)
{
/**
Expand All @@ -21,6 +23,29 @@ function(
return this;
};

/**
* when z change we restore saved scale, then change it again to final values and update worldScale
* @private
* @memberOf GameObject
*/
GameObject.prototype._updateZScale = function()
{
// this come from old Camera render (working fine as excepted...)
// zMaxDepth is 10 by default so if z is 1 scale modifier will be 0.9 (1 - 0.1)
var zscale = 1 - ( this.z / config.zMaxDepth );
this._zscale = zscale;

this.scale.x = zscale * this.savedScale.x;
this.scale.y = zscale * this.savedScale.y;

// update worldScale
this._updateWorldScale();
for ( var i = 0; i < this.gameObjects.length; ++i )
{
this.gameObjects[ i ]._updateWorldScale();
}
};

/**
* when we change the scale manually, we need to re-apply z deformation
* directly save the old scale before zscale applies (this way we can recalculate things from the beginning)
Expand Down
3 changes: 3 additions & 0 deletions js/engine/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ define( [], function()
, DEBUG_LEVEL: 0
};

// zMaxDepth is the global scaling used for z. 10 by default so if you put an object to z = 10 his scale will be 0
config.zMaxDepth = 10;

config.notifications = {
enable: true // notifications enable by default
,gamepadEnable : true
Expand Down
2 changes: 2 additions & 0 deletions js/game1/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ function( DE )

Game.heart1 = new DE.GameObject( {
x: 700, y: 100
, zindex : 10
,renderer: new DE.TextureRenderer( { spriteName: "heart" } )
} );
Game.heart2 = new DE.GameObject( {
x: 800, y: 100
, zindex : 10
,renderer: new DE.TextureRenderer( { spriteName: "heart", width: 50, height: 20 } )
} );

Expand Down

0 comments on commit ff6ae4a

Please sign in to comment.