123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- class Object2D extends GameObject
- {
-
- constructor(name)
- {
- super(name);
- this.position = Vector2.ZERO();
- this.rotationDegrees = 0;
- this.scale = Vector2.ONE();
- this.visible = true;
- this.globalPosition = Vector2.ZERO();
- this.globalRotationDegrees = 0;
- this.globalScale = Vector2.ONE();
- }
-
- setPosition(x, y)
- {
- this.position.x = x;
- this.position.y = y;
- }
-
- show()
- {
- this.visible = true;
- for (let i = 0; i < this.children.length; i++)
- {
- if (!this.children[i].show) continue;
- this.children[i].show();
- }
- }
-
- hide()
- {
- this.visible = false;
- for (let i = 0; i < this.children.length; i++)
- {
- if (!this.children[i].hide) continue;
- this.children[i].hide();
- }
- }
-
- setVisibility(val)
- {
- this.visible = val;
- for (let i = 0; i < this.children.length; i++)
- {
- if (!this.children[i].setVisibility) continue;
- this.children[i].setVisibility(val);
- }
- }
-
- getVisibility()
- {
- return this.visible;
- }
-
- translate(x, y)
- {
- this.position.x += x;
- this.position.y += y;
- }
-
- rotate(a)
- {
- this.rotationDegrees += a;
- }
-
- addScale(x, y)
- {
- this.scale.x *= x;
- this.scale.y *= y;
- }
-
- udpateGlobalTransform()
- {
- if (!this.parented || !(this.parent instanceof Object2D))
- {
- this.globalPosition = this.position;
- this.globalRotationDegrees = this.rotationDegrees;
- this.globalScale = this.scale;
- }
- else
- {
- this.globalPosition.x = this.parent.globalPosition.x + this.position.x;
- this.globalPosition.y = this.parent.globalPosition.y + this.position.y;
- this.globalRotationDegrees = this.parent.globalRotationDegrees + this.rotationDegrees;
- this.globalScale.x = this.parent.globalScale.x * this.scale.x;
- this.globalScale.y = this.parent.globalScale.y * this.scale.y;
- }
- }
-
- update(delta)
- {
- this.udpateGlobalTransform();
- this.updateChildren(delta);
- }
-
- applyTransform(db)
- {
- db.translate(this.position.x, this.position.y);
- db.rotate(this.rotationDegrees / 180 * PI);
- db.scale(this.scale.x, this.scale.y);
- }
-
- draw(delta, db)
- {
- if (!this.visible) return;
- db.push();
- this.applyTransform(db);
- this.drawChildren(delta, db);
- db.pop()
- }
- }
|