Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [#1687] Actor z-index regression #1679

Merged
merged 3 commits into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Fixed z-index regression where actors did not respect z-index ([#1678](https://github.com/excaliburjs/Excalibur/issues/1678))
- Fixed Animation flicker bug when switching to an animation ([#1636](https://github.com/excaliburjs/Excalibur/issues/1636))
- Fixed `ex.Actor.easeTo` actions, they now use velocity to move Actors ([#1638](https://github.com/excaliburjs/Excalibur/issues/1638))

Expand Down
7 changes: 4 additions & 3 deletions src/engine/Actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,9 +977,10 @@ export class ActorImpl extends Entity implements Actionable, Eventable, PointerE
* @param newIndex new z-index to assign
*/
public setZIndex(newIndex: number) {
this.scene.cleanupDrawTree(this);
this._zIndex = newIndex;
this.scene.updateDrawTree(this);
const newZ = newIndex;
this.scene?.cleanupDrawTree(this);
this._zIndex = newZ;
this.scene?.updateDrawTree(this);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/engine/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class Scene extends Class implements CanInitialize, CanActivate, CanDeact

private _isInitialized: boolean = false;

private _sortedDrawingTree: SortedList<Actor> = new SortedList<Actor>(Actor.prototype.getZIndex);
private _sortedDrawingTree: SortedList<Actor> = new SortedList<Actor>(a => a.z);

private _broadphase: CollisionBroadphase = new DynamicTreeCollisionBroadphase();

Expand Down
4 changes: 2 additions & 2 deletions src/engine/Util/SortedList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ export class SortedList<T> {
}
} else if (this._getComparable(element) < node.getKey()) {
if (node.getLeft() == null) {
node.setLeft(new BinaryTreeNode(this._getComparable.call(element, element), [element], null, null));
node.setLeft(new BinaryTreeNode(this._getComparable(element), [element], null, null));
return true;
} else {
return this._insert(node.getLeft(), element);
}
} else {
if (node.getRight() == null) {
node.setRight(new BinaryTreeNode(this._getComparable.call(element, element), [element], null, null));
node.setRight(new BinaryTreeNode(this._getComparable(element), [element], null, null));
return true;
} else {
return this._insert(node.getRight(), element);
Expand Down
35 changes: 34 additions & 1 deletion src/spec/ActorSpec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExcaliburMatchers, ensureImagesLoaded } from 'excalibur-jasmine';
import * as ex from '@excalibur';
import { ensureImagesLoaded, ExcaliburMatchers } from 'excalibur-jasmine';
import { TestUtils } from './util/TestUtils';

describe('A game actor', () => {
Expand Down Expand Up @@ -520,6 +520,39 @@ describe('A game actor', () => {
expect(invisibleActor.draw).not.toHaveBeenCalled();
});

it('can be drawn with a z-index', (done) => {
engine = TestUtils.engine({
width: 62,
height: 64,
suppressHiDPIScaling: true
});

const green = new ex.Actor({ x: 50, y: 50, width: 40, height: 40, color: ex.Color.Green });
const blue = new ex.Actor({ x: 40, y: 40, width: 40, height: 40, color: ex.Color.Blue });

green.z = 1;
blue.z = 2;

// Actors currently need to be in a scene for z to work
scene.add(blue);
scene.add(green);

scene.draw(engine.ctx, 100);

ensureImagesLoaded(engine.canvas, 'src/spec/images/ActorSpec/zindex-blue-top.png').then(([canvas, image]) => {
expect(canvas).toEqualImage(image);

green.z = 2;
blue.z = 1;
scene.draw(engine.ctx, 100);

ensureImagesLoaded(engine.canvas, 'src/spec/images/ActorSpec/zindex-green-top.png').then(([canvas, image]) => {
expect(canvas).toEqualImage(image);
done();
});
});
});

it('can have a graphic drawn at an opacity', (done) => {
engine = TestUtils.engine({
width: 62,
Expand Down
Binary file added src/spec/images/ActorSpec/zindex-blue-top.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/spec/images/ActorSpec/zindex-green-top.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.