Day 6 of the Level Editor project
I decided that I would make the level scrollable today, so that tiles could be placed outside the first screen.
In game.h, we define two new variables
unsigned int currentPosX;
unsigned int currentPosY;
which we set to zero in the Initialize method. These variables will store the current position in the world, where x,y = 0,0 is the bottom-left corner.
We need to consider this position whenever we work with coordinates of tiles. So in the Update method, we have to write:
case Solid:
newElement->coordX0 += currentPosX;
newElement->coordX1 += currentPosX;
newElement->coordY0 += currentPosY;
newElement->coordY1 += currentPosY;
level.push_back(newElement);
break;
and evaluate the keypresses:
if (g_keys->keyDown[VK_UP])
{
currentPosY++;
}
if (g_keys->keyDown[VK_DOWN])
{
if (currentPosY > 0) currentPosY--;
}
if (g_keys->keyDown[VK_LEFT])
{
if (currentPosX > 0) currentPosX--;
}
if (g_keys->keyDown[VK_RIGHT])
{
currentPosX++;
}
For the Draw method, we define two auxiliary methods to compute the correct coordinates:
unsigned int Game::TransformX(unsigned int x0)
{
GlobalStatics globalStatics;
if (x0 < currentPosX)
{
x0 = 0;
}
else
{
x0 -= currentPosX;
if (x0 > globalStatics.worldX) x0 = globalStatics.worldX;
}
return x0;
}
unsigned int Game::TransformY(unsigned int y0)
{
GlobalStatics globalStatics;
if (y0 < currentPosY)
{
y0 = 0;
}
else
{
y0 -= currentPosY;
if (y0 > globalStatics.worldY) y0 = globalStatics.worldY;
}
return y0;
}
Then we write in the Draw method:
case Solid:
glColor3f(1.0f, 0.8f, 0.2f);
glBegin(GL_QUADS);
glVertex2f(TransformX(i->coordX0), TransformY(i->coordY0));
glVertex2f(TransformX(i->coordX1), TransformY(i->coordY0));
glVertex2f(TransformX(i->coordX1), TransformY(i->coordY1));
glVertex2f(TransformX(i->coordX0), TransformY(i->coordY1));
glEnd();
These transform methods help us clip the tiles.
In the SaveLevel method we also have to compute the size of the level based on the coordinates of the tiles:
for (auto const& i : level)
{
levelSizeX = max(levelSizeX, i->coordX1);
levelSizeY = max(levelSizeY, i->coordY1);
}
That should do it.
Kommentare
Kommentar veröffentlichen