fix: tileMove-based functions on a level tilemap#1078
Conversation
| /** | ||
| * Enable spatial map after adding level tiles | ||
| */ | ||
| spatialMap?: boolean; | ||
| /** | ||
| * Enable cost, connectivity, and edge maps after adding level tiles | ||
| */ | ||
| tilePathMaps?: boolean; |
There was a problem hiding this comment.
| /** | |
| * Enable spatial map after adding level tiles | |
| */ | |
| spatialMap?: boolean; | |
| /** | |
| * Enable cost, connectivity, and edge maps after adding level tiles | |
| */ | |
| tilePathMaps?: boolean; |
|
|
||
| if (opt.spatialMap) { | ||
| createSpatialMap(this); | ||
| } | ||
|
|
||
| if (opt.tilePathMaps) { | ||
| createCostMap(this); | ||
| createEdgeMap(this); | ||
| createConnectivityMap(this); | ||
| } |
There was a problem hiding this comment.
| if (opt.spatialMap) { | |
| createSpatialMap(this); | |
| } | |
| if (opt.tilePathMaps) { | |
| createCostMap(this); | |
| createEdgeMap(this); | |
| createConnectivityMap(this); | |
| } |
| const ensureSpatialMapExists = () => { | ||
| if (!spatialMap) { | ||
| throw new Error( | ||
| "Spatial map is not initialized. Call level.getSpatialMap() before using spatial map functions, or enable the 'spatialMap' option in level().", | ||
| ); | ||
| } | ||
| }; | ||
|
|
There was a problem hiding this comment.
| const ensureSpatialMapExists = () => { | |
| if (!spatialMap) { | |
| throw new Error( | |
| "Spatial map is not initialized. Call level.getSpatialMap() before using spatial map functions, or enable the 'spatialMap' option in level().", | |
| ); | |
| } | |
| }; | |
| const ensureSpatialMapExists = () => { | |
| if (!spatialMap) { | |
| createSpatialMap(this); | |
| } | |
| }; | |
If the spatial map doesn't exist yet ...why not just create it now?
There was a problem hiding this comment.
The this inside an arrow function is lexically bound, so it won't refer to the level instance here.
The throw approach sidesteps the problem by making it the caller's responsibility to initialize the map first.
I also mused on
- Simply creating the spatial map when the component is attached via add()
- Making the tile.tilemove functions throw errors if no spatial map is found in their responsible level
- Creating a spatial map whenever level.spawn() is called.
What do you think the best approach is? To implement the above suggestion we'd need to change the props of insertIntoSpatialMap and removeFromSpatialMap which may break other usages of the functions?
There was a problem hiding this comment.
shoot, forgot about that part, that's what I get for not checking this out locally.
to your muses
- not necessary for levels that don't use it
- please no errors when it could just be created
- not necessary for levels that don't use it
I mostly just wanted to reduce the number of edits, so just every place that ensureSpatialMapExists() is called, just replace it with if (!spatialMap) createSpatialMap(this) or if (!spatialMap) createSpatialMap(obj), you might have to make wrappers for insertIntoSpacialMap and removeFromSpatialMap, instead of just using the functions directly, make them wrappers to get the this -
- insertIntoSpatialMap,
+ insertIntoSpatialMap(obj) { insertIntoSpatialMap(obj, this); }|
figuring out fmt |
Closes #903
Added options to create spatial map and tilepath-based maps to levelcompopt, as well as more descriptive errors in spatialmap functions. Users should be able to more easily understand and proactively handle tilemove and spatialmap based issus