How to make an Ai to do a specific animation (2d game)

I want to make an AI character to do a specific animation when I am close to it ?
For example when I am far from it id does its things such as walking etc… but when I get close to it to start going towards me and when close enough start the attack animation