What you are looking for is, a state machine. Basically, a state machine is a series of code (actions and events) that contain and update a state variable with certain actions that can only be triggered if you are in a specific state. I built an example of a boss type fight using a state machine for a previous forum question that could help you out.
drive.google.com/file/d/1t3nFqiq7syZXKRWGSOE-VrwVknfgDbeV/view
Notice how each action checks the state value of the boss. Also remember, a state in a state machine must have an entrance case (an event that occurs in a different state that sets, or enters the new state) and an exit case (this is an event that is an entrance case to another state). Notice in the example how ever single state has at least one action that sets the state to a different state. These are the entrance and exit cases.