I am currently working on expanding Phaser by incorporating a new module called Phaser.Physics.Box2D
. While Phaser already utilizes this module internally, it is an additional plugin and I am determined to create my own version.
TypeScript is the language of choice for my project, and I want to ensure that I extend Phaser properly. After some searching, I came across an interesting issue that provides guidance on adding a new definition (interface), but lacks information on implementing code.
Below is my attempt at extending Phaser using TypeScript:
// This section contains phaser typescript definitions
// The code I am trying to incorporate can be found at the end.
// Feel free to try it out on https://www.typescriptlang.org/play/index.html
declare module "phaser-ce" {
export = Phaser;
}
declare class Phaser {
static VERSION: string;
static DEV_VERSION: string;
static GAMES: Phaser.Game[];
}
declare module Phaser {
enum blendModes {
NORMAL,
ADD,
MULTIPLY,
SCREEN,
OVERLAY,
}
// Additional classes like Animation, Signal, and Game are defined here...
class Physics {
constructor(game: Phaser.Game, config?: any);
static ARCADE: number;
static P2JS: number;
static NINJA: number;
static BOX2D: number;
static CHIPMUNK: number;
static MATTERJS: number;
// Various physics implementations such as arcade, ninja, and p2 are included here.
box2d: any; // I aim to customize this, open to suggestions!
}
module Physics {
class Arcade {
}
// Other physics modules like Ninja and P2 are defined similarly...
}
}
// My goal is to introduce a new class in the Physics module...
class MyAwesomeWork {
constructor(greet: string) {
console.log(greet);
}
}
declare module Phaser {
module Physics {
class Box2D {
}
module Box2D {
// ... Unfortunately, this approach did not work :(
class Body {
}
}
}
}
Phaser.Physics.Box2D = MyAwesomeWork;