Skip to content

Commit

Permalink
Refactor pan move
Browse files Browse the repository at this point in the history
- Remove confusing actual_pan_ member
- Fix bug where teleporting to same map at edge of map would causing
panning problems.
  • Loading branch information
fmatthew5876 committed Dec 6, 2018
1 parent c1ea398 commit 0f30b42
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
30 changes: 12 additions & 18 deletions src/game_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,17 +317,17 @@ void Game_Player::Center() {
Game_Map::AddScreenX(x, dist_to_screen_left);
Game_Map::AddScreenY(y, dist_to_screen_top);

actual_pan_x = Game_Map::GetPanX();
actual_pan_y = Game_Map::GetPanY();
last_pan_x = Game_Map::GetPanX();
last_pan_y = Game_Map::GetPanY();

auto actual_pan_x = last_pan_x;
auto actual_pan_y = last_pan_y;

Game_Map::AddScreenX(x, actual_pan_x);
Game_Map::AddScreenY(y, actual_pan_y);

Game_Map::SetPositionX(x);
Game_Map::SetPositionY(y);

last_pan_x = actual_pan_x;
last_pan_y = actual_pan_y;
}

void Game_Player::MoveTo(int x, int y) {
Expand All @@ -342,8 +342,8 @@ void Game_Player::MoveTo(int x, int y) {
void Game_Player::UpdateScroll() {
// First, update for the player's movement...

int center_x = DisplayUi->GetWidth() / 2 - TILE_SIZE / 2 - actual_pan_x / (SCREEN_TILE_WIDTH / TILE_SIZE);
int center_y = DisplayUi->GetHeight() / 2 + TILE_SIZE / 2 - actual_pan_y / (SCREEN_TILE_WIDTH / TILE_SIZE);
int center_x = DisplayUi->GetWidth() / 2 - TILE_SIZE / 2 - last_pan_x / (SCREEN_TILE_WIDTH / TILE_SIZE);
int center_y = DisplayUi->GetHeight() / 2 + TILE_SIZE / 2 - last_pan_y / (SCREEN_TILE_WIDTH / TILE_SIZE);

int dx = 0;
int dy = 0;
Expand Down Expand Up @@ -386,24 +386,18 @@ void Game_Player::UpdateScroll() {
int pan_y = Game_Map::GetPanY();
int pan_dx = pan_x - last_pan_x;
int pan_dy = pan_y - last_pan_y;
last_pan_x = pan_x;
last_pan_y = pan_y;

// Change pan_dx/pan_dy to account for hitting the edges
int screen_x = Game_Map::GetPositionX();
int screen_y = Game_Map::GetPositionY();
Game_Map::AddScreenX(screen_x, pan_dx);
Game_Map::AddScreenY(screen_y, pan_dy);

// Only move for the pan if we're closer to the target pan than we were before.
if (std::abs(actual_pan_x + pan_dx - Game_Map::GetTargetPanX()) < std::abs(actual_pan_x - Game_Map::GetTargetPanX())) {
Game_Map::ScrollRight(pan_dx, true);
actual_pan_x += pan_dx;
}
if (std::abs(actual_pan_y + pan_dy - Game_Map::GetTargetPanY()) < std::abs(actual_pan_y - Game_Map::GetTargetPanY())) {
Game_Map::ScrollDown(pan_dy, true);
actual_pan_y += pan_dy;
}
Game_Map::ScrollRight(pan_dx, true);
Game_Map::ScrollDown(pan_dy, true);

last_pan_x = pan_x;
last_pan_y = pan_y;
}

void Game_Player::Update() {
Expand Down
3 changes: 0 additions & 3 deletions src/game_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,6 @@ class Game_Player : public Game_Character {

int last_pan_x = 0, last_pan_y = 0;
int last_remaining_move = 0, last_remaining_jump = 0;
// These track how much of the pan has actually occurred, which
// may be less than the pan values if the pan went off the map.
int actual_pan_x = 0, actual_pan_y = 0;

RPG::Music walking_bgm;

Expand Down

0 comments on commit 0f30b42

Please sign in to comment.