I want to make an 2D endless runner, that also allows player to go backwards.
So I have to create random world as the map goes, also keep the old map in case player goes back. I ask about how to update and render this program.
I am thinking having a generate function that takes an x value and generates world from x to (x + chunk size) and when camera reaches x + chunk size, call this function again to generate more map:
function source(x) {
// generate world for x value
};
And maybe use this function like:
let worldThings = [];
// push the world from 0 to chunk size
worldThings.push(...source(0));
// called every frame to update world
function update() {
if (camera.x > chunkSize) {
// generate more world
worldThings.push(...source(chunkSize));
}
}
To render these something like:
// object pooling for rendering things in view
let renderPool = new Pool();
function render() {
// release all objects
renderPool.releaseAll();
// take things that are visible
let renderThings = worldThings.filter(thingsInCameraView);
// for everything allocate a resource in the render pool
renderThings.each(renderPool.allocate);
// render the pool
renderPool.each(renderThing);
}