Skip to content

Commit

Permalink
fix: Prevent errant overrides of .update()
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Jan 21, 2024
1 parent c73d459 commit df59264
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

- Remove confusing Graphics Layering from `ex.GraphicsComponent`, recommend we use the `ex.GraphicsGroup` to manage this behavior
* Update `ex.GraphicsGroup` to be consistent and use `offset` instead of `pos` for graphics relative positioning
- Prevent people from inadvertently overriding `update()` in `ex.Scene` and `ex.Actor`. This method can still be overridden with the `//@ts-ignore` pragma

### Deprecated

Expand Down
10 changes: 9 additions & 1 deletion sandbox/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -954,4 +954,12 @@ game.currentScene.camera.y = 200;
// Run the mainloop
game.start(boot).then(() => {
logger.info('All Resources have finished loading');
});
});


class MySceneCustomUpdate extends ex.Scene {
//@ts-ignore
update() {
console.log('my update')
}
}
1 change: 1 addition & 0 deletions sandbox/tests/collisionvelocity/vel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var player = new ex.Actor({
collisionType: ex.CollisionType.Active
});

//@ts-ignore
player.update = (e, ms) => {
if (engine.input.keyboard.isHeld(ex.Keys.Space)) {
player.vel.x = 0;
Expand Down
1 change: 1 addition & 0 deletions sandbox/tests/coordinates/coordinates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class PointLabel extends ex.Actor {
this.addChild(this._actualLabel);
}

//@ts-ignore
update(engine: ex.Engine, delta: number) {
super.update(engine, delta);

Expand Down
10 changes: 9 additions & 1 deletion src/engine/Actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ import { Text } from './Graphics/Text';
import { CoordPlane } from './Math/coord-plane';
import { EventEmitter, EventKey, Handler, Subscription } from './EventEmitter';

declare const _: unique symbol;
type Forbidden = { [_]: typeof _; }
type NoOverride<T=void> = T & Forbidden;

/**
* Type guard for checking if something is an Actor
* @param x
Expand Down Expand Up @@ -852,12 +856,16 @@ export class Actor extends Entity implements Eventable, PointerEvents, CanInitia
// #region Update

/**
* It is not recommended that internal excalibur methods be overridden, do so at your own risk.
*
* Use **[[onPreUpdate]]** or **[[onPostUpdate]]**, or be sure to call the **`super.update()`**
*
* Called by the Engine, updates the state of the actor
* @internal
* @param engine The reference to the current game engine
* @param delta The time elapsed since the last update in milliseconds
*/
public update(engine: Engine, delta: number) {
public update(engine: Engine, delta: number): NoOverride<void> {
this._initialize(engine);
this._preupdate(engine, delta);
this._postupdate(engine, delta);
Expand Down
2 changes: 2 additions & 0 deletions src/engine/Particles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export class ParticleImpl extends Entity {
this.emitter.removeParticle(this);
}

//@ts-ignore
public update(engine: Engine, delta: number) {
this.life = this.life - delta;
this.elapsedMultiplier = this.elapsedMultiplier + delta;
Expand Down Expand Up @@ -596,6 +597,7 @@ export class ParticleEmitter extends Actor {
return p;
}

//@ts-ignore
public update(engine: Engine, delta: number) {
super.update(engine, delta);

Expand Down
18 changes: 16 additions & 2 deletions src/engine/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ import { Color } from './Color';
import { DefaultLoader } from './Director/DefaultLoader';
import { Transition } from './Director';

declare const _: unique symbol;
type Forbidden = { [_]: typeof _; }
type NoOverride<T=void> = T & Forbidden;

export class PreLoadEvent {
loader: DefaultLoader;
}
Expand Down Expand Up @@ -395,11 +399,16 @@ implements CanInitialize, CanActivate<TActivationData>, CanDeactivate, CanUpdate
}

/**
* It is not recommended that internal excalibur methods be overridden, do so at your own risk.
*
* Use **[[onPreUpdate]]** or **[[onPostUpdate]]**, or be sure to call the **`super.update()`**
*
* Updates all the actors and timers in the scene. Called by the [[Engine]].
* @internal
* @param engine Reference to the current Engine
* @param delta The number of milliseconds since the last update
*/
public update(engine: Engine, delta: number) {
public update(engine: Engine, delta: number): NoOverride<void> {
if (!this.isInitialized) {
throw new Error('Scene update called before it was initialized!');
}
Expand Down Expand Up @@ -431,11 +440,16 @@ implements CanInitialize, CanActivate<TActivationData>, CanDeactivate, CanUpdate
}

/**
* It is not recommended that internal excalibur methods be overridden, do so at your own risk.
*
* Use **[[onPreDraw]]** or **[[onPostDraw]]**, or be sure to call the **`super.update()`**
*
* Draws all the actors in the Scene. Called by the [[Engine]].
* @internal
* @param ctx The current rendering context
* @param delta The number of milliseconds since the last draw
*/
public draw(ctx: ExcaliburGraphicsContext, delta: number) {
public draw(ctx: ExcaliburGraphicsContext, delta: number): NoOverride<void> {
this._predraw(ctx, delta);

this.world.update(SystemType.Draw, delta);
Expand Down

0 comments on commit df59264

Please sign in to comment.