sliding-window is an unidimensional chunk allocation / free system.
sliding-windowcan be used for making your Infinite Scrollers, game level generation by chunk (e.g Flappy Bird like), allocation of anything in an infinite (or huge) unidimensional world.
Creates a SlidingWindow:
var slidingWindow = SlidingWindow(alloc, free, options);
alloc is a function to alloc something. It receives 2 parameters: the first parameter is the
index of the chunk to alloc. The second parameter is an optional parameter that can be transmit at second parameter of
alloc function should returns a value that can be useful representing what you have allocated in that chunk.
free is a function to free something allocated with
alloc. It receives 3 parameters:
The first parameter is the
index. The second parameter is the chunk which was the returned value of
alloc (it can be useful to free the thing!). The third parameter is also the optional parameter that can be transmit at second parameter of
options is an object with following optional parameters:
chunkSize: the size of each chunk in the sliding window
right: the number of chunks to keep ahead to the right of the window (meaning in higher position values)
left: the numbers of chunks to keep ahead to the left of the window (meaning in lower position values)
bounds: the allowed bounds of the SlidingWindow – format example: [0, +Infinity], [-Infinity, 0], [-Infinity, +Infinity], [0, 100]
Move the window:
Usually you can call
.move() as soon as possible: for each tick of an update loop or for each scroll event.
Get a Chunk:
var chunk = slidingWindow.getChunk(position); var chunk = slidingWindow.getChunkByIndex(index); // alternative way
Destroy the SlidingWindow:
free to all remaining chunks.
sliding-window is used by: