lewster32 / phaser-plugin-isometric

Feature-packed axonometric plugin for Phaser 2 which stays true to the Phaser API.
http://rotates.org/phaser/iso
MIT License
472 stars 165 forks source link

isoSprite _isoPositionChanged is broken #46

Open stovenator opened 7 years ago

stovenator commented 7 years ago

In the prototype for isoSprite , there's no way for _isoSpriteChanged to ever get set false, so every update cycle the it updates the isoSprite, regardless of need and has to re-do the math for resetBounds and depth.

Phaser.Plugin.Isometric.IsoSprite.prototype._project = function () {
    if (this._isoPositionChanged) {
        this.game.iso.project(this._isoPosition, this.position);

        if (this.snap > 0) {
            this.position.x = Phaser.Math.snapTo(this.position.x, this.snap);
            this.position.y = Phaser.Math.snapTo(this.position.y, this.snap);
        }

        this._depthChanged = this._isoPositionChanged = this._isoBoundsChanged = true;
    }
};

This can be changed to be:

    this._isoPositionChanged = false;
    this._depthChanged = this._isoBoundsChanged = true;

After this change, it requires that isoSprite positioning is changed only using isoSprite.isoX, isoSprite.isoY, and isoSprite.isoZ because those setters update the _isoPositionChanged flag, or with body.reset if there is a body.

If the position is updated using isoSprite.isoPosition.setTo(x,y,z) , _isoPositionChanged never gets set and thus the position.x, position.y remain unchanged.

lewster32 commented 7 years ago

Nice spot - I'll take a look at this and see what I can do to maintain compatibility. This as you can probably tell is a caching technique to prevent unnecessary projection calculations but it looks like it's not doing its job!

stovenator commented 7 years ago

I also noticed that in the Body postUpdate, there's no dirty check. I adjusted it in mine to look like this, but likely it could be optimized more:

            if(this._dx !== 0 || this._dy !== 0 || this._dx !== 0){
                this.sprite.isoX += this._dx;
                this.sprite.isoY += this._dy;
                this.sprite.isoZ += this._dz;
            }

...

        if (this.position.x !== this.prev.x || this.position.y !== this.prev.y || this.position.z !== this.prev.z){
            this.prev.x = this.position.x;
            this.prev.y = this.position.y;
            this.prev.z = this.position.z;
        }