diff options
author | Philipp Sehmisch <tmw@crushnet.org> | 2007-12-31 16:14:27 +0000 |
---|---|---|
committer | Philipp Sehmisch <tmw@crushnet.org> | 2007-12-31 16:14:27 +0000 |
commit | 5f04c73af0dbc456fb24047ab11845d751bc5b16 (patch) | |
tree | e473b7b7729e36c484b9146e9804bd5623fdcba7 /src | |
parent | eae0c3171fd94970105d4cbd987c8e1218481985 (diff) | |
download | mana-5f04c73af0dbc456fb24047ab11845d751bc5b16.tar.gz mana-5f04c73af0dbc456fb24047ab11845d751bc5b16.tar.bz2 mana-5f04c73af0dbc456fb24047ab11845d751bc5b16.tar.xz mana-5f04c73af0dbc456fb24047ab11845d751bc5b16.zip |
Added config options to set the scroll center. Retained scroll offset during map change for smoother map transitions.
Diffstat (limited to 'src')
-rw-r--r-- | src/engine.cpp | 5 | ||||
-rw-r--r-- | src/engine.h | 5 | ||||
-rw-r--r-- | src/gui/viewport.cpp | 104 | ||||
-rw-r--r-- | src/gui/viewport.h | 17 | ||||
-rw-r--r-- | src/net/playerhandler.cpp | 7 |
5 files changed, 81 insertions, 57 deletions
diff --git a/src/engine.cpp b/src/engine.cpp index 98b4a5f1..49ffbbfc 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -127,6 +127,11 @@ void Engine::changeMap(const std::string &mapPath) outMsg.writeInt16(CMSG_MAP_LOADED); } +void Engine::scrollBy(float scrollX, float scrollY) +{ + viewport->scrollBy(scrollX, scrollY); +} + void Engine::logic() { beingManager->logic(); diff --git a/src/engine.h b/src/engine.h index 4575051e..06fa2e01 100644 --- a/src/engine.h +++ b/src/engine.h @@ -57,6 +57,11 @@ class Engine void changeMap(const std::string &mapName); /** + * Changes viewpoint by relative pixel coordinates. + */ + void scrollBy(float scrollX, float scrollY); + + /** * Performs engine logic. */ void logic(); diff --git a/src/gui/viewport.cpp b/src/gui/viewport.cpp index 2c664176..d99b48ba 100644 --- a/src/gui/viewport.cpp +++ b/src/gui/viewport.cpp @@ -50,10 +50,10 @@ Viewport::Viewport(): mMap(0), - mViewX(0.0f), - mViewY(0.0f), - mCameraX(0), - mCameraY(0), + mPixelViewX(0.0f), + mPixelViewY(0.0f), + mTileViewX(0), + mTileViewY(0), mShowDebugPath(false), mPlayerFollowMouse(false) { @@ -62,6 +62,8 @@ Viewport::Viewport(): mScrollLaziness = (int) config.getValue("ScrollLaziness", 32); mScrollRadius = (int) config.getValue("ScrollRadius", 32); + mScrollCenterOffsetX = (int) config.getValue("ScrollCenterOffsetX", 0); + mScrollCenterOffsetY = (int) config.getValue("ScrollCenterOffsetY", 0); config.addListener("ScrollLaziness", this); config.addListener("ScrollRadius", this); @@ -150,8 +152,8 @@ Viewport::draw(gcn::Graphics *gcnGraphics) } // Calculate viewpoint - int midTileX = graphics->getWidth() / 32 / 2; - int midTileY = graphics->getHeight() / 32 / 2; + int midTileX = (graphics->getWidth() + mScrollCenterOffsetX) / 32 / 2; + int midTileY = (graphics->getHeight() + mScrollCenterOffsetY) / 32 / 2; int player_x = (player_node->mX - midTileX) * 32 + player_node->getXOffset(); @@ -164,34 +166,34 @@ Viewport::draw(gcn::Graphics *gcnGraphics) // Apply lazy scrolling while (lastTick < tick_time) { - if (player_x > mViewX + mScrollRadius) + if (player_x > mPixelViewX + mScrollRadius) { - mViewX += (player_x - mViewX - mScrollRadius) / mScrollLaziness; + mPixelViewX += (player_x - mPixelViewX - mScrollRadius) / mScrollLaziness; } - if (player_x < mViewX - mScrollRadius) + if (player_x < mPixelViewX - mScrollRadius) { - mViewX += (player_x - mViewX + mScrollRadius) / mScrollLaziness; + mPixelViewX += (player_x - mPixelViewX + mScrollRadius) / mScrollLaziness; } - if (player_y > mViewY + mScrollRadius) + if (player_y > mPixelViewY + mScrollRadius) { - mViewY += (player_y - mViewY - mScrollRadius) / mScrollLaziness; + mPixelViewY += (player_y - mPixelViewY - mScrollRadius) / mScrollLaziness; } - if (player_y < mViewY - mScrollRadius) + if (player_y < mPixelViewY - mScrollRadius) { - mViewY += (player_y - mViewY + mScrollRadius) / mScrollLaziness; + mPixelViewY += (player_y - mPixelViewY + mScrollRadius) / mScrollLaziness; } lastTick++; } // Auto center when player is off screen - if ( player_x - mViewX > graphics->getWidth() / 2 - || mViewX - player_x > graphics->getWidth() / 2 - || mViewY - player_y > graphics->getHeight() / 2 - || player_y - mViewY > graphics->getHeight() / 2 + if ( player_x - mPixelViewX > graphics->getWidth() / 2 + || mPixelViewX - player_x > graphics->getWidth() / 2 + || mPixelViewY - player_y > graphics->getHeight() / 2 + || player_y - mPixelViewY > graphics->getHeight() / 2 ) { - mViewX = player_x; - mViewY = player_y; + mPixelViewX = player_x; + mPixelViewY = player_y; }; // Don't move camera so that the end of the map is on screen @@ -199,31 +201,31 @@ Viewport::draw(gcn::Graphics *gcnGraphics) int viewYmax = (mMap->getHeight() * 32) - graphics->getHeight(); if (mMap) { - if (mViewX < 0) { - mViewX = 0; + if (mPixelViewX < 0) { + mPixelViewX = 0; } - if (mViewY < 0) { - mViewY = 0; + if (mPixelViewY < 0) { + mPixelViewY = 0; } - if (mViewX > viewXmax) { - mViewX = viewXmax; + if (mPixelViewX > viewXmax) { + mPixelViewX = viewXmax; } - if (mViewY > viewYmax) { - mViewY = viewYmax; + if (mPixelViewY > viewYmax) { + mPixelViewY = viewYmax; } } - mCameraX = (int) (mViewX + 16) / 32; - mCameraY = (int) (mViewY + 16) / 32; + mTileViewX = (int) (mPixelViewX + 16) / 32; + mTileViewY = (int) (mPixelViewY + 16) / 32; // Draw tiles and sprites if (mMap) { - mMap->draw(graphics, (int) mViewX, (int) mViewY, 0); + mMap->draw(graphics, (int) mPixelViewX, (int) mPixelViewY, 0); drawTargetCursor(graphics); - mMap->draw(graphics, (int) mViewX, (int) mViewY, 1); - mMap->draw(graphics, (int) mViewX, (int) mViewY, 2); - mMap->drawOverlay(graphics, mViewX, mViewY, + mMap->draw(graphics, (int) mPixelViewX, (int) mPixelViewY, 1); + mMap->draw(graphics, (int) mPixelViewX, (int) mPixelViewY, 2); + mMap->drawOverlay(graphics, mPixelViewX, mPixelViewY, (int) config.getValue("OverlayDetail", 2)); drawTargetName(graphics); } @@ -236,8 +238,8 @@ Viewport::draw(gcn::Graphics *gcnGraphics) int mouseX, mouseY; SDL_GetMouseState(&mouseX, &mouseY); - int mouseTileX = mouseX / 32 + mCameraX; - int mouseTileY = mouseY / 32 + mCameraY; + int mouseTileX = mouseX / 32 + mTileViewX; + int mouseTileY = mouseY / 32 + mTileViewY; Path debugPath = mMap->findPath( player_node->mX, player_node->mY, @@ -246,8 +248,8 @@ Viewport::draw(gcn::Graphics *gcnGraphics) graphics->setColor(gcn::Color(255, 0, 0)); for (PathIterator i = debugPath.begin(); i != debugPath.end(); i++) { - int squareX = i->x * 32 - (int) mViewX + 12; - int squareY = i->y * 32 - (int) mViewY + 12; + int squareX = i->x * 32 - (int) mPixelViewX + 12; + int squareY = i->y * 32 - (int) mPixelViewY + 12; graphics->fillRectangle(gcn::Rectangle(squareX, squareY, 8, 8)); graphics->drawText( @@ -260,9 +262,9 @@ Viewport::draw(gcn::Graphics *gcnGraphics) Beings &beings = beingManager->getAll(); for (BeingIterator i = beings.begin(); i != beings.end(); i++) { - (*i)->drawSpeech(graphics, -(int) mViewX, -(int) mViewY); - (*i)->drawName(graphics, -(int) mViewX, -(int) mViewY); - (*i)->drawEmotion(graphics, -(int) mViewX, -(int) mViewY); + (*i)->drawSpeech(graphics, -(int) mPixelViewX, -(int) mPixelViewY); + (*i)->drawName(graphics, -(int) mPixelViewX, -(int) mPixelViewY); + (*i)->drawEmotion(graphics, -(int) mPixelViewX, -(int) mPixelViewY); } // Draw contained widgets @@ -283,8 +285,8 @@ Viewport::logic() if (mPlayerFollowMouse && button & SDL_BUTTON(1) && mWalkTime != player_node->mWalkTime) { - player_node->setDestination(mouseX / 32 + mCameraX, - mouseY / 32 + mCameraY); + player_node->setDestination(mouseX / 32 + mTileViewX, + mouseY / 32 + mTileViewY); mWalkTime = player_node->mWalkTime; } @@ -321,8 +323,8 @@ Viewport::drawTargetCursor(Graphics *graphics) } // Draw the target cursor at the correct position - int posX = target->getPixelX() + 16 - targetCursor->getWidth() / 2 - (int) mViewX; - int posY = target->getPixelY() + 16 - targetCursor->getHeight() / 2 - (int) mViewY; + int posX = target->getPixelX() + 16 - targetCursor->getWidth() / 2 - (int) mPixelViewX; + int posY = target->getPixelY() + 16 - targetCursor->getHeight() / 2 - (int) mPixelViewY; graphics->drawImage(targetCursor, posX, posY); } @@ -339,8 +341,8 @@ Viewport::drawTargetName(Graphics *graphics) graphics->setColor(gcn::Color(255, 32, 32)); const MonsterInfo &mi = static_cast<Monster*>(target)->getInfo(); - int posX = target->getPixelX() + 16 - (int)mViewX; - int posY = target->getPixelY() + 16 - target->getHeight() - (int)mViewY; + int posX = target->getPixelX() + 16 - (int)mPixelViewX; + int posY = target->getPixelY() + 16 - target->getHeight() - (int)mPixelViewY; graphics->drawText(mi.getName(), posX, posY, gcn::Graphics::CENTER); } @@ -359,8 +361,8 @@ Viewport::mousePressed(gcn::MouseEvent &event) mPlayerFollowMouse = false; - int tilex = event.getX() / 32 + mCameraX; - int tiley = event.getY() / 32 + mCameraY; + int tilex = event.getX() / 32 + mTileViewX; + int tiley = event.getY() / 32 + mTileViewY; // Right click might open a popup if (event.getButton() == gcn::MouseEvent::RIGHT) @@ -455,8 +457,8 @@ Viewport::mouseDragged(gcn::MouseEvent &event) if (mPlayerFollowMouse && mWalkTime == player_node->mWalkTime) { - int destX = event.getX() / 32 + mCameraX; - int destY = event.getY() / 32 + mCameraY; + int destX = event.getX() / 32 + mTileViewX; + int destY = event.getY() / 32 + mTileViewY; player_node->setDestination(destX, destY); } } diff --git a/src/gui/viewport.h b/src/gui/viewport.h index eeb31bae..58ef7146 100644 --- a/src/gui/viewport.h +++ b/src/gui/viewport.h @@ -120,13 +120,16 @@ class Viewport : public WindowContainer, public gcn::MouseListener, * Returns camera x offset in tiles. */ int - getCameraX() { return mCameraX; } + getCameraX() { return mTileViewX; } /** * Returns camera y offset in tiles. */ int - getCameraY() { return mCameraY; } + getCameraY() { return mTileViewY; } + + void + scrollBy (float x, float y) { mPixelViewX += x; mPixelViewY += y; } private: /** @@ -153,10 +156,12 @@ class Viewport : public WindowContainer, public gcn::MouseListener, int mScrollRadius; int mScrollLaziness; - float mViewX; /**< Current viewpoint in pixels. */ - float mViewY; /**< Current viewpoint in pixels. */ - int mCameraX; /**< Current viewpoint in tiles. */ - int mCameraY; /**< Current viewpoint in tiles. */ + int mScrollCenterOffsetX; + int mScrollCenterOffsetY; + float mPixelViewX; /**< Current viewpoint in pixels. */ + float mPixelViewY; /**< Current viewpoint in pixels. */ + int mTileViewX; /**< Current viewpoint in tiles. */ + int mTileViewY; /**< Current viewpoint in tiles. */ bool mShowDebugPath; /**< Show a path from player to pointer. */ /** Images of in range target cursor. */ diff --git a/src/net/playerhandler.cpp b/src/net/playerhandler.cpp index a5a2330b..0ea404ed 100644 --- a/src/net/playerhandler.cpp +++ b/src/net/playerhandler.cpp @@ -125,11 +125,18 @@ void PlayerHandler::handleMessage(MessageIn *msg) current_npc = 0; + float scrollOffsetX = (x - player_node->mX) * 32; + float scrollOffsetY = (y - player_node->mY) * 32; + player_node->setAction(Being::STAND); player_node->stopAttack(); player_node->mFrame = 0; player_node->mX = x; player_node->mY = y; + + logger->log("Adjust scrolling by %d:%d", (int)scrollOffsetX, (int)scrollOffsetY); + + engine->scrollBy(scrollOffsetX, scrollOffsetY); } break; |