sliding-window
sliding-window is an unidimensional chunk allocation / free system.
sliding-window
can be used for making your Infinite Scrollers, game level generation by chunk (e.g thing about Flappy Bird), allocation of anything in an infinite (or huge) unidimensional world.
Concept
API
Creates a SlidingWindow:
var slidingWindow = ;
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 move()
.
Your 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 move()
.
options is an object with following optional parameters:
chunkSize
: the size of each chunk in the sliding windowright
: 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:
slidingWindow;slidingWindow;
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;var chunk = slidingWindow; // alternative way
Destroy the SlidingWindow:
slidingWindow;
will call free
to all remaining chunks.
Usages
sliding-window is used by:
- bonhomme game: handling game level generation by chunk. Also used on the server side to handle different "rooms" of players to only broadcast events to people that are visible each other.