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)
This commit is contained in:
BeniS 2013-01-23 20:31:28 +13:00
parent caf86a9fc6
commit a2ddb472f5
7 changed files with 29 additions and 15 deletions

View File

@ -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()

View File

@ -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;

View File

@ -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();

View File

@ -293,6 +293,7 @@ public:
std::string getWorldName() { return m_worldName; }
std::vector<uint8> 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();

View File

@ -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)

View File

@ -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;

View File

@ -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);