I am looking for a liable solution to my problem. The goal of this algorithm was to attempt to allow the player to remember the last keystroke of the player so once they lift the finger off we can play the animation corresponding to that key.
The approach I took was to use a hashmap and a stack. I pop the last key off the stack to move onto the next key. This approach allows me to use the logic like an undo/redo queue. However, the animations work part of the time but not always.
Please tell me if I overengineered this problem. As I am looking for any solution to make a smooth walking in all directions with animations script.
function RunAnimation(State, KeyCode)
if State == Enum.UserInputState.End then
if KeyCode or Stack[#Stack] == Enum.KeyCode.A then
Animations.Left(nil, State)
elseif KeyCode or Stack[#Stack] == Enum.KeyCode.D then
Animations.Right(nil, State)
elseif KeyCode or Stack[#Stack] == Enum.KeyCode.W then
Animations.Walk(nil, State)
elseif KeyCode or Stack[#Stack] == Enum.KeyCode.S then
Animations.Back(nil, State)
end
end
end
function Append(KeyCode)
local Index = #Stack + 1
Hash[KeyCode] = Index
Stack[Index] = KeyCode
end
function Pop(KeyCode)
local Index = Hash[KeyCode]
RunAnimation(Enum.UserInputState.End, table.remove(Stack, Index))
Hash[KeyCode] = nil
end