phaser/v3/merge/gameobjects/GameObject.js
2016-11-23 00:17:46 +00:00

356 lines
6.9 KiB
JavaScript

/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* This is the base Game Object class that you can use when creating your own extended Game Objects.
* It hides away the 'private' stuff and exposes only the useful getters, setters and properties.
*
* @class
*/
// Phaser.Texture and Phaser.Frame objects passed in here, instead of looked-up.
// Allows override from non-standard GO types
Phaser.GameObject = function (state, x, y, texture, frame, parent)
{
this.state = state;
this.game = state.game;
this.name = '';
this.type = 0;
this.parent = parent;
// Texture is globally shared between GameObjects, not specific to this one
this.texture = texture;
// Frame is globally shared between GameObjects, not specific to this one
this.frame = frame;
// All GameObjects have the following components, always:
this.transform = new Phaser.Component.Transform(this, x, y);
// Optional? Maybe set on a per GO basis?
this.data = new Phaser.Component.Data(this);
this.color = new Phaser.Component.Color(this);
// ----------------------------------------------------------------
// ----------------------------------------------------------------
// The following properties are debatable to have in this class
// ----------------------------------------------------------------
// ----------------------------------------------------------------
this.scaleMode = Phaser.scaleModes.DEFAULT;
// Allows you to turn off a GameObject from rendering, but still render its children (if it has any)
// Maybe this should move?
// this.skipRender = (key === undefined);
this.skipRender = false;
this.visible = true;
// Either null, or the Children component
this.children = null;
this.exists = true;
};
Phaser.GameObject.prototype.constructor = Phaser.GameObject;
Phaser.GameObject.prototype = {
preUpdate: function ()
{
// NOOP
},
update: function ()
{
// NOOP
},
postUpdate: function ()
{
// NOOP
},
render: function ()
{
// NOOP
},
destroy: function ()
{
// NOOP
}
};
Object.defineProperties(Phaser.GameObject.prototype, {
// Transform getters / setters
x: {
enumerable: true,
get: function ()
{
return this.transform._posX;
},
set: function (value)
{
this.transform._posX = value;
this.transform.dirty = true;
}
},
y: {
enumerable: true,
get: function ()
{
return this.transform._posY;
},
set: function (value)
{
this.transform._posY = value;
this.transform.dirty = true;
}
},
scale: {
enumerable: true,
get: function ()
{
return this.transform._scaleX;
},
set: function (value)
{
this.transform._scaleX = value;
this.transform._scaleY = value;
this.transform.dirty = true;
this.transform.updateCache();
}
},
scaleX: {
enumerable: true,
get: function ()
{
return this.transform._scaleX;
},
set: function (value)
{
this.transform._scaleX = value;
this.transform.dirty = true;
this.transform.updateCache();
}
},
scaleY: {
enumerable: true,
get: function ()
{
return this.transform._scaleY;
},
set: function (value)
{
this.transform._scaleY = value;
this.transform.dirty = true;
this.transform.updateCache();
}
},
anchor: {
enumerable: true,
get: function ()
{
return this.transform._anchorX;
},
set: function (value)
{
this.transform.setAnchor(value);
}
},
anchorX: {
enumerable: true,
get: function ()
{
return this.transform._anchorX;
},
set: function (value)
{
this.transform._anchorX = value;
this.transform.dirty = true;
}
},
anchorY: {
enumerable: true,
get: function ()
{
return this.transform._anchorY;
},
set: function (value)
{
this.transform._anchorY = value;
this.transform.dirty = true;
}
},
pivotX: {
enumerable: true,
get: function ()
{
return this.transform._pivotX;
},
set: function (value)
{
this.transform._pivotX = value;
this.transform.dirty = true;
this.transform.updateCache();
}
},
pivotY: {
enumerable: true,
get: function ()
{
return this.transform._pivotY;
},
set: function (value)
{
this.transform._pivotY = value;
this.transform.dirty = true;
this.transform.updateCache();
}
},
angle: {
enumerable: true,
get: function ()
{
return Phaser.Math.wrapAngle(this.rotation * Phaser.Math.RAD_TO_DEG);
},
set: function (value)
{
this.rotation = Phaser.Math.wrapAngle(value) * Phaser.Math.DEG_TO_RAD;
}
},
rotation: {
enumerable: true,
get: function ()
{
return this.transform._rotation;
},
set: function (value)
{
if (this.transform._rotation === value)
{
return;
}
this.transform._rotation = value;
this.transform.dirty = true;
if (this.transform._rotation % Phaser.Math.PI2)
{
this.transform.cache.sr = Math.sin(this.transform._rotation);
this.transform.cache.cr = Math.cos(this.transform._rotation);
this.transform.updateCache();
this.transform.hasLocalRotation = true;
}
else
{
this.transform.hasLocalRotation = false;
}
}
},
// Color getters / setters
alpha: {
enumerable: true,
get: function ()
{
return this.color._alpha;
},
set: function (value)
{
this.color.alpha = value;
}
},
blendMode: {
enumerable: true,
get: function ()
{
return this.color._blendMode;
},
set: function (value)
{
this.color.blendMode = value;
}
}
});