Skip to content

Commit

Permalink
Finishing Noah player interaction with scrolling maps (closes #4).
Browse files Browse the repository at this point in the history
  • Loading branch information
jpike committed Jul 12, 2014
1 parent 89db101 commit 8d03147
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 10 deletions.
23 changes: 23 additions & 0 deletions noah_ark/library/noah_ark_library/src/Graphics/AnimatedSprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,35 @@ void AnimatedSprite::SetZPosition(const float zPosition)
m_zPosition = zPosition;
}

MATH::Vector2f AnimatedSprite::GetWorldPosition() const
{
return m_worldPositionInPixels;
}

void AnimatedSprite::SetWorldPosition(const float xPositionInPixels, const float yPositionInPixels)
{
m_worldPositionInPixels.X = xPositionInPixels;
m_worldPositionInPixels.Y = yPositionInPixels;
}

hgeRect AnimatedSprite::GetWorldBoundingBox()
{
// SET A DEFAULT BOUNDING BOX.
// This default box should be harmless in the event
// that no animation sequence is currently being used.
hgeRect boundingBox(0.0f, 0.0f, 0.0f, 0.0f);

// GET THE CURRENT ANIMATION SEQUENCE.
std::shared_ptr<AnimationSequence> currentAnimationSequence = GetCurrentAnimationSequence();
bool currentAnimationExists = (nullptr != currentAnimationSequence);
if (currentAnimationExists)
{
boundingBox = currentAnimationSequence->GetWorldBoundingBox(m_worldPositionInPixels.X, m_worldPositionInPixels.Y);
}

return boundingBox;
}

void AnimatedSprite::MoveUp(const float distanceToMoveInPixels)
{
m_worldPositionInPixels.Y -= distanceToMoveInPixels;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@ namespace GRAPHICS
/// @copydoc IGraphicsComponent::SetZPosition(const float zPosition)
virtual void SetZPosition(const float zPosition);

/// @brief Gets the world position of the sprite.
/// @return The world position of the sprite, in pixels.
MATH::Vector2f GetWorldPosition() const;

/// @brief Sets the world position of the sprite.
/// @param[in] xPositionInPixels - The x-coordinate of the sprite in the world.
/// @param[in] yPositionInPixels - The y-coordinate of the sprite in the world.
void SetWorldPosition(const float xPositionInPixels, const float yPositionInPixels);

/// @brief Gets the bounding box of the sprite in the world.
/// @return The bounding box of the sprite.
hgeRect GetWorldBoundingBox();

/// @brief Moves the sprite's world position up the specified distance.
/// @param[in] distanceToMoveInPixels - The distance to move, in pixels.
void MoveUp(const float distanceToMoveInPixels);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ void AnimationSequence::SetZPosition(const float zPosition)
m_pAnimation->SetZ(zPosition);
}

hgeRect AnimationSequence::GetWorldBoundingBox(const float worldXPositionInPixels, const float worldYPositionInPixels)
{
hgeRect worldBoundingBox(0.0f, 0.0f, 0.0f, 0.0f);
m_pAnimation->GetBoundingBox(worldXPositionInPixels, worldYPositionInPixels, &worldBoundingBox);
return worldBoundingBox;
}

void AnimationSequence::Play()
{
// PLAY THE ANIMATION SEQUENCE IF IT ISN'T ALREADY PLAYING.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ namespace GRAPHICS
/// @param[in] zPosition - The z-position to set for the graphics component.
void SetZPosition(const float zPosition);

/// @brief Gets the bounding box of the animation sequence, given
/// the provided world coordinates.
/// @param[in] worldXPositionInPixels - The world X position that the animation sequence is rendered at.
/// @param[in] worldYPositionInPixels - The world Y position that the animation sequence is rendered at.
/// @return The bounding box of the animation sequence.
hgeRect GetWorldBoundingBox(const float worldXPositionInPixels, const float worldYPositionInPixels);

/// @brief Begins playing the animation sequence, if it isn't already playing.
void Play();

Expand Down
20 changes: 20 additions & 0 deletions noah_ark/library/noah_ark_library/src/Maps/TileMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ MATH::Vector2f TileMap::GetTopLeftWorldPosition() const
return m_topLeftWorldPositionInPixels;
}

MATH::Vector2f TileMap::GetBottomRightWorldPosition() const
{
// CALCULATE THE BOTTOM RIGHT POSITION FROM OTHER TILE MAP PROPERTIES.
// Calculate the bottom position.
float mapHeightInTiles = static_cast<float>(GetHeightInTiles());
float tileHeightInPixels = static_cast<float>(GetTileHeightInPixels());
float mapHeightInPixels = mapHeightInTiles * tileHeightInPixels;
float bottomWorldPosition = m_topLeftWorldPositionInPixels.Y + mapHeightInPixels;

// Calculate the right position.
float mapWidthInTiles = static_cast<float>(GetWidthInTiles());
float tileWidthInPixels = static_cast<float>(GetTileWidthInPixels());
float mapWidthInPixels = mapWidthInTiles * tileWidthInPixels;
float rightWorldPosition = m_topLeftWorldPositionInPixels.X + mapWidthInPixels;

// Return the bottom right position.
MATH::Vector2f bottomRightWorldPosition(rightWorldPosition, bottomWorldPosition);
return bottomRightWorldPosition;
}

unsigned int TileMap::GetWidthInTiles() const
{
return static_cast<unsigned int>(m_tmxMap->GetWidth());
Expand Down
4 changes: 4 additions & 0 deletions noah_ark/library/noah_ark_library/src/Maps/TileMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ namespace MAPS
/// @return The top-left position of the map.
MATH::Vector2f GetTopLeftWorldPosition() const;

/// @brief Gets the bottom-right world position of the tile map, in pixels.
/// @return The bottom-right position of the map.
MATH::Vector2f GetBottomRightWorldPosition() const;

/// @brief Gets the width of the map, in tiles.
/// @return The width of the map, in tiles.
unsigned int GetWidthInTiles() const;
Expand Down
17 changes: 16 additions & 1 deletion noah_ark/library/noah_ark_library/src/Objects/Noah.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,22 @@ void Noah::SetSprite(const std::shared_ptr<GRAPHICS::AnimatedSprite>& sprite)
{
m_sprite = sprite;
}


hgeRect Noah::GetWorldBoundingBox()
{
return m_sprite->GetWorldBoundingBox();
}

MATH::Vector2f Noah::GetWorldPosition() const
{
return m_sprite->GetWorldPosition();
}

void Noah::SetWorldPosition(const float xPositionInPixels, const float yPositionInPixels)
{
m_sprite->SetWorldPosition(xPositionInPixels, yPositionInPixels);
}

void Noah::MoveUp(const float elapsedTimeInSeconds)
{
// MOVE THE SPRITE.
Expand Down
13 changes: 13 additions & 0 deletions noah_ark/library/noah_ark_library/src/Objects/Noah.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ namespace OBJECTS
/// @param[in] sprite - The sprite to set.
void SetSprite(const std::shared_ptr<GRAPHICS::AnimatedSprite>& sprite);

/// @brief Gets the world position of Noah.
/// @return The world position of Noah, in pixels.
MATH::Vector2f GetWorldPosition() const;

/// @brief Sets the world position of Noah.
/// @param[in] xPositionInPixels - The x-coordinate of the Noah in the world.
/// @param[in] yPositionInPixels - The y-coordinate of the Noah in the world.
void SetWorldPosition(const float xPositionInPixels, const float yPositionInPixels);

/// @brief Gets the bounding box of Noah in the world.
/// @return The bounding box of Noah.
hgeRect GetWorldBoundingBox();

/// @brief Moves Noah up based on the specified amount of time.
/// @param[in] elapsedTimeInSeconds - The elapsed time for which to move Noah.
void MoveUp(const float elapsedTimeInSeconds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,31 @@ void TileMapScrollingTestState::HandleUserInput(
return;
}
MATH::Vector2f currentMapTopLeftPosition = currentTileMap->GetTopLeftWorldPosition();
MATH::Vector2f currentMapBottomRightPosition = currentTileMap->GetBottomRightWorldPosition();

// SCROLL IN THE DIRECTION BASED ON USER INPUT.
if (inputController.ScrollUpButtonPressed())
{
m_noahPlayer.MoveUp(elapsedTimeInSeconds);
return;

// CHECK IF NOAH HAS EXCEEDED THE CURRENT MAP'S TOP BOUNDARY.
MATH::Vector2f noahPosition = m_noahPlayer.GetWorldPosition();
bool noahMovedBeyondCurrentMapTopBoundary = (noahPosition.Y < currentMapTopLeftPosition.Y);
if (!noahMovedBeyondCurrentMapTopBoundary)
{
// Noah hasn't moved beyond the top boundary, so we don't need to do anything.
return;
}

// CHECK IF A TOP MAP EXISTS.
std::shared_ptr<MAPS::TileMap> topTileMap = m_overworldMap.GetTopTileMap();
bool topTileMapExists = (nullptr != topTileMap);
if (topTileMapExists)
if (!topTileMapExists)
{
// Ensure that Noah stays within the bounds of the current map.
m_noahPlayer.SetWorldPosition(noahPosition.X, currentMapTopLeftPosition.Y);
}
else
{
// Get the top map's top-left corner.
MATH::Vector2f topMapTopLeftPosition = topTileMap->GetTopLeftWorldPosition();
Expand Down Expand Up @@ -210,12 +224,25 @@ void TileMapScrollingTestState::HandleUserInput(
if (inputController.ScrollDownButtonPressed())
{
m_noahPlayer.MoveDown(elapsedTimeInSeconds);
return;

// CHECK IF NOAH HAS EXCEEDED THE CURRENT MAP'S BOTTOM BOUNDARY.
MATH::Vector2f noahPosition = m_noahPlayer.GetWorldPosition();
bool noahMovedBeyondCurrentMapBottomBoundary = (noahPosition.Y > currentMapBottomRightPosition.Y);
if (!noahMovedBeyondCurrentMapBottomBoundary)
{
// Noah hasn't moved beyond the bottom boundary, so we don't need to do anything.
return;
}

// CHECK IF A BOTTOM MAP EXISTS.
std::shared_ptr<MAPS::TileMap> bottomTileMap = m_overworldMap.GetBottomTileMap();
bool bottomTileMapExists = (nullptr != bottomTileMap);
if (bottomTileMapExists)
if (!bottomTileMapExists)
{
// Ensure that Noah stays within the bounds of the current map.
m_noahPlayer.SetWorldPosition(noahPosition.X, currentMapBottomRightPosition.Y);
}
else
{
// Get the bottom map's top-left corner.
MATH::Vector2f bottomMapTopLeftPosition = bottomTileMap->GetTopLeftWorldPosition();
Expand Down Expand Up @@ -250,12 +277,25 @@ void TileMapScrollingTestState::HandleUserInput(
if (inputController.ScrollLeftButtonPressed())
{
m_noahPlayer.MoveLeft(elapsedTimeInSeconds);
return;


// CHECK IF NOAH HAS EXCEEDED THE CURRENT MAP'S LEFT BOUNDARY.
MATH::Vector2f noahPosition = m_noahPlayer.GetWorldPosition();
bool noahMovedBeyondCurrentMapLeftBoundary = (noahPosition.X < currentMapTopLeftPosition.X);
if (!noahMovedBeyondCurrentMapLeftBoundary)
{
// Noah hasn't moved beyond the left boundary, so we don't need to do anything.
return;
}

// CHECK IF A LEFT MAP EXISTS.
std::shared_ptr<MAPS::TileMap> leftTileMap = m_overworldMap.GetLeftTileMap();
bool leftTileMapExists = (nullptr != leftTileMap);
if (leftTileMapExists)
if (!leftTileMapExists)
{
// Ensure that Noah stays within the bounds of the current map.
m_noahPlayer.SetWorldPosition(currentMapTopLeftPosition.X, noahPosition.Y);
}
else
{
// Get the left map's top-left corner.
MATH::Vector2f leftMapTopLeftPosition = leftTileMap->GetTopLeftWorldPosition();
Expand Down Expand Up @@ -290,12 +330,25 @@ void TileMapScrollingTestState::HandleUserInput(
if (inputController.ScrollRightButtonPressed())
{
m_noahPlayer.MoveRight(elapsedTimeInSeconds);
return;

// CHECK IF NOAH HAS EXCEEDED THE CURRENT MAP'S RIGHT BOUNDARY.
MATH::Vector2f noahPosition = m_noahPlayer.GetWorldPosition();
bool noahMovedBeyondCurrentMapRightBoundary = (noahPosition.X > currentMapBottomRightPosition.X);
if (!noahMovedBeyondCurrentMapRightBoundary)
{
// Noah hasn't moved beyond the right boundary, so we don't need to do anything.
return;
}

// CHECK IF A RIGHT MAP EXISTS.
std::shared_ptr<MAPS::TileMap> rightTileMap = m_overworldMap.GetRightTileMap();
bool rightTileMapExists = (nullptr != rightTileMap);
if (rightTileMapExists)
if (!rightTileMapExists)
{
// Ensure that Noah stays within the bounds of the current map.
m_noahPlayer.SetWorldPosition(currentMapBottomRightPosition.X, noahPosition.Y);
}
else
{
// Get the right map's top-left corner.
MATH::Vector2f rightMapTopLeftPosition = rightTileMap->GetTopLeftWorldPosition();
Expand Down

0 comments on commit 8d03147

Please sign in to comment.