diff --git a/examples/fire/fire.js b/examples/fire/fire.js index 74d63bc..073248d 100644 --- a/examples/fire/fire.js +++ b/examples/fire/fire.js @@ -52,18 +52,18 @@ class FireParticle extends MomentumEngine.Classes.Entity { render () { var gradient = this._game.context.createRadialGradient( - this._calculatedPos.x + fireParticleWidth / 2, - this._calculatedPos.y + fireParticleHeight / 2, + this._relativePos.x + fireParticleWidth / 2, + this._relativePos.y + fireParticleHeight / 2, fireParticleWidth / 10, - this._calculatedPos.x + fireParticleWidth / 2, - this._calculatedPos.y + fireParticleHeight / 2, + this._relativePos.x + fireParticleWidth / 2, + this._relativePos.y + fireParticleHeight / 2, fireParticleWidth / 2 ); gradient.addColorStop(0, this.startColor.toString()); gradient.addColorStop(1, "rgba(0, 0, 0, 0)"); this._game.context.fillStyle = gradient; - this._game.context.fillRect(this._calculatedPos.x, this._calculatedPos.y, fireParticleWidth, fireParticleHeight); + this._game.context.fillRect(this._relativePos.x, this._relativePos.y, fireParticleWidth, fireParticleHeight); return true; diff --git a/src/classes/emitter.js b/src/classes/emitter.js index fc7b1a7..1824b8e 100644 --- a/src/classes/emitter.js +++ b/src/classes/emitter.js @@ -42,7 +42,8 @@ class Emitter extends Entity { magnitude = this.particleVelocity.length(), velocity = Vector2D.fromAngle(angle, magnitude); - let particle = new ParticleClass(this._calculatedPos.x, this._calculatedPos.y); + // NK: This might cause a bug where child renders have an incorrect position because preprocess should normally be called after the update function but before the render, here it is before update. We'll see. + let particle = new ParticleClass(this.relativeX, this.relativeY); particle.velocity = velocity; Utils.mergeIntoArray(particle.fields, this.particleFields); @@ -54,10 +55,6 @@ class Emitter extends Entity { _triggerEmissions () { - // We prematurely call preprocess so that child particles can spawn from the emitters permission but be children of a different parent - // NK: This might cause a bug where child renders have an incorrect position because preprocess should normally be called after the update function but before the render, here it is before update. We'll see. - this._preprocess(); - if (this.emitting) { let currentTime = +(new Date()); diff --git a/src/classes/entity.js b/src/classes/entity.js index 93be8eb..e4f34ed 100644 --- a/src/classes/entity.js +++ b/src/classes/entity.js @@ -16,7 +16,7 @@ class Entity { this.state = {}; this.children = []; - this._calculatedPos = this.pos.clone(); + this._relativePos = this.pos.clone(); this._lastCalculated = 0; this._game = null; this._parent = null; @@ -26,6 +26,34 @@ class Entity { } + get x () { + return this.pos.x; + } + + set x (val) { + return this.pos.x = val; + } + + + get y () { + return this.pos.y; + } + + set y (val) { + return this.pos.y = val; + } + + + get relativeX () { + return this._calculateRelativePos().x; + } + + + get relativeY () { + return this._calculateRelativePos().y; + } + + setVelocity (x, y) { if (x instanceof Vector2D) { @@ -90,33 +118,31 @@ class Entity { } - _preprocess () { + _calculateRelativePos () { - // NK: The purpose of this function is to calculate the true position of the entity relative to all its parents. It does this recursively, calling the _preprocess method all the way back up the tree and continuously adding the results together. + // NK: The purpose of this function is to calculate the true position of the entity relative to all its parents. It does this recursively, calling the _calculateRelativePos method all the way back up the tree and continuously adding the results together. // Note there is a limiter, where the last calculated frame is stored, so that if the position has already been calculated for that node in this particular frame, the cached result is used rather than recalculating. - // When rendering, the draw calls should use this._calculatedPos rather than this.pos in order for the position to be correct. + // When rendering, the draw calls should use this._relativePos rather than this.pos in order for the position to be correct. if (this._game && this._lastCalculated < this._game.frameCounter) { if (this._parent) { - let parentPos = this._parent._preprocess(); - - this._calculatedPos.x = this.pos.x + parentPos.x; - this._calculatedPos.y = this.pos.y + parentPos.y; + this._relativePos.x = this.x + this._parent.relativeX; + this._relativePos.y = this.y + this._parent.relativeY; } else { - this._calculatedPos.x = this.pos.x; - this._calculatedPos.y = this.pos.y; + this._relativePos.x = this.x; + this._relativePos.y = this.y; } this._lastCalculated = this._game.frameCounter; } - return this._calculatedPos; + return this._relativePos; } @@ -134,18 +160,16 @@ class Entity { _calculateFields (delta) { - this._preprocess(); // NK: I don't like calling preprocess everywhere outside of the render method... - let acceleration = new Vector2D(0, 0); for (let i = 0; i < this.fields.length; i++) { let field = this.fields[i]; - field._preprocess(); + // NK: These call _relativePos, I don't like using this outside of the render method... let vector = new Vector2D( - field._calculatedPos.x - this._calculatedPos.x, - field._calculatedPos.y - this._calculatedPos.y + field.relativeX - this.relativeX, + field.relativeY - this.relativeY ); let force = field.mass / Math.pow(vector.dot(vector), 1.5); @@ -191,8 +215,6 @@ class Entity { _renderEntity () { - this._preprocess(); - let rendered = this.render && this.render(); if (rendered) { diff --git a/src/classes/rect.js b/src/classes/rect.js index 929a17b..ccb09ef 100644 --- a/src/classes/rect.js +++ b/src/classes/rect.js @@ -33,7 +33,7 @@ class Rect extends Entity { if (this._game) { this._game.context.fillStyle = this.color.toString(); - this._game.context.fillRect(this._calculatedPos.x, this._calculatedPos.y, this.size.x, this.size.y); + this._game.context.fillRect(this.relativeX, this.relativeY, this.size.x, this.size.y); return true; diff --git a/src/classes/sprite.js b/src/classes/sprite.js index 3aa0048..df4323b 100644 --- a/src/classes/sprite.js +++ b/src/classes/sprite.js @@ -57,8 +57,8 @@ class Sprite extends Entity { this._imagePos.y, this._imageSize.x || subWidth, this._imageSize.y || subHeight, - this._calculatedPos.x, - this._calculatedPos.y, + this.relativeX, + this.relativeY, this.size.x || subWidth, this.size.y || subHeight ); diff --git a/src/libs/collisionmethods.js b/src/libs/collisionmethods.js index cec5d57..9f11fa2 100644 --- a/src/libs/collisionmethods.js +++ b/src/libs/collisionmethods.js @@ -9,10 +9,10 @@ class CollisionMethods { throw new Error("AABB collisions can only be checked on these entity types: Rect"); } - return (entity1.pos.x < entity2.pos.x + entity2.size.x && - entity1.pos.x + entity1.size.x > entity2.pos.x && - entity1.pos.y < entity2.pos.y + entity2.size.y && - entity1.size.y + entity1.pos.y > entity2.pos.y); + return (entity1.x < entity2.x + entity2.size.x && + entity1.x + entity1.size.x > entity2.x && + entity1.y < entity2.y + entity2.size.y && + entity1.size.y + entity1.y > entity2.y); }