/* START OF COMPILED CODE */
export default class PolygonArea extends Phaser.GameObjects.Polygon {
constructor(scene: Phaser.Scene, x?: number, y?: number, points?: string) {
super(scene, x ?? 690, y ?? 430, points ?? "35 100 0 50 70 0 140 50 105 100");
this.setOrigin(0, 0);
this.alpha = 0.5;
this.isFilled = true;
/* START-USER-CTR-CODE */
this.visible = false;
/* END-USER-CTR-CODE */
}
/* START-USER-CODE */
public hasPoint(x: number, y: number) {
return Phaser.Geom.Polygon.Contains(this.geom, x - this.x, y - this.y);
}
/* END-USER-CODE */
}
/* END OF COMPILED CODE */
Then, I created multiple prefabs that extend this one, namely:
import type RoomScene from '@game/scenes/rooms/RoomScene';
import PolygonArea from '@game/scenes/shared_prefabs/base/PolygonArea';
/* START OF COMPILED CODE */
export default class NavArea extends PolygonArea {
constructor(scene: Phaser.Scene, x?: number, y?: number, points?: string) {
super(scene, x ?? 690, y ?? 430);
this.fillColor = 65280;
/* START-USER-CTR-CODE */
(this.scene as RoomScene).navAreas.push(this);
/* END-USER-CTR-CODE */
}
/* START-USER-CODE */
/* END-USER-CODE */
}
/* END OF COMPILED CODE */
Notice how the points parameter of NavArea 's constructor is left unused. Phaser Editor is not passing it to super, so whatever custom shape I define for a NavArea in the editor, it won't be passed down to the PolygonArea and the polygon will stay with its default points.
Sure, I can bypass this by adding
this.setTo(points)
to the constructor of every class that extends PolygonArea, but that kind of beats the purpose of having a base class. I could just pass it to the constructor if the editor let me...
Later, I found out this was because I wasn't unlocking the points property in child classes. But then Arian stepped in, and said that it should pass down the property anyway, so another nested prefab can unlock it.
Version
Description
I created this prefab:
Then, I created multiple prefabs that extend this one, namely:
Notice how the
points
parameter ofNavArea
's constructor is left unused. Phaser Editor is not passing it tosuper
, so whatever custom shape I define for aNavArea
in the editor, it won't be passed down to thePolygonArea
and the polygon will stay with its default points.Sure, I can bypass this by adding
to the constructor of every class that extends
PolygonArea
, but that kind of beats the purpose of having a base class. I could just pass it to the constructor if the editor let me...Later, I found out this was because I wasn't unlocking the points property in child classes. But then Arian stepped in, and said that it should pass down the property anyway, so another nested prefab can unlock it.
Full Discord thread here