-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`TileMap.getTileAt()` now returns `Tile` object instead of number Moves `TileMap.query` to separate file
- Loading branch information
Showing
6 changed files
with
155 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import labyrinthos from '../lib/labyrinthos.js'; | ||
|
||
let map = new labyrinthos.TileMap({ | ||
width: 10, | ||
height: 10 | ||
}); | ||
|
||
map.fill(1); // fill entire map with 1 | ||
|
||
// Example transformation: Increment each tile's value by 2 | ||
map.transform(function(tile){ | ||
tile.value += 2; | ||
return tile; | ||
}); | ||
|
||
console.log(map.data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export default function getTileAt(x, y, z) { | ||
let tileId; // This will hold the actual value from the data array | ||
|
||
// Retrieve the tile value based on whether the map is 3D or 2D | ||
if (this.is3D) { | ||
if (z !== undefined && this.data[z] !== undefined) { | ||
tileId = this.data[z][y * this.width + x]; | ||
} else { | ||
return undefined; // Return undefined if the z layer does not exist | ||
} | ||
} else { | ||
tileId = this.data[y * this.width + x]; | ||
} | ||
|
||
// Construct the tile object with the id, value, and coordinates | ||
let tileObject = { | ||
// id: tileId, // Tile ID from the tileSet array | ||
id: tileId, // Actual tile ID from the data array | ||
value: tileId, // Actual value from the data array | ||
x: x, | ||
y: y, | ||
...(this.is3D ? { z: z } : {}), // Include z coordinate if the map is 3D | ||
}; | ||
|
||
// If the tileSet is defined and contains an entry for this tile ID, merge its properties into the tile object | ||
if (Array.isArray(this.tileSet) && this.tileSet[tileId]) { | ||
tileObject = { ...tileObject, ...this.tileSet[tileId] }; | ||
} | ||
return tileObject; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import TileMap from '../TileMap.js'; // self reference required for new TileMap instance | ||
|
||
export default function query({ x, y, width, height, z, tile } = {}) { | ||
let results = []; | ||
|
||
if (x !== undefined && y !== undefined && width !== undefined && height !== undefined) { | ||
for (let offsetY = 0; offsetY < height; offsetY++) { | ||
for (let offsetX = 0; offsetX < width; offsetX++) { | ||
const queryX = x + offsetX; | ||
const queryY = y + offsetY; | ||
|
||
if (queryX >= this.width || queryY >= this.height) { | ||
results.push(undefined); // Add undefined for out-of-bounds indices | ||
} else { | ||
const index = queryY * this.width + queryX; // Calculate the correct index in the 1D array | ||
if (this.is3D) { | ||
if (z !== undefined && this.data[z] && this.data[z][index] !== undefined) { | ||
results.push(this.data[z][index]); | ||
} else { | ||
results.push(undefined); | ||
} | ||
} else { | ||
if (this.data[index] !== undefined) { | ||
results.push(this.data[index]); | ||
} else { | ||
results.push(undefined); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// create a new TileMap instance from the results | ||
let subsection = new TileMap({ | ||
width, | ||
height, | ||
is3D: this.is3D | ||
}); | ||
|
||
subsection.data = results; | ||
|
||
return subsection; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export default function transform(transformFunction) { | ||
// Check if the map is 3D | ||
if (this.depth > 1) { | ||
for (let z = 0; z < this.depth; z++) { | ||
for (let y = 0; y < this.height; y++) { | ||
for (let x = 0; x < this.width; x++) { | ||
// Use getTileAt to get the tile object | ||
const tileObject = this.getTileAt(x, y, z); | ||
// Apply the transformation function to the tile object | ||
const transformedTile = transformFunction(tileObject, x, y, z); | ||
// Assuming the transformation function returns a tile object with an updated 'value' | ||
// Update the data array with the new value | ||
this.data[z][y * this.width + x] = transformedTile; | ||
} | ||
} | ||
} | ||
} else { | ||
// Handle 2D map transformation | ||
for (let y = 0; y < this.height; y++) { | ||
for (let x = 0; x < this.width; x++) { | ||
// Use getTileAt to get the tile object for 2D maps (z is undefined) | ||
const tileObject = this.getTileAt(x, y); | ||
// Apply the transformation function to the tile object | ||
const transformedTile = transformFunction(tileObject, x, y); | ||
// Update the data array with the new value | ||
this.data[y * this.width + x] = transformedTile.id; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import tap from 'tape'; | ||
import TileMap from '../lib/TileMap.js'; | ||
|
||
tap.test('applies a transformation function to all tiles', (t) => { | ||
const map = new TileMap({ width: 2, height: 2, tileSet: [{}, { value: 'tile1' }, { value: 'tile2' }] }); | ||
map.fill(1); // Assuming '1' corresponds to a tile in the tileSet | ||
|
||
// Transformation function that increments the id of the tile object | ||
const increment = tile => ({ ...tile, id: tile.id + 1 }); | ||
|
||
map.transform(increment); | ||
|
||
let passed = true; | ||
for (let y = 0; y < map.height; y++) { | ||
for (let x = 0; x < map.width; x++) { | ||
const tile = map.getTileAt(x, y); | ||
// Check if the transformation has been applied correctly | ||
if (typeof tile !== 'object' || tile.id !== 2) { | ||
passed = false; | ||
} | ||
} | ||
} | ||
|
||
t.ok(passed, 'All tiles should have their id incremented by 1'); | ||
t.end(); | ||
}); |