HDB: Add the centerMapXY to center the map

This commit is contained in:
Nipun Garg 2019-06-18 05:47:12 +05:30 committed by Eugene Sandulenko
parent 2eacf30d82
commit c661f8663a
2 changed files with 64 additions and 0 deletions

View File

@ -264,4 +264,67 @@ void Map::setMapXY(int x, int y) {
_mapY = y;
}
// Sets _mapX and _mapY and tries to center the map around X, Y
void Map::centerMapXY(int x, int y) {
int checkx = x / kTileWidth;
int checky = y / kTileHeight;
int minx, miny, maxx, maxy;
// Scan from centerX to right edge
maxx = (_width - (16/2)) * kTileWidth;
for (int i = checkx + 1; i <= checkx + (16 / 2); i++) {
if (!getMapBGTileIndex(i, checky)) {
maxx = (i - (16 / 2)) * kTileWidth;
break;
}
}
// Scan from centerX to left edge
minx = 0;
for (int i = checkx - 1; i >= checkx - (16 / 2); i--) {
if (!getMapBGTileIndex(i, checky)) {
// +1 because we don't want to see one whole tile
minx = (1 + i + (16 / 2)) * kTileWidth;
break;
}
}
// Scan from centerY to bottom edge
maxy = (_height - (16/2)) * kTileHeight;
for (int i = checky + 1; i <= checky + (16 / 2); i++) {
if (!getMapBGTileIndex(checkx, i)) {
maxy = (i - (16 / 2)) * kTileHeight;
break;
}
}
// Scan from centerY to top edge
miny = 0;
for (int i = checky - 1; i >= checkx - (16 / 2); i--) {
if (!getMapBGTileIndex(checkx, i)) {
// +! because we don't want to see one whole tile
miny = (1 + i + (16 / 2)) * kTileHeight;
break;
}
}
if (x < minx) {
x = minx;
} else if (x > maxx) {
x = maxx;
}
if (y < miny) {
y = miny;
} else if (y > maxy) {
y = maxy;
}
x -= (480 / 2);
y -= (480 / 2);
setMapXY(x, y);
}
}

View File

@ -62,6 +62,7 @@ public:
uint16 getMapFGTileIndex(int x, int y);
void getMapXY(int *x, int *y);
void setMapXY(int x, int y);
void centerMapXY(int x, int y);
int _mapX, _mapY; // Coordinates of Map
int _mapTileX, _mapTileY; // Tile Coordinates of Map