diff options
-rw-r--r-- | src/flooritem.cpp | 6 | ||||
-rw-r--r-- | src/map.cpp | 12 | ||||
-rw-r--r-- | src/map.h | 8 | ||||
-rw-r--r-- | src/net/tmwa/beinghandler.cpp | 41 | ||||
-rw-r--r-- | src/net/tmwa/playerhandler.cpp | 9 |
5 files changed, 40 insertions, 36 deletions
diff --git a/src/flooritem.cpp b/src/flooritem.cpp index c5a031fe..8805ba88 100644 --- a/src/flooritem.cpp +++ b/src/flooritem.cpp @@ -38,11 +38,7 @@ FloorItem::FloorItem(int id, { setMap(map); - // TODO: Eventually, we probably should fix all sprite offsets so that - // these translations aren't necessary anymore. The sprites know - // best where their base point should be. - mPos.x = x * map->getTileWidth() + 16; - mPos.y = y * map->getTileHeight() + 16; + mPos = map->getTileCenter(x, y); setupSpriteDisplay(itemDb->get(itemId).getDisplay()); } diff --git a/src/map.cpp b/src/map.cpp index 78171ca9..949b00bd 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -607,6 +607,18 @@ bool Map::occupied(int x, int y) const return false; } +Vector Map::getTileCenter(int x, int y) +{ + Vector tileCenterPos; + + if (!contains(x, y)) + return tileCenterPos; + + tileCenterPos.x = x * mTileWidth + mTileWidth / 2; + tileCenterPos.y = y * mTileHeight + mTileHeight / 2; + return tileCenterPos; +} + bool Map::contains(int x, int y) const { return x >= 0 && y >= 0 && x < mWidth && y < mHeight; @@ -267,6 +267,14 @@ class Map : public Properties int getTileHeight() const { return mTileHeight; } + /** + * Returns the nearest tile center position in pixels coordinates. + * + * @param x the horizontal tile position + * @param y the vertical tile position + */ + Vector getTileCenter(int x, int y); + const std::string getMusicFile() const; const std::string getName() const; diff --git a/src/net/tmwa/beinghandler.cpp b/src/net/tmwa/beinghandler.cpp index d61559f8..960f08ed 100644 --- a/src/net/tmwa/beinghandler.cpp +++ b/src/net/tmwa/beinghandler.cpp @@ -101,18 +101,15 @@ Being *createBeing(int id, short job) return being; } -static void handleMoveMessage(Uint16 srcX, Uint16 srcY, - Uint16 dstX, Uint16 dstY, - Being *dstBeing, int tileWidth, int tileHeight) +static void handleMoveMessage(Map *map, Being *dstBeing, + Uint16 srcX, Uint16 srcY, + Uint16 dstX, Uint16 dstY) { // Avoid dealing with flawed destination - if (srcX && srcY && dstX && dstY) + if (map && dstBeing && srcX && srcY && dstX && dstY) { - Vector pos(srcX * tileWidth + tileWidth / 2, - srcY * tileHeight + tileHeight / 2); - - Vector dest(dstX * tileWidth + tileWidth / 2, - dstY * tileHeight + tileHeight / 2); + Vector pos = map->getTileCenter(srcX, srcY); + Vector dest = map->getTileCenter(dstX, dstY); // Don't set the position as the movement algorithm // can guess it and it would break the animation played, @@ -125,14 +122,13 @@ static void handleMoveMessage(Uint16 srcX, Uint16 srcY, } } -static void handlePosMessage(Uint16 x, Uint16 y, Being *dstBeing, - int tileWidth, int tileHeight, Uint8 dir = 0) +static void handlePosMessage(Map *map, Being *dstBeing, Uint16 x, Uint16 y, + Uint8 dir = 0) { // Avoid dealing with flawed destination - if (x && y) + if (map && dstBeing && x && y) { - Vector pos(x * tileWidth + tileWidth / 2, - y * tileHeight + tileHeight / 2); + Vector pos = map->getTileCenter(x, y); Vector beingPos = dstBeing->getPosition(); // Don't set the position as the movement algorithm // can guess it and it would break the animation played, @@ -171,8 +167,6 @@ void BeingHandler::handleMessage(Net::MessageIn &msg) // Prepare useful translation variables Map *map = Game::instance()->getCurrentMap(); - int tileWidth = map->getTileWidth(); - int tileHeight = map->getTileHeight(); switch (msg.getId()) { @@ -264,15 +258,14 @@ void BeingHandler::handleMessage(Net::MessageIn &msg) { Uint16 srcX, srcY, dstX, dstY; msg.readCoordinatePair(srcX, srcY, dstX, dstY); - handleMoveMessage(srcX, srcY, dstX, dstY, dstBeing, - tileWidth, tileHeight); + handleMoveMessage(map, dstBeing, srcX, srcY, dstX, dstY); } else { Uint8 dir; Uint16 x, y; msg.readCoordinates(x, y, dir); - handlePosMessage(x, y, dstBeing, tileWidth, tileHeight, dir); + handlePosMessage(map, dstBeing, x, y, dir); } msg.readInt8(); // unknown @@ -312,8 +305,7 @@ void BeingHandler::handleMessage(Net::MessageIn &msg) Uint16 srcX, srcY, dstX, dstY; msg.readCoordinatePair(srcX, srcY, dstX, dstY); msg.readInt32(); // Server tick - handleMoveMessage(srcX, srcY, dstX, dstY, dstBeing, - tileWidth, tileHeight); + handleMoveMessage(map, dstBeing, srcX, srcY, dstX, dstY); } break; @@ -624,15 +616,14 @@ void BeingHandler::handleMessage(Net::MessageIn &msg) { Uint16 srcX, srcY, dstX, dstY; msg.readCoordinatePair(srcX, srcY, dstX, dstY); - handleMoveMessage(srcX, srcY, dstX, dstY, dstBeing, - tileWidth, tileHeight); + handleMoveMessage(map, dstBeing, srcX, srcY, dstX, dstY); } else { Uint8 dir; Uint16 x, y; msg.readCoordinates(x, y, dir); - handlePosMessage(x, y, dstBeing, tileWidth, tileHeight, dir); + handlePosMessage(map, dstBeing, x, y, dir); } gmstatus = msg.readInt16(); @@ -687,7 +678,7 @@ void BeingHandler::handleMessage(Net::MessageIn &msg) Uint16 x, y; x = msg.readInt16(); y = msg.readInt16(); - handlePosMessage(x, y, dstBeing, tileWidth, tileHeight); + handlePosMessage(map, dstBeing, x, y); } } break; diff --git a/src/net/tmwa/playerhandler.cpp b/src/net/tmwa/playerhandler.cpp index 9d97dc73..f7a211d1 100644 --- a/src/net/tmwa/playerhandler.cpp +++ b/src/net/tmwa/playerhandler.cpp @@ -202,21 +202,18 @@ void PlayerHandler::handleMessage(Net::MessageIn &msg) /* Scroll if necessary */ Map *map = game->getCurrentMap(); - int tileWidth = map->getTileWidth(); - int tileHeight = map->getTileHeight(); int tileX = player_node->getTileX(); int tileY = player_node->getTileY(); if (!sameMap || (abs(x - tileX) > MAP_TELEPORT_SCROLL_DISTANCE) || (abs(y - tileY) > MAP_TELEPORT_SCROLL_DISTANCE)) { - scrollOffsetX = (x - tileX) * tileWidth; - scrollOffsetY = (y - tileY) * tileHeight; + scrollOffsetX = (x - tileX) * map->getTileWidth(); + scrollOffsetY = (y - tileY) * map->getTileHeight(); } player_node->setAction(Being::STAND); - Vector pos(x * tileWidth + tileWidth / 2, - y * tileHeight + tileHeight / 2); + Vector pos = map->getTileCenter(x, y); player_node->setPosition(pos); // Stop movement player_node->setDestination(pos.x, pos.y); |