2018-03-23 17:15:52 +00:00
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var Components = require('../components');
|
|
|
|
var GameObject = require('../GameObject');
|
|
|
|
var Render = require('./ContainerRender');
|
|
|
|
|
|
|
|
var Container = new Class({
|
|
|
|
|
|
|
|
Extends: GameObject,
|
|
|
|
|
|
|
|
Mixins: [
|
|
|
|
Components.BlendMode,
|
|
|
|
Components.Transform,
|
|
|
|
Render
|
|
|
|
],
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2018-04-05 08:02:36 +00:00
|
|
|
function Container (scene, x, y)
|
2018-03-23 17:15:52 +00:00
|
|
|
{
|
|
|
|
GameObject.call(this, scene, 'Container');
|
|
|
|
this.parentContainer = null;
|
|
|
|
this.children = [];
|
|
|
|
this.setPosition(x, y);
|
|
|
|
this.localTransform = new Components.TransformMatrix();
|
2018-03-28 23:47:57 +00:00
|
|
|
this.tempTransformMatrix = new Components.TransformMatrix();
|
2018-03-23 17:15:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
add: function (gameObject)
|
|
|
|
{
|
2018-03-29 15:34:23 +00:00
|
|
|
if (gameObject.type === 'Container')
|
|
|
|
{
|
|
|
|
gameObject.parentContainer = this;
|
|
|
|
}
|
2018-03-23 17:15:52 +00:00
|
|
|
if (this.children.indexOf(gameObject) < 0)
|
|
|
|
{
|
|
|
|
this.children.push(gameObject);
|
|
|
|
}
|
2018-03-26 20:23:18 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
remove: function (gameObject)
|
|
|
|
{
|
|
|
|
var index = this.children.indexOf(gameObject);
|
|
|
|
if (index >= 0)
|
|
|
|
{
|
2018-03-29 15:34:23 +00:00
|
|
|
if (gameObject.type === 'Container')
|
|
|
|
{
|
|
|
|
gameObject.parentContainer = null;
|
|
|
|
}
|
2018-03-26 20:23:18 +00:00
|
|
|
this.children.splice(index, 1);
|
|
|
|
}
|
|
|
|
return this;
|
2018-03-28 23:47:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
pointToContainer: function (pointSrc, pointDst)
|
|
|
|
{
|
|
|
|
var parent = this.parentContainer;
|
|
|
|
var tempMatrix = this.tempTransformMatrix;
|
|
|
|
|
|
|
|
if (pointDst === undefined)
|
|
|
|
{
|
|
|
|
pointDst = { x: 0, y: 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent !== null)
|
|
|
|
{
|
|
|
|
parent.pointToContainer(pointSrc, pointDst);
|
|
|
|
}
|
|
|
|
|
|
|
|
tempMatrix.loadIdentity();
|
|
|
|
tempMatrix.applyITRS(this.x, this.y, this.rotation, this.scaleX, this.scaleY);
|
|
|
|
tempMatrix.invert();
|
|
|
|
tempMatrix.transformPoint(pointSrc.x, pointSrc.y, pointDst);
|
|
|
|
|
|
|
|
return pointDst;
|
2018-03-23 17:15:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Container;
|