How do I make a camera2d that follow player but only stay inside certain area such as area2d (camera seen from megaman or metroid). I would appreciate if you could teach me how to implement such camera2d. Red line is area2d and camera2d only stay within area2d.

I have looked at mega-man-engine available at github, as they keep camera2d within reference rect, but it was beyond my understand to fully understand how the camera2d is controlled with my few knowledge and experience in coding. I have also looked camera flip screen template published by chucklepie and thought this could be very useful for my project. However, it didnt work somehow, though I followed his tutorial.