function switch(c, cases)
local f
if (c) then
f = cases[c] or cases.default
else
f = cases.missing or cases.default
end
if f then
if type(f)=="function" then
return f()
else
error("case "..tostring(c).." not a function")
end
end
end
keymask = tonumber("40000000",16)
eKeyRight = keymask + 79
eKeyLeft = keymask + 80
eKeyDown = keymask + 81
eKeyUp = keymask + 82
target_pos = {0, 0}
registerEventHandler("keyEvent", "keyHandler")
function keyHandler(type, char, keycode, mod)
local oldtarget = {target_pos[1], target_pos[2]}
if type == eEvtKeyDown then
switch(keycode, {
[eKeyRight] = function() target_pos[1] = 1 end,
[eKeyLeft] = function() target_pos[1] = -1 end,
[eKeyUp] = function() target_pos[2] = 1 end,
[eKeyDown] = function() target_pos[2] = -1 end,
})
elseif type == eEvtKeyUp then
switch(keycode, {
[eKeyRight] = function() target_pos[1] = 0 end,
[eKeyLeft] = function() target_pos[1] = 0 end,
[eKeyUp] = function() target_pos[2] = 0 end,
[eKeyDown] = function() target_pos[2] = 0 end,
})
end
if target_pos[1] ~= oldtarget[1] or target_pos[2] ~= oldtarget[2] then
local newtarget = {}
if target_pos[1] == 0 then
newtarget[1] = game.CurrentCharacter.Position.x
elseif target_pos[1] == -1 then
newtarget[1] = 0
else
newtarget[1] = 1900
end
if target_pos[2] == 0 then
newtarget[2] = game.CurrentCharacter.Position.y
elseif target_pos[2] == -1 then
newtarget[2] = 1000
else
newtarget[2] = 0
end
game.CurrentCharacter.Destination = {x = newtarget[1] , y = newtarget[2]}
end
return true
end