phaser/wip/gameobjects/GameObject.js

357 lines
6.9 KiB
JavaScript
Raw Normal View History

2016-10-09 22:39:27 +00:00
/**
* @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)
2016-10-09 22:39:27 +00:00
{
this.state = state;
this.game = state.game;
2016-10-09 22:39:27 +00:00
2016-10-14 07:59:24 +00:00
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);
2016-10-14 07:59:24 +00:00
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
2016-10-09 22:39:27 +00:00
this.children = null;
this.exists = true;
2016-10-14 07:59:24 +00:00
};
Phaser.GameObject.prototype.constructor = Phaser.GameObject;
Phaser.GameObject.prototype = {
preUpdate: function ()
{
// NOOP
},
update: function ()
{
// NOOP
},
postUpdate: function ()
{
// NOOP
},
render: function ()
{
// NOOP
},
destroy: function ()
2016-10-14 07:59:24 +00:00
{
// NOOP
}
2016-10-09 22:39:27 +00:00
};
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;
}
}
});