From 050cee64b64aba57de84383c7138c1ad0f4ded55 Mon Sep 17 00:00:00 2001 From: Philipp Sehmisch Date: Sun, 26 Oct 2008 00:02:01 +0000 Subject: Pixel-accurate and fluently animated keyboard movement by Kage Jittai - albeit not flawless it is still a big improvement. --- src/game.cpp | 19 ++++++++++++++++++- src/localplayer.cpp | 36 +++++++++++++++++++++++++++++++----- src/localplayer.h | 11 +++++++++++ 3 files changed, 60 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/game.cpp b/src/game.cpp index fc16e1ea..901b8dea 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -787,7 +787,24 @@ void Game::handleInput() direction |= Being::RIGHT; } - player_node->setWalkingDir(direction); + // First if player is pressing key for the direction he is already going + if (direction == player_node->getWalkingDir()) + { + player_node->setWalkingDir(direction); + } + // Else if he is pressing a key, and its different from what he has + // been pressing, stop (do not send this stop to the server) and + // start in the new direction + else if (direction && direction != player_node->getWalkingDir()) + { + player_node->stopWalking(false); + player_node->setWalkingDir(direction); + } + // Else, he is not pressing a key, stop (sending to server) + else + { + player_node->stopWalking(true); + } // Target the nearest player if 'q' is pressed if (keyboard.isKeyActive(keyboard.KEY_TARGET_PLAYER)) diff --git a/src/localplayer.cpp b/src/localplayer.cpp index 68648d74..9c1cd743 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -46,7 +46,7 @@ #include "utils/tostring.h" #include "utils/gettext.h" -const short walkingKeyboardDelay = 100; +const short walkingKeyboardDelay = 500; LocalPlayer *player_node = NULL; @@ -242,6 +242,7 @@ void LocalPlayer::walk(unsigned char dir) return; const Vector &pos = getPosition(); + int dScaler; // Distance to walk if (mAction == WALK && !mPath.empty()) { @@ -273,11 +274,22 @@ void LocalPlayer::walk(unsigned char dir) (pos.y + dy) / 32, getWalkMask())) dx = 16 - (int) pos.x % 32; - // Walk to where the player can actually go - if ((dx || dy) && mMap->getWalk(((int) pos.x + dx) / 32, - ((int) pos.y + dy) / 32, getWalkMask())) + // Checks our path up to 5 tiles, if a blocking tile is found + // We go to the last good tile, and break out of the loop + for (dScaler = 1; dScaler <= 5; dScaler++) { - setDestination((int) pos.x + dx, (int) pos.y + dy); + if ( (dx || dy) && + !mMap->getWalk( ((int) pos.x + (dx * dScaler)) / 32, + ((int) pos.y + (dy * dScaler)) / 32, getWalkMask()) ) + { + dScaler--; + break; + } + } + + if (dScaler >= 0) + { + setDestination((int) pos.x + (dx * dScaler), (int) pos.y + (dy * dScaler)); } else if (dir) { @@ -336,6 +348,20 @@ void LocalPlayer::setWalkingDir(int dir) } } +void LocalPlayer::stopWalking(bool sendToServer) +{ + if(mAction == WALK && mWalkingDir){ + mWalkingDir = 0; + mLocalWalkTime = 0; + Being::setDestination(getPosition().x,getPosition().y); + if (sendToServer) + Net::GameServer::Player::walk(getPosition().x, getPosition().y); + setAction(STAND); + } + + clearPath(); +} + void LocalPlayer::toggleSit() { if (mLastAction != -1) diff --git a/src/localplayer.h b/src/localplayer.h index 7d6f3862..7a5158cc 100644 --- a/src/localplayer.h +++ b/src/localplayer.h @@ -251,6 +251,17 @@ class LocalPlayer : public Player */ void setWalkingDir(int dir); + /** + * Gets the walking direction + */ + int getWalkingDir() const + { return mWalkingDir; } + + /** + * Stops the player dead in his tracks + */ + void stopWalking(bool sendToServer = true); + /** * Uses a character point to raise an attribute */ -- cgit v1.2.3-70-g09d2