phaser/v3/src/gameobjects/GameObject.js

328 lines
5.9 KiB
JavaScript
Raw Normal View History

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}
*/
// var CONST = require('../const');
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');
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;
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:
this.transform = new Component.Transform2D(x, y);
2016-12-07 02:28:22 +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 ()
{
return this.transform.x;
2016-12-07 02:28:22 +00:00
},
set: function (value)
{
this.transform.x = value;
2016-12-07 02:28:22 +00:00
}
},
y: {
enumerable: true,
get: function ()
{
return this.transform.y;
2016-12-07 02:28:22 +00:00
},
set: function (value)
{
this.transform.y = value;
2016-12-07 02:28:22 +00:00
}
},
scale: {
enumerable: true,
get: function ()
{
return this.transform.scaleX;
2016-12-07 02:28:22 +00:00
},
set: function (value)
{
this.transform.scaleX = value;
this.transform.scaleY = value;
2016-12-07 02:28:22 +00:00
}
},
scaleX: {
enumerable: true,
get: function ()
{
return this.transform.scaleX;
2016-12-07 02:28:22 +00:00
},
set: function (value)
{
this.transform.scaleX = value;
2016-12-07 02:28:22 +00:00
}
},
scaleY: {
enumerable: true,
get: function ()
{
return this.transform.scaleY;
2016-12-07 02:28:22 +00:00
},
set: function (value)
{
this.transform.scaleY = value;
2016-12-07 02:28:22 +00:00
}
},
rotation: {
2016-12-07 02:28:22 +00:00
enumerable: true,
get: function ()
{
return this.transform.angle;
2016-12-07 02:28:22 +00:00
},
set: function (value)
{
this.transform.angle = value;
2016-12-07 02:28:22 +00:00
}
},
angle: {
2016-12-07 02:28:22 +00:00
enumerable: true,
get: function ()
{
return WrapAngle(this.transform.angle * MATH_CONST.RAD_TO_DEG);
2016-12-07 02:28:22 +00:00
},
set: function (value)
{
// value is in degrees
this.transform.angle = WrapAngle(value * MATH_CONST.DEG_TO_RAD);
2016-12-07 02:28:22 +00:00
}
},
anchorX: {
2016-12-07 02:28:22 +00:00
enumerable: true,
get: function ()
{
return this.anchor.getX();
2016-12-07 02:28:22 +00:00
},
set: function (value)
{
this.anchor.setX(value);
2016-12-07 02:28:22 +00:00
}
},
anchorY: {
2016-12-07 02:28:22 +00:00
enumerable: true,
get: function ()
{
return this.anchor.getY();
2016-12-07 02:28:22 +00:00
},
set: function (value)
{
this.anchor.setY(value);
2016-12-07 02:28:22 +00:00
}
},
/*
pivotX: {
2016-12-07 02:28:22 +00:00
enumerable: true,
get: function ()
{
return this.transform._pivotX;
2016-12-07 02:28:22 +00:00
},
set: function (value)
{
this.transform._pivotX = value;
2016-12-07 02:28:22 +00:00
this.transform.dirty = true;
this.transform.updateCache();
}
},
pivotY: {
2016-12-07 02:28:22 +00:00
enumerable: true,
get: function ()
{
return this.transform._pivotY;
2016-12-07 02:28:22 +00:00
},
set: function (value)
{
this.transform._pivotY = value;
2016-12-07 02:28:22 +00:00
this.transform.dirty = true;
this.transform.updateCache();
2016-12-07 02:28:22 +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;