0
\$\begingroup\$

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);
   }
\$\endgroup\$
  • \$\begingroup\$ See Sidescrolling terrain 1D perlin noise \$\endgroup\$ – Theraot Oct 2 at 17:35
  • \$\begingroup\$ Sounds like you have a workable plan! How have you tried implementing this plan so far, and where have you gotten stuck or encountered behaviour that you want to improve upon? \$\endgroup\$ – DMGregory Oct 2 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.