wiserim / phaser-raycaster

Raycasting plugin for Phaser 3. Documentation:

Home Page:https://wiserim.github.io/phaser-raycaster/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Config should check if object type is supported

robbeman opened this issue · comments

When adding a container it just iterates over all children and it tries to create a raycaster map for each of these.

container.iterate(function(child){

In essence this is okay, but when - for example - adding a graphics object to a container the config method falls through to the default case. When this object is missing some required methods this will crash the game in several places.

  • in rectangle.updateMap which could be skipped by disabling the child map right after creating it, but this is currently not easy to do.
  • in container.updateMap which can not be skipped by disabling the object's raycastermap.
  • Maybe other places.

So it would be interesting to check for existence of required methods before calling them.

function isObjectSupportedByRaycaster(
  object: Phaser.GameObjects.GameObject | Phaser.GameObjects.Components.GetBounds,
): object is Phaser.GameObjects.Components.GetBounds {
  // add whatever methods are required for raycaster map to work.
  return 'getBounds' in object;
}

or in js

function isObjectSupportedByRaycaster(object) {
  // add whatever methods are required for raycaster map to work.
  return typeof object.getBounds === 'function';
}

Sidenote: my current workaround is to add the graphics to the scene, with a reference to it in my Actor and drawing everything offsetted to the position of the container. (But this makes z-sorting and transforms etc. a little more difficult to handle.)

I've published version 0.10.4.
Now Raycaster.mapGameObjects method and Map.updateMap method for container objects will now check if object is supported (if it's a Matter body or contains getBounds method).