diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f746d133..343c0e7b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added -- +- Added `GraphicsComponent.bounds` which will report the world bounds of the graphic if applicable! ### Fixed diff --git a/src/engine/Graphics/GraphicsComponent.ts b/src/engine/Graphics/GraphicsComponent.ts index c0d67b456..6559d940b 100644 --- a/src/engine/Graphics/GraphicsComponent.ts +++ b/src/engine/Graphics/GraphicsComponent.ts @@ -7,6 +7,7 @@ import { Component } from '../EntityComponentSystem/Component'; import { Material } from './Context/material'; import { Logger } from '../Util/Log'; import { WatchVector } from '../Math/watch-vector'; +import { TransformComponent } from '../EntityComponentSystem'; /** * Type guard for checking if a Graphic HasTick (used for graphics that change over time like animations) @@ -362,6 +363,9 @@ export class GraphicsComponent extends Component { this._localBounds = bb; } + /** + * Get local bounds of graphics component + */ public get localBounds(): BoundingBox { if (!this._localBounds || this._localBounds.hasZeroDimensions()) { this.recalculateBounds(); @@ -369,6 +373,20 @@ export class GraphicsComponent extends Component { return this._localBounds; } + /** + * Get world bounds of graphics component + */ + public get bounds(): BoundingBox { + let bounds = this.localBounds; + if (this.owner) { + const tx = this.owner.get(TransformComponent); + if (tx) { + bounds = bounds.transform(tx.get().matrix); + } + } + return bounds; + } + /** * Update underlying graphics if necessary, called internally * @param elapsed diff --git a/src/spec/GraphicsComponentSpec.ts b/src/spec/GraphicsComponentSpec.ts index 25607a191..b20d97fea 100644 --- a/src/spec/GraphicsComponentSpec.ts +++ b/src/spec/GraphicsComponentSpec.ts @@ -289,6 +289,27 @@ describe('A Graphics ECS Component', () => { })); }); + it('correctly calculates graphics world bounds (rasters)', () => { + const tx = new ex.TransformComponent(); + tx.pos = ex.vec(500, 900); + const sut = new ex.GraphicsComponent(); + const rec2 = new ex.Rectangle({ + width: 200, + height: 10 + }); + rec2.scale = ex.vec(2, 2); + sut.add(rec2); + + const entity = new ex.Entity([tx, sut]); + + expect(sut.bounds).toEqual(new ex.BoundingBox({ + left: 300, + right: 700, + top: 890, + bottom: 910 + })); + }); + it('correctly calculates graphics bounds (rasters + offset)', () => { const sut = new ex.GraphicsComponent(); const rec2 = new ex.Rectangle({