I am making an endless runner using C++ and raylib. I am trying to spawn obstacles off screen and move them towards the player to jump or slide under. I am doing this by using a list of structs and populating it with this function:
std::list<AnimData> barrierList{};
std::list<Rectangle> barrierCollisionRecs{};
std::list<HighBarrier> highBarrierList{};
void SetUpLapObstacles(Texture2D barrierTexture, Texture2D frontHighBarrier, Texture2D backHighBarrier, int windowDimensions[])
{
printf("Setting up obstacles!\n");
barrierList.clear();
barrierCollisionRecs.clear();
highBarrierList.clear();
int numberOfObstacles = 15;
int currentDistance = 0;
for(int i = 0; i < numberOfObstacles; i++)
{
int random = GetRandomValue(0, 1);
if(random == 0)
{
AnimData newBarrier{{0.0f, 0.0f, barrierTexture.width / 2, barrierTexture.height},
{windowDimensions[0] + currentDistance, GROUND_LEVEL + 125}, 0, (1.0 / 2.0), 0.0f};
int padding = 17;
Rectangle newBarrierRec{newBarrier.pos.x + padding, newBarrier.pos.y + padding, barrierTexture.width / 2 - 20,
barrierTexture.height};
barrierList.push_back(newBarrier);
barrierCollisionRecs.push_back(newBarrierRec);
}
else
{
HighBarrier newHighBarrier;
newHighBarrier.frontAnim.rec.x = 0.0;
newHighBarrier.frontAnim.rec.y = 0.0;
newHighBarrier.frontAnim.rec.width = (float)frontHighBarrier.width / 2.0f;
newHighBarrier.frontAnim.rec.height = (float)frontHighBarrier.height;
newHighBarrier.frontAnim.pos.x = (float)windowDimensions[0] + (float)currentDistance;
newHighBarrier.frontAnim.pos.y = GROUND_LEVEL + 5;
newHighBarrier.frontAnim.frame = 0;
newHighBarrier.frontAnim.updateTime = (1.0 / 2.0);
newHighBarrier.frontAnim.runningTime = 0.0;
newHighBarrier.backAnim.rec.x = 0.0;
newHighBarrier.backAnim.rec.y = 0.0;
newHighBarrier.backAnim.rec.width = backHighBarrier.width / 2;
newHighBarrier.backAnim.rec.height = backHighBarrier.height;
newHighBarrier.backAnim.pos.x = newHighBarrier.frontAnim.pos.x + newHighBarrier.frontAnim.rec.width;
newHighBarrier.backAnim.pos.y = GROUND_LEVEL + 5;
newHighBarrier.backAnim.frame = 0;
newHighBarrier.backAnim.updateTime = (1.0 / 2.0);
newHighBarrier.backAnim.runningTime = 0.0;
newHighBarrier.collisionRec.x = newHighBarrier.frontAnim.pos.x + HIGH_BARRIER_PADDING.x;
newHighBarrier.collisionRec.y = newHighBarrier.frontAnim.pos.y - HIGH_BARRIER_PADDING.y;
newHighBarrier.collisionRec.width = 74.0f;
newHighBarrier.collisionRec.height = 110.0f;
highBarrierList.push_back(newHighBarrier);
}
currentDistance += distanceBetweenBarriers;
}
}
I then update the position of each element in barrierList, barrierCollisionRecs, and highBarrierList. The logic for all three is very similar, so I'll simply use barrierList and barrierCollider as an example, though all have the same issue.
//set barrier velocity
barrierVel = -FG_SCROLL_SPEED;
//barriers are not moving
int i = 0;
for(AnimData barrier : barrierList)
{
barrier.pos.x += barrierVel * DELTA_TIME;
if(i == 0)
{
printf("Barrier 1 X Pos: %f\n", barrier.pos.x);
}
i++;
}
for(Rectangle barrierCollider : barrierCollisionRecs)
{
barrierCollider.x += barrierVel * DELTA_TIME;
}
I have looked all over the rest of my code to see if at any point the position is set to something it shouldn't be, locking it in place. After printing the positions to the console for all three lists (barrier list is the only printf I kept for now), the barrier positions are locked in the place they spawned, and do not move at all while the game is running, meaning they spawn offscreen, and stay offscreen, obviously making for some very boring gameplay. Is there any reason this is happening? How can I fix this?