• Create Account

### #ActualServant of the Lord

Posted 10 February 2013 - 11:20 PM

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:
- 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.

### #2Servant of the Lord

Posted 10 February 2013 - 11:16 PM

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:
- 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.

### #1Servant of the Lord

Posted 10 February 2013 - 11:11 PM

Let's separate some concepts here in your mind:
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:
- The map
- The player's avatar
- The camera

This gives you two possibilities of appearance:
A) The camera is always centered on the player, even if it means showing the black background.
B) The black background is never shown, the camera never leaves the boundary of the map, and the character is always centered in the camera except when the player gets close to the edge of the map.

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 above, then you'll get blackness anyway, anytime the player walks near the edge of the map.

PARTNERS