Factor common methods to gdjs.RuntimeObject.

Thanks to exposePIXIDisplayObject, objects can expose the PIXI.DisplayObject that they are using
and let RuntimeObject handles common operations (layer change, z order change, deletion, visibility).
This commit is contained in:
Florian Rival
2015-09-10 22:37:32 +02:00
parent 1ff06d4c9c
commit 3ac292ec18
10 changed files with 115 additions and 142 deletions

View File

@@ -5,7 +5,7 @@ Copyright (c) 2008-2015 Florian Rival (Florian.Rival@gmail.com)
This project is released under the MIT License.
*/
if (typeof AdMob !== "undefined")
if (typeof AdMob !== "undefined")
console.warn("AdMob plugin for Cordova is not installed - no ads will be displayed. Ensure you have installed com.google.cordova.admob or cordova-plugin-admobpro.");
/**
@@ -21,8 +21,6 @@ gdjs.AdMobRuntimeObject = function(runtimeScene, objectData)
{
gdjs.RuntimeObject.call(this, runtimeScene, objectData);
this._runtimeScene = runtimeScene;
this._androidBannerId = objectData.androidBannerId;
this._androidInterstitialId = objectData.androidInterstitialId;
this._iosBannerId = objectData.iosBannerId;
@@ -56,12 +54,12 @@ gdjs.AdMobRuntimeObject.getPlatformName = function() {
};
gdjs.AdMobRuntimeObject.prototype._onAdPresent = function(data) {
if (data.adType == 'interstitial')
if (data.adType == 'interstitial')
this._interstitialReady = false;
};
gdjs.AdMobRuntimeObject.prototype._onAdDismiss = function(data) {
if (data.adType == 'interstitial')
if (data.adType == 'interstitial')
this._interstitialReady = false;
};
@@ -145,9 +143,10 @@ gdjs.AdMobRuntimeObject.prototype.isInterstitialReady = function() {
};
gdjs.AdMobRuntimeObject.prototype.onDeletedFromScene = function(runtimeScene) {
gdjs.RuntimeObject.prototype.onDeletedFromScene.call(this, runtimeScene);
if (typeof AdMob === "undefined") return;
document.removeEventListener('onAdPresent', this._onAdPresent, false);
document.removeEventListener('onAdDismiss', this._onAdDismiss, false);
if (this._bannerDisplayed) this.hideBanner();
};
};

View File

@@ -49,7 +49,6 @@ gdjs.PanelSpriteRuntimeObject = function(runtimeScene, objectData)
this._lBorder = objectData.leftMargin;
this._tBorder = objectData.topMargin;
this._bBorder = objectData.bottomMargin;
this._runtimeScene = runtimeScene;
this.setTexture(objectData.texture, runtimeScene);
this.setWidth(objectData.width);
this.setHeight(objectData.height);
@@ -68,8 +67,8 @@ gdjs.PanelSpriteRuntimeObject = function(runtimeScene, objectData)
gdjs.PanelSpriteRuntimeObject.prototype = Object.create( gdjs.RuntimeObject.prototype );
gdjs.PanelSpriteRuntimeObject.thisIsARuntimeObjectConstructor = "PanelSpriteObject::PanelSprite";
gdjs.PanelSpriteRuntimeObject.prototype.onDeletedFromScene = function(runtimeScene) {
runtimeScene.getLayer(this.layer).removePIXIContainerChild(this._spritesContainer);
gdjs.PanelSpriteRuntimeObject.prototype.exposePIXIDisplayObject = function(cb) {
cb(this._spritesContainer);
};
/**
@@ -209,13 +208,6 @@ gdjs.PanelSpriteRuntimeObject.prototype.setAngle = function(angle) {
this._spritesContainer.rotation = gdjs.toRad(angle);
};
gdjs.PanelSpriteRuntimeObject.prototype.setLayer = function(name) {
//We need to move the object from the pixi container of the layer
this._runtimeScene.getLayer(this.layer).removePIXIContainerChild(this._spritesContainer);
this.layer = name;
this._runtimeScene.getLayer(this.layer).addChildToPIXIContainer(this._spritesContainer, this.zOrder);
};
gdjs.PanelSpriteRuntimeObject.prototype.getWidth = function() {
return this._width;
};
@@ -237,10 +229,3 @@ gdjs.PanelSpriteRuntimeObject.prototype.setHeight = function(height) {
this._updateSpritesAndTexturesSize();
this._updateSpritePositions();
};
gdjs.PanelSpriteRuntimeObject.prototype.setZOrder = function(z) {
if ( z !== this.zOrder ) {
this._runtimeScene.getLayer(this.layer).changePIXIContainerChildZOrder(this._spritesContainer, z);
this.zOrder = z;
}
};

View File

@@ -25,15 +25,14 @@ gdjs.ShapePainterRuntimeObject = function(runtimeScene, objectData)
this._outlineSize = objectData.outlineSize;
this._absoluteCoordinates = objectData.absoluteCoordinates;
this._runtimeScene = runtimeScene;
runtimeScene.getLayer("").addChildToPIXIContainer(this._graphics, this.zOrder);
};
gdjs.ShapePainterRuntimeObject.prototype = Object.create( gdjs.RuntimeObject.prototype );
gdjs.ShapePainterRuntimeObject.thisIsARuntimeObjectConstructor = "PrimitiveDrawing::Drawer";
gdjs.ShapePainterRuntimeObject.prototype.onDeletedFromScene = function(runtimeScene) {
runtimeScene.getLayer(this.layer).removePIXIContainerChild(this._graphics);
gdjs.ShapePainterRuntimeObject.prototype.exposePIXIDisplayObject = function(cb) {
cb(this._graphics);
};
gdjs.ShapePainterRuntimeObject.prototype.stepBehaviorsPreEvents = function(runtimeScene) {
@@ -126,13 +125,6 @@ gdjs.ShapePainterRuntimeObject.prototype.setY = function(y) {
}
};
gdjs.ShapePainterRuntimeObject.prototype.setLayer = function(name) {
//We need to move the object from the pixi container of the layer
this._runtimeScene.getLayer(this.layer).removePIXIContainerChild(this._graphics);
this.layer = name;
this._runtimeScene.getLayer(this.layer).addChildToPIXIContainer(this._graphics, this.zOrder);
};
gdjs.ShapePainterRuntimeObject.prototype.getWidth = function() {
return 32;
};
@@ -140,10 +132,3 @@ gdjs.ShapePainterRuntimeObject.prototype.getWidth = function() {
gdjs.ShapePainterRuntimeObject.prototype.getHeight = function() {
return 32;
};
gdjs.ShapePainterRuntimeObject.prototype.setZOrder = function(z) {
if ( z !== this.zOrder ) {
this._runtimeScene.getLayer(this.layer).changePIXIContainerChildZOrder(this._graphics, z);
this.zOrder = z;
}
};

View File

@@ -68,6 +68,8 @@ gdjs.TextEntryRuntimeObject.prototype = Object.create( gdjs.RuntimeObject.protot
gdjs.TextEntryRuntimeObject.thisIsARuntimeObjectConstructor = "TextEntryObject::TextEntry";
gdjs.TextEntryRuntimeObject.prototype.onDeletedFromScene = function(runtimeScene) {
gdjs.RuntimeObject.prototype.onDeletedFromScene.call(this, runtimeScene);
document.removeEventListener('keypress', this._pressHandler);
document.removeEventListener('keyup', this._upHandler);
document.removeEventListener('keydown', this._downHandler);

View File

@@ -15,7 +15,6 @@ gdjs.TextRuntimeObject = function(runtimeScene, objectData)
{
gdjs.RuntimeObject.call(this, runtimeScene, objectData);
this._runtimeScene = runtimeScene;
this._characterSize = objectData.characterSize;
this._fontName = "Arial";
this._bold = objectData.bold;
@@ -42,8 +41,8 @@ gdjs.TextRuntimeObject = function(runtimeScene, objectData)
gdjs.TextRuntimeObject.prototype = Object.create( gdjs.RuntimeObject.prototype );
gdjs.TextRuntimeObject.thisIsARuntimeObjectConstructor = "TextObject::Text";
gdjs.TextRuntimeObject.prototype.onDeletedFromScene = function(runtimeScene) {
runtimeScene.getLayer(this.layer).removePIXIContainerChild(this._text);
gdjs.TextRuntimeObject.prototype.exposePIXIDisplayObject = function(cb) {
cb(this._text);
};
/**
@@ -205,29 +204,6 @@ gdjs.TextRuntimeObject.prototype.setItalic = function(enable) {
this._updateTextStyle();
};
/**
* Hide or show the object
* @method hide
* @param enable {Boolean} Set it to true to hide the object, false to show it.
*/
gdjs.TextRuntimeObject.prototype.hide = function(enable) {
if ( enable === undefined ) enable = true;
this._hidden = enable;
this._text.visible = !enable;
};
/**
* Set the layer of the object.
* @method setLayer
* @param {String} The new layer of the object
*/
gdjs.TextRuntimeObject.prototype.setLayer = function(name) {
//We need to move the object from the pixi container of the layer
this._runtimeScene.getLayer(this.layer).removePIXIContainerChild(this._text);
this.layer = name;
this._runtimeScene.getLayer(this.layer).addChildToPIXIContainer(this._text, this.zOrder);
};
/**
* Get width for your object text.
* @method getWidth
@@ -258,15 +234,3 @@ gdjs.TextRuntimeObject.prototype.setColor = function(str) {
this._color[2] = parseInt(color[2], 10);
this._updateTextStyle();
};
/**
* Set the Z order of the object.
* @method setZOrder
* @param z {Number} The new Z order position of the object
*/
gdjs.TextRuntimeObject.prototype.setZOrder = function(z) {
if ( z !== this.zOrder ) {
this._runtimeScene.getLayer(this.layer).changePIXIContainerChildZOrder(this._text, z);
this.zOrder = z;
}
};

View File

@@ -26,15 +26,14 @@ gdjs.TiledSpriteRuntimeObject = function(runtimeScene, objectData)
this._xOffset = 0;
this._yOffset = 0;
this._runtimeScene = runtimeScene;
runtimeScene.getLayer("").addChildToPIXIContainer(this._tiledSprite, this.zOrder);
};
gdjs.TiledSpriteRuntimeObject.prototype = Object.create( gdjs.RuntimeObject.prototype );
gdjs.TiledSpriteRuntimeObject.thisIsARuntimeObjectConstructor = "TiledSpriteObject::TiledSprite";
gdjs.TiledSpriteRuntimeObject.prototype.onDeletedFromScene = function(runtimeScene) {
runtimeScene.getLayer(this.layer).removePIXIContainerChild(this._tiledSprite);
gdjs.TiledSpriteRuntimeObject.prototype.exposePIXIDisplayObject = function(cb) {
cb(this._tiledSprite);
};
/**
@@ -73,13 +72,6 @@ gdjs.TiledSpriteRuntimeObject.prototype.setAngle = function(angle) {
this._tiledSprite.rotation = gdjs.toRad(angle);
};
gdjs.TiledSpriteRuntimeObject.prototype.setLayer = function(name) {
//We need to move the object from the pixi container of the layer
this._runtimeScene.getLayer(this.layer).removePIXIContainerChild(this._tiledSprite);
this.layer = name;
this._runtimeScene.getLayer(this.layer).addChildToPIXIContainer(this._tiledSprite, this.zOrder);
};
gdjs.TiledSpriteRuntimeObject.prototype.getWidth = function() {
return this._tiledSprite.width;
};
@@ -117,10 +109,3 @@ gdjs.TiledSpriteRuntimeObject.prototype.getXOffset = function() {
gdjs.TiledSpriteRuntimeObject.prototype.getYOffset = function() {
return this._yOffset;
};
gdjs.TiledSpriteRuntimeObject.prototype.setZOrder = function(z) {
if ( z !== this.zOrder ) {
this._runtimeScene.getLayer(this.layer).changePIXIContainerChildZOrder(this._tiledSprite, z);
this.zOrder = z;
}
};

View File

@@ -450,11 +450,14 @@ gdjs.RuntimeGame.prototype.startStandardGameLoop = function() {
//Uncomment to profile the first x frames of the game.
/*var x = 250;
var startTime = Date.now();
console.profile("Stepping for " + x + " frames")
for(var i = 0; i < x; ++i) {
currentScene.renderAndStep();
this._sceneStack.step();
}
console.profileEnd();
var time = Date.now() - startTime;
console.log("Took", time, "ms");
return;*/
requestAnimationFrame(gameLoop);

View File

@@ -33,6 +33,7 @@ gdjs.RuntimeObject = function(runtimeScene, objectData)
this.layer = "";
this.livingOnScene = true;
this.id = runtimeScene.createNewUniqueId();
this._runtimeScene = runtimeScene; //This could/should be avoided.
//Hit boxes:
if ( this._defaultHitBoxes === undefined ) {
@@ -138,6 +139,22 @@ gdjs.RuntimeObject.prototype.deleteFromScene = function(runtimeScene) {
* @param runtimeScene The RuntimeScene owning the object.
*/
gdjs.RuntimeObject.prototype.onDeletedFromScene = function(runtimeScene) {
var theLayer = runtimeScene.getLayer(this.layer);
this.exposePIXIDisplayObject(function(displayObject) {
theLayer.removePIXIContainerChild(displayObject);
});
};
//Rendering:
/**
* Called with a callback function that should be called for
* each PIXI.DisplayObject used by the object
*
* @method exposePIXIDisplayObject
* @param cb The callback to be called with a PIXI.DisplayObject
*/
gdjs.RuntimeObject.prototype.exposePIXIDisplayObject = function(cb) {
};
//Common properties:
@@ -285,26 +302,6 @@ gdjs.RuntimeObject.prototype.rotate = function(speed, runtimeScene) {
this.setAngle(this.getAngle()+speed*runtimeScene.getElapsedTime()/1000);
};
/**
* Set the Z order of the object.
*
* @method setZOrder
* @param z {Number} The new Z order position of the object
*/
gdjs.RuntimeObject.prototype.setZOrder = function(z) {
this.zOrder = z;
};
/**
* Get the Z order of the object.
*
* @method getZOrder
* @return {Number} The Z order of the object
*/
gdjs.RuntimeObject.prototype.getZOrder = function() {
return this.zOrder;
};
/**
* Set the angle of the object.
*
@@ -335,7 +332,15 @@ gdjs.RuntimeObject.prototype.getAngle = function() {
* @return {String} The new layer of the object
*/
gdjs.RuntimeObject.prototype.setLayer = function(layer) {
if (layer === this.layer) return;
this.layer = layer;
var theLayer = this._runtimeScene.getLayer(this.layer);
var that = this;
this.exposePIXIDisplayObject(function (displayObject) {
theLayer.removePIXIContainerChild(displayObject);
theLayer.addChildToPIXIContainer(displayObject, that.zOrder);
});
};
/**
@@ -359,6 +364,33 @@ gdjs.RuntimeObject.prototype.isOnLayer = function(layer) {
return this.layer === layer;
};
/**
* Set the Z order of the object.
*
* @method setZOrder
* @param z {Number} The new Z order position of the object
*/
gdjs.RuntimeObject.prototype.setZOrder = function(z) {
if ( z === this.zOrder ) return;
this.zOrder = z;
var theLayer = this._runtimeScene.getLayer(this.layer);
this.exposePIXIDisplayObject(function(displayObject) {
theLayer.changePIXIContainerChildZOrder(displayObject, z);
});
};
/**
* Get the Z order of the object.
*
* @method getZOrder
* @return {Number} The Z order of the object
*/
gdjs.RuntimeObject.prototype.getZOrder = function() {
return this.zOrder;
};
/**
* Get the container of the object variables
* @method getVariables

View File

@@ -35,7 +35,6 @@ gdjs.RuntimeScene = function(runtimeGame, pixiRenderer)
this._gameStopRequested = false;
this._requestedScene = "";
this._isLoaded = false; // True if loadFromScene was called and the scene is being played.
this.layers = this._layers;
this._allInstancesList = []; //An array used to create a list of all instance when necessary ( see _constructListOfAllInstances )
this._instancesRemoved = []; //The instances removed from the scene and waiting to be sent to the cache.
@@ -201,6 +200,7 @@ gdjs.RuntimeScene.prototype.renderAndStep = function() {
this._updateObjectsPreEvents();
this._eventsFunction(this, this._eventsContext);
this._updateObjects();
this._updateObjectsVisibility();
this.render();
this._firstFrame = false;
@@ -240,6 +240,47 @@ gdjs.RuntimeScene.prototype._updateTime = function() {
this._timeFromStart += this._elapsedTime;
};
gdjs.RuntimeScene.prototype._updateObjectsVisibility = function() {
var allLayers = this._layers.entries();
var layersCameraCoordinates = {};
for(var i = 0;i < allLayers.length;++i) {
var theLayer = allLayers[i][1];
layersCameraCoordinates[allLayers[i][0]] =
[theLayer.getCameraX() - theLayer.getCameraWidth(),
theLayer.getCameraY() - theLayer.getCameraHeight(),
theLayer.getCameraX() + theLayer.getCameraWidth(),
theLayer.getCameraY() + theLayer.getCameraHeight()];
}
function hide(displayObject) {
displayObject.visible = false;
}
function show(displayObject) {
displayObject.visible = true;
}
this._constructListOfAllInstances();
for( var i = 0, len = this._allInstancesList.length;i<len;++i) {
var object = this._allInstancesList[i];
var cameraCoords = layersCameraCoordinates[object.getLayer()];
if (!cameraCoords) continue;
if (object.isHidden()) {
object.exposePIXIDisplayObject(hide);
} else {
var aabb = object.getAABB();
if (aabb.min[0] > cameraCoords[2] || aabb.min[1] > cameraCoords[3] ||
aabb.max[0] < cameraCoords[0] || aabb.max[1] < cameraCoords[1]) {
object.exposePIXIDisplayObject(hide);
} else {
object.exposePIXIDisplayObject(show);
}
}
}
};
/**
* Empty the list of the removed objects:<br>
* When an object is removed from the scene, it is still kept in the _instancesRemoved member

View File

@@ -160,7 +160,6 @@ gdjs.SpriteRuntimeObject = function(runtimeScene, objectData)
this._blendMode = 0;
this._flippedX = false;
this._flippedY = false;
this._runtimeScene = runtimeScene;
this.opacity = 255;
//Animations:
@@ -302,8 +301,8 @@ gdjs.SpriteRuntimeObject.prototype.updateTime = function(elapsedTime) {
if ( this._spriteDirty ) this._updatePIXISprite();
};
gdjs.SpriteRuntimeObject.prototype.onDeletedFromScene = function(runtimeScene) {
runtimeScene.getLayer(this.layer).removePIXIContainerChild(this._sprite);
gdjs.SpriteRuntimeObject.prototype.exposePIXIDisplayObject = function(cb) {
cb(this._sprite);
};
/**
@@ -647,14 +646,6 @@ gdjs.SpriteRuntimeObject.prototype.hide = function(enable) {
this._sprite.alpha = this._sprite.visible ? this.opacity / 255 : 0;
};
gdjs.SpriteRuntimeObject.prototype.setLayer = function(name) {
//We need to move the object from the pixi container of the layer
//TODO: Pass the runtimeScene as parameter ?
this._runtimeScene.getLayer(this.layer).removePIXIContainerChild(this._sprite);
this.layer = name;
this._runtimeScene.getLayer(this.layer).addChildToPIXIContainer(this._sprite, this.zOrder);
};
/**
* Change the tint of the sprite object.
*
@@ -759,20 +750,6 @@ gdjs.SpriteRuntimeObject.prototype.getScaleX = function() {
//Other :
/**
* Set the Z order of the object.
*
* @method setZOrder
* @param z {Number} The new Z order position of the object
*/
gdjs.SpriteRuntimeObject.prototype.setZOrder = function(z) {
if ( z !== this.zOrder ) {
//TODO: Pass the runtimeScene as parameter ?
this._runtimeScene.getLayer(this.layer).changePIXIContainerChildZOrder(this._sprite, z);
this.zOrder = z;
}
};
/**
* @method turnTowardObject
* @param obj The target object