Skip to content

Instantly share code, notes, and snippets.

@blackandred
Created March 31, 2018 19:55
Show Gist options
  • Select an option

  • Save blackandred/c62f382660907afcb7f5eba1125dcad3 to your computer and use it in GitHub Desktop.

Select an option

Save blackandred/c62f382660907afcb7f5eba1125dcad3 to your computer and use it in GitHub Desktop.
Manages tiles with support for double-sized tiles
class TilesViewManager {
/**
* @param {array} tiles
* @param {number} maxTiles
* @param {number} maxInRow
* @param {number} maxDoubleElements
*/
constructor (tiles, maxTiles, maxInRow, maxDoubleElements) {
this.tiles = tiles
this.maxTiles = maxTiles
this.maxInRow = maxInRow
this.maxDoubleElements = maxDoubleElements
}
processTiles () {
let inRow = 0
let doubleSizeElements = 0
window.console.info('[Tiles] processTiles()')
for (let tile in this.tiles) {
inRow++
if (this.tiles[tile].highlighted === true) {
if (doubleSizeElements >= this.maxDoubleElements) {
this.tiles[tile].highlighted = false
continue
}
inRow++
}
if (inRow >= this.maxInRow) {
if (this.tiles[tile].highlighted === true) {
let position = this.findFreeTileToExchangePlace(doubleSizeElements)
// case: there is no free space anymore, so we need to limit the amount of highlighted things
if (position === null) {
this.tiles[tile].highlighted = false
inRow--
window.console.info('[Tile] No free position to move ' + tile + ', making the tile smaller')
// case: switch the positions
} else if (position) {
window.console.info('[Tile] Moving "' + this.tiles[tile].title + '" from ' + (parseInt(tile) + 1) + ' to ' + (parseInt(position) + 1) + ' "' + this.tiles[position].title + '" / inRow=' + inRow + ', maxInRow: ' + this.maxInRow)
let copy = this.tiles[position]
this.tiles[position] = this.tiles[tile]
this.tiles[tile] = copy
doubleSizeElements++
}
}
inRow = 0
}
}
window.console.info('[Tile] After processTiles', this.tiles)
// if some elements has changed the size and are now double-sized
// then we need to free the space (remove last elements)
this.removeLastTiles()
window.console.info('[Tile] doubleSizeElements', doubleSizeElements)
return this.tiles
}
/**
* Finds a free array position in the tiles array
* (a tile that is not at the right edge of a row and is not highlighted)
*
* @param {number} existingDoubleSizeElements
* @returns {null|number}
*/
findFreeTileToExchangePlace (existingDoubleSizeElements) {
let inRow = 0
let maxDoubleSized = (this.maxTiles / this.maxInRow) - 1
if (maxDoubleSized < 1) {
maxDoubleSized = 1
}
if (existingDoubleSizeElements >= maxDoubleSized) {
window.console.info('[Tile][Freespace] Maximum reached, doubleSized=' + existingDoubleSizeElements)
return null
}
for (let tile in this.tiles) {
inRow++
// highlighted tile is doublesized
if (this.tiles[tile].highlighted === true) {
window.console.info('[Tile][FreeSpace] ' + tile + ' is double-sized')
inRow++
}
if (inRow < this.maxInRow && this.tiles[tile].highlighted !== true) {
return tile
}
if (inRow >= this.maxInRow) {
inRow = 0
}
}
return null
}
/**
* Remove last X elements
*
* @returns {array}
*/
removeLastTiles () {
let length = 0
window.console.info('[Tiles] removeLastTiles(max=' + this.maxTiles + ', current=' + this.tiles.length + ')')
for (let tile in this.tiles) {
length++
// double sized tile
if (this.tiles[tile].highlighted) {
length++
}
if (length > this.maxTiles) {
length = length - (this.tiles[tile].highlighted ? 2 : 1)
delete this.tiles[tile]
}
}
return this.tiles
}
}
export default TilesViewManager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment