Skip to content

Commit

Permalink
fix: GraphicsGroup null/undefined guard to prevent crash
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Feb 9, 2024
1 parent 9449d94 commit 3377b41
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Fixed issue where a null/undefined graphics group member graphic would cause a crash, now logs a warning.
- Fixed issue where Actor built in components could not be extended because of the way the Actor based type was built.
- Actors now use instance properties for built-ins instead of getters
- With the ECS refactor you can now subtype built-in `Components` and `.get(Builtin)` will return the correct subtype.
Expand Down
Binary file added sandbox/tests/graphics-group/heart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions sandbox/tests/graphics-group/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Graphics Padding Test</title>
</head>
<body>
<script src="../../lib/excalibur.js"></script>
<script src="index.js"></script>
</body>
</html>
53 changes: 53 additions & 0 deletions sandbox/tests/graphics-group/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/// <reference path="../../lib/excalibur.d.ts" />

var game = new ex.Engine({
width: 1000,
height: 1000,
});

var heartImage = new ex.ImageSource('./heart.png');

var loader = new ex.Loader([heartImage])

class MyActor2 extends ex.Actor {
constructor() {
super({
pos: ex.vec(200, 200)
});
}
onInitialize() {
this.graphics.add(
"interactive",
new ex.GraphicsGroup({
members: [
{
graphic: undefined,
offset: ex.vec(8, 8),
},
{
graphic: heartImage.toSprite(),
offset: ex.vec(8, -16),
},
],
}),
{
anchor: ex.vec(0, 0),
}
);
this.graphics.add(
"noninteractive",
heartImage.toSprite(),
{
anchor: ex.vec(8, 8),
}
)
}

onPreUpdate(engine: ex.Engine<any>, delta: number): void {
this.graphics.use("interactive");
}
}

game.add(new MyActor2());

game.start(loader)
11 changes: 10 additions & 1 deletion src/engine/Graphics/GraphicsGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Graphic, GraphicOptions } from './Graphic';
import { Animation, HasTick } from './Animation';
import { ExcaliburGraphicsContext } from './Context/ExcaliburGraphicsContext';
import { BoundingBox } from '../Collision/Index';
import { Logger } from '../Util/Log';

export interface GraphicsGroupingOptions {
members: (GraphicsGrouping | Graphic)[];
Expand All @@ -14,6 +15,7 @@ export interface GraphicsGrouping {
}

export class GraphicsGroup extends Graphic implements HasTick {
private _logger = Logger.getInstance();
public members: (GraphicsGrouping | Graphic)[] = [];

constructor(options: GraphicsGroupingOptions & GraphicOptions) {
Expand Down Expand Up @@ -43,7 +45,11 @@ export class GraphicsGroup extends Graphic implements HasTick {
bb = member.localBounds.combine(bb);
} else {
const { graphic, offset: pos } = member;
bb = graphic.localBounds.translate(pos).combine(bb);
if (graphic) {
bb = graphic.localBounds.translate(pos).combine(bb);
} else {
this._logger.warnOnce(`Graphics group member has an null or undefined graphic, member definition: ${JSON.stringify(member)}.`);
}
}
}
return bb;
Expand Down Expand Up @@ -96,6 +102,9 @@ export class GraphicsGroup extends Graphic implements HasTick {
graphic = member.graphic;
member.offset.clone(pos);
}
if (!graphic) {
continue;
}
ex.save();
ex.translate(x, y);
graphic.draw(ex, pos.x, pos.y);
Expand Down
2 changes: 1 addition & 1 deletion src/engine/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ implements CanInitialize, CanActivate<TActivationData>, CanDeactivate, CanUpdate
*/
public update(engine: Engine, delta: number) {
if (!this.isInitialized) {
throw new Error('Scene update called before it was initialized!');
throw new Error('Scene update called before it was initialized! Was there an error in actor or entity initialization?');
}
this._preupdate(engine, delta);

Expand Down

0 comments on commit 3377b41

Please sign in to comment.