diff --git a/CHANGELOG.md b/CHANGELOG.md index f4edc6d15..cce79ba88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/engine/Graphics/GraphicsSystem.ts b/src/engine/Graphics/GraphicsSystem.ts index 42674562c..aefd65723 100644 --- a/src/engine/Graphics/GraphicsSystem.ts +++ b/src/engine/Graphics/GraphicsSystem.ts @@ -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); diff --git a/src/spec/GraphicsSystemSpec.ts b/src/spec/GraphicsSystemSpec.ts index 06527269c..15d1d625e 100644 --- a/src/spec/GraphicsSystemSpec.ts +++ b/src/spec/GraphicsSystemSpec.ts @@ -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(); + }); });