Load MAP around player.

Started by
10 comments, last by Servant of the Lord 11 years, 2 months ago
Let's separate some mental concepts here:
The map and player data should be separate from the view.
Which means, the collision data of the player should never depend upon the view.

Regardless of how big your screen is, it shouldn't effect the logic of the game data.

Logically speaking, you have four things:
- Your blank background (blackness)
- The map
- The player's avatar
- The camera

This gives you two possibilities of appearance (at least that we care about):
A) The camera is always centered on the player, even if it means showing the black background.
B) The camera is always centered on the player, except when the player gets near the edge of the map, then the camera stays as centered as possible without showing any blackness.

If you want A, and not B, just comment out these lines:
//Keep camera in bounds of the map.
//if(startX < 0) startX = 0;
//if((startX + showWidth) > maxX) startX = (maxX - showWidth);

//if(startY < 0) startX = 0;
//if((startY + showWidth) > maxY) startY = (maxY - showWidth);

To make sure the player doesn't walk outside the map boundries, you have to check that when you receive player input:
on-event:
    If-left-key:
        move-player-left
        If-player-is-outside-map
            keep-player-inside-map-boundries

Maybe to handle that, after getting input each frame, you might call something like this:
player->KeepWithin(map->GetWidth(), map->GetHeight);
 
 
Player::KeepWithin(unsigned int width, unsigned int height)
{
    if(playerX < 0) playerX = 0;
    if((playerX + playerWidth) > width) playerX = (width - playerWidth);
    if(playerY < 0) playerY = 0;
    if((playerX + playerHeight) > height)  playerY = (height - playerHeight);
}

Also, your code has an unrelated mistake here: (unless your map is square)
const int maxX = map->GetHeight(); //<--This should be map->GetWidth()
const int maxY = map->GetHeight();
That's probably what's causing the blackness to be visible in your first screenshot.
Ofcourse, if you commented out the lines mentioned above, then you'll get blackness anyway, anytime the player walks near the edge of the map.

Also, these shouldn't have "magic numbers" typed directly into the code.
const int showWidth = 15;
const int showHeight = 13;
What if you change the screen size later? It should be something like:
const int showWidth = (screen->GetWidth() / TILE_WIDTH) + 1;
const int showHeight = (screen->GetHeight() / TILE_WIDTH) + 1;
That way, if you later change the screen width, this part of the code automatically works. Also, if you let users choose their own screen size, it'll work for most of their screen sizes automatically.
Advertisement

Ah, I see where you're handling the camera. I'd suggest renaming 'offset1' to 'tilesheetOffset', and 'offset2' to 'cameraOffset'.

Try separating out the map drawing information from the camera information, and calculate the entire map's offset outside of the function itself.

Map::OnRender(SDL_Surface* Surf_Display, int drawX, int drawY);
 
map->OnRender(screen, (0 - camera.x), (0 - camera.y));

(Assuming you want to start drawing the map at pixel 0,0 normally)

If you actually have zooming, then inside the map function you'll need to apply your zoom to the 'drawX' and 'drawY' before using those variables to position your tiles.

Then you can go like this:

//Center the camera on the player.
camera.x = player.x + (player.width / 2);
camera.y = player.y + (player.height / 2);
 
//Draw the map.
map->OnRender(screen, (0 - camera.x), (0 - camera.y));

This topic is closed to new replies.

Advertisement