|
| 1 | +var Class = require('../../utils/Class'); |
| 2 | +var Components = require('../components'); |
| 3 | +var GameObject = require('../GameObject'); |
| 4 | +var Render = require('./ContainerRender'); |
| 5 | + |
| 6 | +var Container = new Class({ |
| 7 | + |
| 8 | + Extends: GameObject, |
| 9 | + |
| 10 | + Mixins: [ |
| 11 | + Components.BlendMode, |
| 12 | + Components.Transform, |
| 13 | + Render |
| 14 | + ], |
| 15 | + |
| 16 | + initialize: |
| 17 | + |
| 18 | + function Container(scene, x, y) |
| 19 | + { |
| 20 | + GameObject.call(this, scene, 'Container'); |
| 21 | + this.parentContainer = null; |
| 22 | + this.children = []; |
| 23 | + this.setPosition(x, y); |
| 24 | + this.localTransform = new Components.TransformMatrix(); |
| 25 | + this.tempTransformMatrix = new Components.TransformMatrix(); |
| 26 | + }, |
| 27 | + |
| 28 | + add: function (gameObject) |
| 29 | + { |
| 30 | + if (gameObject.type === 'Container') |
| 31 | + { |
| 32 | + gameObject.parentContainer = this; |
| 33 | + } |
| 34 | + if (this.children.indexOf(gameObject) < 0) |
| 35 | + { |
| 36 | + this.children.push(gameObject); |
| 37 | + } |
| 38 | + return this; |
| 39 | + }, |
| 40 | + |
| 41 | + remove: function (gameObject) |
| 42 | + { |
| 43 | + var index = this.children.indexOf(gameObject); |
| 44 | + if (index >= 0) |
| 45 | + { |
| 46 | + if (gameObject.type === 'Container') |
| 47 | + { |
| 48 | + gameObject.parentContainer = null; |
| 49 | + } |
| 50 | + this.children.splice(index, 1); |
| 51 | + } |
| 52 | + return this; |
| 53 | + }, |
| 54 | + |
| 55 | + pointToContainer: function (pointSrc, pointDst) |
| 56 | + { |
| 57 | + var parent = this.parentContainer; |
| 58 | + var tempMatrix = this.tempTransformMatrix; |
| 59 | + |
| 60 | + if (pointDst === undefined) |
| 61 | + { |
| 62 | + pointDst = { x: 0, y: 0 }; |
| 63 | + } |
| 64 | + |
| 65 | + if (parent !== null) |
| 66 | + { |
| 67 | + parent.pointToContainer(pointSrc, pointDst); |
| 68 | + } |
| 69 | + |
| 70 | + tempMatrix.loadIdentity(); |
| 71 | + tempMatrix.applyITRS(this.x, this.y, this.rotation, this.scaleX, this.scaleY); |
| 72 | + tempMatrix.invert(); |
| 73 | + tempMatrix.transformPoint(pointSrc.x, pointSrc.y, pointDst); |
| 74 | + |
| 75 | + return pointDst; |
| 76 | + } |
| 77 | +}); |
| 78 | + |
| 79 | +module.exports = Container; |
0 commit comments