How to code movement in a top down game?

Hello, my character always look where the mouse is. I’ve animations where he is walking forward and walking to the right. If I press D he moves to the right and the animation plays. However this will only work if he’s looking forward (up). If he’s looking to the right, then when I press D he supposed to walk using the forward animation. How do I play the right animation taking in consideration where he is looking? Video to show what’s happening. reddit post

I’ve found this video which helped