Hi guys, first post here. Ok so im creating a 2d tile based game which uses a camera to slide the map the opposite direction the player is trying to walk in order to keep the character centered in the screen. Sounds simple right? It is, this isnt where im having any issues.
My first question is kind of a general question about how i should impliment maps since mine seems to not be working so well and ive noticed some random pauses in my game (not sure if that has to do with the key input handling though)…
Would it be better to have a map load from a text file or to have a pre defined map within the code. I feel like having the map as a saved text file would be nice because if i feel like making any event within the game that happens (perhaps an explosion in a mountain wall which reveals a cave enterance) i can perminantly change the environment, but having the map defined within the code seems like it would allow the game to run smoother.
Anyways for my question. How i was currently doing my maps was that the map and all the map data was placed in a text file with region ID, X, and Y, and the Tile Type. Then the game would load the camera cells tile by tile, loading just enough of the tiles from the DB to fill the viewable screen, with a little bit of a buffer off to the side in order for tiles sliding on / off the screen.
So the player loads, then the tiles for the active screen loads, i can walk camera slides and the camera slides like it should, now here comes the problem. When the camera slides, one row or column of tiles gets knocked out of the active camera cell array when they get pushed off the side of the screen and all the tiles within the camera cells are supposed to slide over by one, depending on the direction traveled, and then the empty row or column of cells gets a new tile loaded
Heres an example of the player moving up (camera slides down) of me trying to swap the tiles within the camera cells and then loading a new set of tiles after moving, im just showing the cameraUpdate because all the other code works fine and ive narrowed it down to this select piece of code
So the player moves up 1 and the camera gets moved down, activeCells[x][y] is an array of camera cells, camXSize and camYSize are just static ints to show how many cells are in the arrays. 0, 0 in the array is in the top left corner of the screen, x increases every cell to the right, y increases every cell down
public void updateCamera(int dir)
{
//Just showing the example of a player walking up because its the same code for every direction
switch (dir)
{
case 0:
{
for (int x = 0; x < camXSize; x++)
{
// This would be the bottom row of the camera cells, these will be "knocked-off" since there is no cell below it to hold this tile
// Instead, just give it the tile before it.
activeCells[x][camYSize - 1].currentTile = activeCells[x][camYSize - 2].currentTile;
activeCells[x][camYSize - 1].currentTile .cameraX = activeCells[x][camYSize - 1].cellXPos;
activeCells[x][camYSize - 1].currentTile .cameraY = activeCells[x][camYSize - 1].cellYPos;
for (int y = camYSize - 2; y > 0; y--)
{
// Already took care of the bottom row of cells so it starts at the 2nd last row, giving it the previous row
activeCells[x][y].currentTile = activeCells[x][y - 1].currentTile;
activeCells[x][y].currentTile.cameraX = activeCells[x][y].cellXPos;
activeCells[x][y].currentTile.cameraY = activeCells[x][y].cellYPos;
}
// Finally we get to the top row, since there was nothing above this row to have sliden into these cells, we gotta load a new cell instead
activeCells[x][0].currentTile = dbMng.loadTile(activeCells[12][10].currentLiving.regID, activeCells[x][1].currentTile.xLoc, activeCells[x][1].currentTile.yLoc - 1);
activeCells[x][0].currentTile.cameraX = activeCells[x][0].cellXPos;
activeCells[x][0].currentTile.cameraY = activeCells[x][0].cellYPos;
}
break;
}
default:
break;
}
}
What is supposed to happen is player clicks a directional key to move -> camera slides + game animates -> after the last step of the animation / camera slide the camera updates which tiles are in which cells, then the game checks for any events at that coordinate… what actually happens is because of the camera update tiles start disappearing and occasionally some of the default grey tiles i made appear in random areas… not quite sure what i did wrong in this code. any info will help. if i need to post more of my code let me know
edit: Well i scrapped trying to switch the cells based off of the cells x and y coordinates and updated the new tiles based on the character position
public void updateCamera(int dir) throws IOException
{
int xMin = activeCells[12][10].currentLiving.xLoc - 12,
xMax = activeCells[12][10].currentLiving.xLoc + 12,
yMax = activeCells[12][10].currentLiving.yLoc + 10,
yMin = activeCells[12][10].currentLiving.yLoc - 10;
//Just showing the example of a player walking up because its the same code for every direction
switch (dir)
{
case 0:
{
for (int y = camYSize - 1; y >= 0; y--)
{
for (int x = 0; x < camXSize; x++)
{
if (y == 0)
{
activeCells[x][y].currentTile = testGame.dbMng.loadTile(activeCells[12][10].currentLiving.regID, (xMin + x), yMin);
}
else
{
activeCells[x][y].currentTile = activeCells[x][y - 1].currentTile;
}
activeCells[x][y].currentTile.cameraX = activeCells[x][y].cellXPos;
activeCells[x][y].currentTile.cameraY = activeCells[x][y].cellYPos;
}
}
break;
}
case 1:
{
for (int x = 0; x < camXSize; x++)
{
for (int y = 0; y < camYSize; y++)
{
if (x == camXSize - 1)
{
activeCells[x][y].currentTile = testGame.dbMng.loadTile(activeCells[12][10].currentLiving.regID, xMax, (yMin + y));
}
else
{
activeCells[x][y].currentTile = activeCells[x + 1][y].currentTile;
}
activeCells[x][y].currentTile.cameraX = activeCells[x][y].cellXPos;
activeCells[x][y].currentTile.cameraY = activeCells[x][y].cellYPos;
}
}
break;
}
case 2:
{
for (int y = 0; y < camYSize; y++)
{
for (int x = 0; x < camXSize; x++)
{
if (y == camYSize - 1)
{
activeCells[x][y].currentTile = testGame.dbMng.loadTile(activeCells[12][10].currentLiving.regID, (xMin + x), yMax);
}
else
{
activeCells[x][y].currentTile = activeCells[x][y + 1].currentTile;
}
activeCells[x][y].currentTile.cameraX = activeCells[x][y].cellXPos;
activeCells[x][y].currentTile.cameraY = activeCells[x][y].cellYPos;
}
}
break;
}
case 3:
{
for (int x = camXSize - 1; x >= 0; x--)
{
for (int y = 0; y < camYSize; y++)
{
if (x == 0)
{
activeCells[x][y].currentTile = testGame.dbMng.loadTile(activeCells[12][10].currentLiving.regID, xMin, (yMin + y));
}
else
{
activeCells[x][y].currentTile = activeCells[x - 1][y].currentTile;
}
activeCells[x][y].currentTile.cameraX = activeCells[x][y].cellXPos;
activeCells[x][y].currentTile.cameraY = activeCells[x][y].cellYPos;
}
}
break;
}
default:
break;
}
}
Still cant figure out quite why the other method didnt work but everything is running smoothly now