2016-12-07 02:28:22 +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}
|
|
|
|
*/
|
|
|
|
|
2017-01-30 23:58:29 +00:00
|
|
|
// var CONST = require('../const');
|
2017-01-31 14:55:15 +00:00
|
|
|
var MATH_CONST = require('../math/const');
|
2017-01-18 14:48:02 +00:00
|
|
|
var ScaleModes = require('../renderer/ScaleModes');
|
2016-12-07 02:28:22 +00:00
|
|
|
var Component = require('../components');
|
2017-01-31 14:55:15 +00:00
|
|
|
var WrapAngle = require('../math/angle/Wrap');
|
2016-12-07 02:28:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
|
|
|
var GameObject = function (state, x, y, texture, frame, parent)
|
|
|
|
{
|
|
|
|
this.state = state;
|
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
this.game = state.sys.game;
|
2016-12-07 02:28:22 +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:
|
2017-02-21 00:38:22 +00:00
|
|
|
this.transform = new Component.Transform2D(x, y);
|
2016-12-07 02:28:22 +00:00
|
|
|
|
2017-01-31 14:55:15 +00:00
|
|
|
this.anchor = new Component.Anchor();
|
|
|
|
|
2016-12-07 02:28:22 +00:00
|
|
|
// Optional? Maybe set on a per GO basis?
|
|
|
|
this.data = new Component.Data(this);
|
|
|
|
|
|
|
|
this.color = new Component.Color(this);
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
// The following properties are debatable to have in this class
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
|
2017-01-18 14:48:02 +00:00
|
|
|
this.scaleMode = ScaleModes.DEFAULT;
|
2016-12-07 02:28:22 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
};
|
|
|
|
|
|
|
|
GameObject.prototype.constructor = GameObject;
|
|
|
|
|
|
|
|
GameObject.prototype = {
|
|
|
|
|
|
|
|
preUpdate: function ()
|
|
|
|
{
|
|
|
|
// NOOP
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function ()
|
|
|
|
{
|
|
|
|
// NOOP
|
|
|
|
},
|
|
|
|
|
|
|
|
postUpdate: function ()
|
|
|
|
{
|
|
|
|
// NOOP
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function ()
|
|
|
|
{
|
|
|
|
// NOOP
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
// NOOP
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.defineProperties(GameObject.prototype, {
|
|
|
|
|
|
|
|
// Transform getters / setters
|
|
|
|
|
|
|
|
x: {
|
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
2017-02-21 00:38:22 +00:00
|
|
|
return this.transform.x;
|
2016-12-07 02:28:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
2017-02-21 00:38:22 +00:00
|
|
|
this.transform.x = value;
|
2016-12-07 02:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
y: {
|
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
2017-02-21 00:38:22 +00:00
|
|
|
return this.transform.y;
|
2016-12-07 02:28:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
2017-02-21 00:38:22 +00:00
|
|
|
this.transform.y = value;
|
2016-12-07 02:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
scale: {
|
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
2017-01-31 14:55:15 +00:00
|
|
|
return this.transform.scaleX;
|
2016-12-07 02:28:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
2017-01-31 14:55:15 +00:00
|
|
|
this.transform.scaleX = value;
|
|
|
|
this.transform.scaleY = value;
|
2016-12-07 02:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
scaleX: {
|
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
2017-01-31 14:55:15 +00:00
|
|
|
return this.transform.scaleX;
|
2016-12-07 02:28:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
2017-01-31 14:55:15 +00:00
|
|
|
this.transform.scaleX = value;
|
2016-12-07 02:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
scaleY: {
|
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
2017-01-31 14:55:15 +00:00
|
|
|
return this.transform.scaleY;
|
2016-12-07 02:28:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
2017-01-31 14:55:15 +00:00
|
|
|
this.transform.scaleY = value;
|
2016-12-07 02:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2017-01-31 14:55:15 +00:00
|
|
|
rotation: {
|
2016-12-07 02:28:22 +00:00
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
2017-02-21 00:38:22 +00:00
|
|
|
return this.transform.angle;
|
2016-12-07 02:28:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
2017-02-21 00:38:22 +00:00
|
|
|
this.transform.angle = value;
|
2016-12-07 02:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2017-01-31 14:55:15 +00:00
|
|
|
angle: {
|
2016-12-07 02:28:22 +00:00
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
2017-02-21 00:38:22 +00:00
|
|
|
return WrapAngle(this.transform.angle * MATH_CONST.RAD_TO_DEG);
|
2016-12-07 02:28:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
2017-01-31 14:55:15 +00:00
|
|
|
// value is in degrees
|
2017-02-21 00:38:22 +00:00
|
|
|
this.transform.angle = WrapAngle(value * MATH_CONST.DEG_TO_RAD);
|
2016-12-07 02:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2017-01-31 14:55:15 +00:00
|
|
|
anchorX: {
|
2016-12-07 02:28:22 +00:00
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
2017-01-31 14:55:15 +00:00
|
|
|
return this.anchor.getX();
|
2016-12-07 02:28:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
2017-01-31 14:55:15 +00:00
|
|
|
this.anchor.setX(value);
|
2016-12-07 02:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2017-01-31 14:55:15 +00:00
|
|
|
anchorY: {
|
2016-12-07 02:28:22 +00:00
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
2017-01-31 14:55:15 +00:00
|
|
|
return this.anchor.getY();
|
2016-12-07 02:28:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
2017-01-31 14:55:15 +00:00
|
|
|
this.anchor.setY(value);
|
2016-12-07 02:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2017-01-31 14:55:15 +00:00
|
|
|
/*
|
|
|
|
pivotX: {
|
2016-12-07 02:28:22 +00:00
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
2017-01-31 14:55:15 +00:00
|
|
|
return this.transform._pivotX;
|
2016-12-07 02:28:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
2017-01-31 14:55:15 +00:00
|
|
|
this.transform._pivotX = value;
|
2016-12-07 02:28:22 +00:00
|
|
|
this.transform.dirty = true;
|
|
|
|
this.transform.updateCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2017-01-31 14:55:15 +00:00
|
|
|
pivotY: {
|
2016-12-07 02:28:22 +00:00
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
2017-01-31 14:55:15 +00:00
|
|
|
return this.transform._pivotY;
|
2016-12-07 02:28:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
2017-01-31 14:55:15 +00:00
|
|
|
this.transform._pivotY = value;
|
2016-12-07 02:28:22 +00:00
|
|
|
this.transform.dirty = true;
|
2017-01-31 14:55:15 +00:00
|
|
|
this.transform.updateCache();
|
2016-12-07 02:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
2017-01-30 23:58:29 +00:00
|
|
|
*/
|
2016-12-07 02:28:22 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = GameObject;
|