-
-
Notifications
You must be signed in to change notification settings - Fork 112
fix: tileMove-based functions on a level tilemap #1078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -111,6 +111,14 @@ export interface LevelCompOpt { | |||||||||||||||||||||||||||
| tiles: { | ||||||||||||||||||||||||||||
| [sym: string]: (pos: Vec2) => CompList<Comp>; | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * Enable spatial map after adding level tiles | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| spatialMap?: boolean; | ||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * Enable cost, connectivity, and edge maps after adding level tiles | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| tilePathMaps?: boolean; | ||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * Called when encountered a symbol not defined in "tiles". | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
|
|
@@ -149,7 +157,16 @@ export function level(map: string[], opt: LevelCompOpt): LevelComp { | |||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| 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().", | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
If the spatial map doesn't exist yet ...why not just create it now?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The this inside an arrow function is lexically bound, so it won't refer to the level instance here. I also mused on
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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shoot, forgot about that part, that's what I get for not checking this out locally. to your muses
I mostly just wanted to reduce the number of edits, so just every place that - insertIntoSpatialMap,
+ insertIntoSpatialMap(obj) { insertIntoSpatialMap(obj, this); } |
||||||||||||||||||||||||||||
| const insertIntoSpatialMap = (obj: GameObj) => { | ||||||||||||||||||||||||||||
| ensureSpatialMapExists(); | ||||||||||||||||||||||||||||
| const i = tile2Hash(obj.tilePos); | ||||||||||||||||||||||||||||
| if (spatialMap![i]) { | ||||||||||||||||||||||||||||
| spatialMap![i].push(obj); | ||||||||||||||||||||||||||||
|
|
@@ -160,6 +177,7 @@ export function level(map: string[], opt: LevelCompOpt): LevelComp { | |||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const removeFromSpatialMap = (obj: GameObj) => { | ||||||||||||||||||||||||||||
| ensureSpatialMapExists(); | ||||||||||||||||||||||||||||
| const i = tile2Hash(obj.tilePos); | ||||||||||||||||||||||||||||
| if (spatialMap![i]) { | ||||||||||||||||||||||||||||
| const index = spatialMap![i].indexOf(obj); | ||||||||||||||||||||||||||||
|
|
@@ -351,6 +369,16 @@ export function level(map: string[], opt: LevelCompOpt): LevelComp { | |||||||||||||||||||||||||||
| this.spawn(key, vec2(j, i)); | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (opt.spatialMap) { | ||||||||||||||||||||||||||||
| createSpatialMap(this); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (opt.tilePathMaps) { | ||||||||||||||||||||||||||||
| createCostMap(this); | ||||||||||||||||||||||||||||
| createEdgeMap(this); | ||||||||||||||||||||||||||||
| createConnectivityMap(this); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| tileWidth() { | ||||||||||||||||||||||||||||
|
|
@@ -737,9 +765,9 @@ export function levelFactory(data: any) { | |||||||||||||||||||||||||||
| const d = data.tiles[key]; | ||||||||||||||||||||||||||||
| const tags = d.tags; | ||||||||||||||||||||||||||||
| opt.tiles[key] = (pos: Vec2) => { | ||||||||||||||||||||||||||||
| const comps: Comp[] = Object.keys(d).filter(k => k != "tags").map( | ||||||||||||||||||||||||||||
| id => deserializeComp(id, d[id]), | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| const comps: Comp[] = Object.keys(d) | ||||||||||||||||||||||||||||
| .filter((k) => k != "tags") | ||||||||||||||||||||||||||||
| .map((id) => deserializeComp(id, d[id])); | ||||||||||||||||||||||||||||
|
SobennaStory marked this conversation as resolved.
|
||||||||||||||||||||||||||||
| return [...comps, ...tags]; | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.