It wouldn't be too hard to do with pathfinding. they have conditions for: on path found, on path arrived, on path not found, etc.
You could then put an instance variable on the enemy and call it "State", and use different enemy states to do different things like Flee state, attack state, wait state.
Here's a tutorial on pathfinding:
construct.net/en