I'm programming a 2D side scrolling game. I currently have a finite world, made up of blocks stored in an array. And a song table containing a list of entities for that song. For now, I only had limits attached to the camera that prevent the camera from overwriting outside the limits of the world, so a normal finished world rendering. But I would like it to be infinite, given the blocks that work rather well. I divide the camera limits by the size of a block and I have the array clues that I need for rendering.
For simplicity, I just post an example with the block centered at the camera position (and also only with the x Coord since y will be the same each time):
blockX = camera.position.x / BLOCK_SIZE;
blockX = (world.width + blockX) % world.width;
The seconds step ensures that I always keep the value within the limits of the world.
So if the camera goes into the negative, I will teleport it to the other side of the world at some point. But until then, the following events happen:
Consider a world width of 100 (world with = number of blocks; the world table goes from 0 to 99 (inclusive)) and a block of 10:
blockX = -10 / 10; => blockX = -1;
blockX = (100 + (-1)) % 100; => blockX = 99;
int blockID = world.blocks(blockX);
renderBlock(blockID, blockX * BLOCK_SIZE)
I receive the information from the block of block -1 which is block 99, because the world should be infinite, but I return it to -10 because the camera is not currently positioned at the other end of the world .
For the pieces, I have a similar system, I make the pieces essentially the same as the blocks. So, for block x = -1, I make block 9 (block size = 10 blocks). But if, for example, I return the entities in block 9 while the camera is still at x = -5, all the entities are returned to their normal world position at around 90 to 100. But they should be returned at -10 to 0. My question now is, how would you properly relate the current position of the songs to the position of the rendered entity?