excaliburjs / Excalibur

🎮 Your friendly TypeScript 2D game engine for the web 🗡️
https://excaliburjs.com
BSD 2-Clause "Simplified" License
1.66k stars 182 forks source link

Align onXYZ method signatures to match event payloads #3106

Open mattjennings opened 2 weeks ago

mattjennings commented 2 weeks ago

Context

Actors (and maybe other classes) expose methods such as onPreUpdate or onCollisionStart as convenient handlers for events to avoid manually wiring up to events.

However, the method signature is different than what you would get if you did manually wire up to the event.


// method on Actor
class Player extends ex.Actor {
   onPreUpdate(engine: Engine, delta: number) {}
}

// wiring up to event manually e.g. when extending Entity
class Player extends ex.Entity {
   constructor() {
     super()

     this.on('preupdate', this.onPreUpdate)
   }

   onPreUpdate({ engine, delta }: ex.PreUpdateEvent) {}
}

This makes it inconvenient if I want to move code around in such a way that i'm going to/from an event handler/class method.

Proposal

This is a severely breaking change

Align all onXYZ convenience methods to use event as the parameter rather than multiple parameters from the event.

class Player extends ex.Actor {
   onPreUpdate({ engine, delta }: ex.PreUpdateEvent) {}
}
mattjennings commented 2 weeks ago

we could consider writing a codemod to help with the breaking change