Skip to content

Commit

Permalink
fix: [#3073] graphics system allows parents without transforms
Browse files Browse the repository at this point in the history
closes #3073
  • Loading branch information
eonarheim committed May 28, 2024
1 parent fc3ab68 commit 01126d4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Fixed issue where `ex.SpriteFont` did not respect scale when measuring text
- Fixed issue where negative transforms would cause collision issues because polygon winding would change.
- Fixed issue where removing and re-adding an actor would cause subsequent children added not to function properly with regards to their parent/child transforms
- Fixed issue where `ex.GraphicsSystem` would crash if a parent entity did not have a `ex.TransformComponent`

### Updates

Expand Down
17 changes: 8 additions & 9 deletions src/engine/Graphics/GraphicsSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,15 @@ export class GraphicsSystem extends System {
for (const ancestor of ancestors) {
const transform = ancestor?.get(TransformComponent);
const optionalBody = ancestor?.get(BodyComponent);
let tx = transform.get();
if (optionalBody) {
if (this._engine.fixedUpdateFps && optionalBody.__oldTransformCaptured && optionalBody.enableFixedUpdateInterpolate) {
// Interpolate graphics if needed
const blend = this._engine.currentFrameLagMs / (1000 / this._engine.fixedUpdateFps);
tx = blendTransform(optionalBody.oldTransform, transform.get(), blend, this._targetInterpolationTransform);
}
}

if (transform) {
let tx = transform.get();
if (optionalBody) {
if (this._engine.fixedUpdateFps && optionalBody.__oldTransformCaptured && optionalBody.enableFixedUpdateInterpolate) {
// Interpolate graphics if needed
const blend = this._engine.currentFrameLagMs / (1000 / this._engine.fixedUpdateFps);
tx = blendTransform(optionalBody.oldTransform, transform.get(), blend, this._targetInterpolationTransform);
}
}
this._graphicsContext.z = transform.globalZ;
this._graphicsContext.translate(tx.pos.x, tx.pos.y);
this._graphicsContext.scale(tx.scale.x, tx.scale.y);
Expand Down
21 changes: 21 additions & 0 deletions src/spec/GraphicsSystemSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,25 @@ describe('A Graphics ECS System', () => {
engine.graphicsContext.flush();
await expectAsync(engine.canvas).toEqualImage('src/spec/images/GraphicsSystemSpec/sword-flip-both-offset.png');
});

it('can add graphics+transform to a parent without a transform', () => {
const world = engine.currentScene.world;
const sut = new ex.GraphicsSystem(world);
engine.currentScene.camera.update(engine, 1);
engine.currentScene._initialize(engine);
engine.screen.setCurrentCamera(engine.currentScene.camera);
sut.initialize(world, engine.currentScene);
sut.preupdate();

const parent = new ex.Entity();
const child = new ex.Entity();
child.addComponent(new ex.TransformComponent());
child.addComponent(new ex.GraphicsComponent());
parent.addChild(child);

sut.query.checkAndAdd(parent);
sut.query.checkAndAdd(child);

expect(() => sut.update(1)).not.toThrow();
});
});

0 comments on commit 01126d4

Please sign in to comment.