From a2ddb472f576aaab5f970cf6a6151f3fdd5e5cbd Mon Sep 17 00:00:00 2001 From: BeniS Date: Wed, 23 Jan 2013 20:31:28 +1300 Subject: [PATCH] Fixed up movement controls again and fixed a typo. * You can change direction while already moving (There could be a better way for walking control, but this works fine for now) --- modules/game_interface/gameinterface.lua | 26 ++++++++++++++++++------ src/client/creature.cpp | 1 - src/client/game.cpp | 3 +-- src/client/game.h | 1 + src/client/localplayer.cpp | 10 ++++----- src/client/localplayer.h | 2 +- src/client/luafunctions.cpp | 1 + 7 files changed, 29 insertions(+), 15 deletions(-) diff --git a/modules/game_interface/gameinterface.lua b/modules/game_interface/gameinterface.lua index e7d29324..fa5e6288 100644 --- a/modules/game_interface/gameinterface.lua +++ b/modules/game_interface/gameinterface.lua @@ -1,4 +1,4 @@ -WALK_AUTO_REPEAT_DELAY = 150 +WALK_AUTO_REPEAT_DELAY = 180 WALK_STEPS_RETRY = 10 gameRootPanel = nil @@ -13,7 +13,8 @@ logoutWindow = nil exitWindow = nil bottomSplitter = nil -lastWalkDir = nil +lastDir = nil +walkEvent = nil arrowKeys = { [North] = 'Up', [South] = 'Down', @@ -64,6 +65,11 @@ function bindKeys() g_keyboard.bindKeyPress('Down', function() smartWalk(South) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY) g_keyboard.bindKeyPress('Left', function() smartWalk(West) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY) + g_keyboard.bindKeyDown('Up', function() changeWalkDir(North) end, gameRootPanel) + g_keyboard.bindKeyDown('Right', function() changeWalkDir(East) end, gameRootPanel) + g_keyboard.bindKeyDown('Down', function() changeWalkDir(South) end, gameRootPanel) + g_keyboard.bindKeyDown('Left', function() changeWalkDir(West) end, gameRootPanel) + g_keyboard.bindKeyPress('Numpad8', function() smartWalk(North) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY) g_keyboard.bindKeyPress('Numpad9', function() smartWalk(NorthEast) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY) g_keyboard.bindKeyPress('Numpad6', function() smartWalk(East) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY) @@ -199,9 +205,17 @@ function tryLogout() anchor=AnchorHorizontalCenter}, yesCallback, noCallback) end +function changeWalkDir(dir) + local player = g_game.getLocalPlayer() + local lastWalkDir = g_game.getLastWalkDir() + if lastWalkDir ~= dir and player:isWalking() then + smartWalk(dir) + end +end + function smartWalk(defaultDir) local rebindKey = false - local lastKey = arrowKeys[lastWalkDir] + local lastKey = arrowKeys[lastDir] -- choose the new direction if not g_keyboard.isKeyPressed(arrowKeys[defaultDir]) then @@ -219,7 +233,7 @@ function smartWalk(defaultDir) end -- key is still pressed from previous walk event - if lastWalkDir and lastWalkDir ~= defaultDir and g_keyboard.isKeyPressed(lastKey) then + if lastDir and lastDir ~= defaultDir and g_keyboard.isKeyPressed(lastKey) then if g_keyboard.isKeySetPressed(arrowKeys) then g_keyboard.unbindKeyPress(lastKey, gameRootPanel) rebindKey = true @@ -250,9 +264,9 @@ function smartWalk(defaultDir) end if rebindKey then - g_keyboard.bindKeyPress(lastKey, function() smartWalk(lastWalkDir) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY) + g_keyboard.bindKeyPress(lastKey, function() smartWalk(lastDir) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY) end - lastWalkDir = dir + lastDir = dir end function updateStretchShrink() diff --git a/src/client/creature.cpp b/src/client/creature.cpp index 13988123..13833505 100644 --- a/src/client/creature.cpp +++ b/src/client/creature.cpp @@ -318,7 +318,6 @@ void Creature::walk(const Position& oldPos, const Position& newPos) m_walkFinishAnimEvent = nullptr; } - // no direction need to be changed when the walk ends m_walkTurnDirection = Otc::InvalidDirection; diff --git a/src/client/game.cpp b/src/client/game.cpp index cbeaa177..ad1a7045 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -538,8 +538,7 @@ bool Game::walk(Otc::Direction direction) if(m_lastWalkDir != direction) { // must add a new walk event float ticks = m_localPlayer->getStepTicksLeft(); - if(ticks < 0) - ticks = 0; + if(ticks < 0) { ticks = 0; } if(m_walkEvent) { m_walkEvent->cancel(); diff --git a/src/client/game.h b/src/client/game.h index e927128d..27f22675 100644 --- a/src/client/game.h +++ b/src/client/game.h @@ -293,6 +293,7 @@ public: std::string getWorldName() { return m_worldName; } std::vector getGMActions() { return m_gmActions; } bool isGM() { return m_gmActions.size() > 0; } + Otc::Direction getLastWalkDir() { return m_lastWalkDir; } std::string formatCreatureName(const std::string &name); int findEmptyContainerId(); diff --git a/src/client/localplayer.cpp b/src/client/localplayer.cpp index 0cc4884a..6662737c 100644 --- a/src/client/localplayer.cpp +++ b/src/client/localplayer.cpp @@ -95,7 +95,7 @@ void LocalPlayer::walk(const Position& oldPos, const Position& newPos) // a prewalk was going on if(m_preWalking) { if(m_waitingWalkPong) { - if(newPos == m_lastPrewalkDestionation) + if(newPos == m_lastPrewalkDestination) m_lastWalkPing = m_walkPingTimer.ticksElapsed(); m_waitingWalkPong = false; @@ -105,7 +105,7 @@ void LocalPlayer::walk(const Position& oldPos, const Position& newPos) m_preWalking = false; m_lastPrewalkDone = true; // if is to the last prewalk destination, updates the walk preserving the animation - if(newPos == m_lastPrewalkDestionation) { + if(newPos == m_lastPrewalkDestination) { updateWalk(); // was to another direction, replace the walk } else @@ -127,7 +127,7 @@ void LocalPlayer::preWalk(Otc::Direction direction) Position newPos = m_position.translatedToDirection(direction); // avoid reanimating prewalks - if(m_preWalking && m_lastPrewalkDestionation == newPos) + if(m_preWalking && m_lastPrewalkDestination == newPos) return; m_waitingWalkPong = false; @@ -143,7 +143,7 @@ void LocalPlayer::preWalk(Otc::Direction direction) // start walking to direction m_lastPrewalkDone = false; - m_lastPrewalkDestionation = newPos; + m_lastPrewalkDestination = newPos; Creature::walk(m_position, newPos); } @@ -233,7 +233,7 @@ void LocalPlayer::stopWalk() Creature::stopWalk(); // will call terminateWalk m_lastPrewalkDone = true; - m_lastPrewalkDestionation = Position(); + m_lastPrewalkDestination = Position(); } void LocalPlayer::updateWalkOffset(int totalPixelsWalked) diff --git a/src/client/localplayer.h b/src/client/localplayer.h index 6f2eaefc..bd1c6919 100644 --- a/src/client/localplayer.h +++ b/src/client/localplayer.h @@ -119,7 +119,7 @@ protected: private: // walk related Timer m_walkPingTimer; - Position m_lastPrewalkDestionation; + Position m_lastPrewalkDestination; Position m_autoWalkDestination; Position m_lastAutoWalkPosition; ScheduledEventPtr m_autoWalkEndEvent; diff --git a/src/client/luafunctions.cpp b/src/client/luafunctions.cpp index 877355a4..b342da74 100644 --- a/src/client/luafunctions.cpp +++ b/src/client/luafunctions.cpp @@ -242,6 +242,7 @@ void Client::registerLuaFunctions() g_lua.bindSingletonFunction("g_game", "disableFeature", &Game::disableFeature, &g_game); g_lua.bindSingletonFunction("g_game", "isGM", &Game::isGM, &g_game); g_lua.bindSingletonFunction("g_game", "answerModalDialog", &Game::answerModalDialog, &g_game); + g_lua.bindSingletonFunction("g_game", "getLastWalkDir", &Game::getLastWalkDir, &g_game); g_lua.registerSingletonClass("g_shaders"); g_lua.bindSingletonFunction("g_shaders", "createShader", &ShaderManager::createShader, &g_shaders);