Updating the player and enemy to use health
Not only Player
and enemy objects but any object can be set up to use HealthSystem
. It’s barely an inconvenience; in fact, the object simply needs to implement the IHaveHealth
interface.
Assigning the object with health – IHaveHealth interface
Back in the health system UML diagram (Figure 8.1), we see at the bottom that the object having health will implement the IHaveHealth
interface (again, some meaningful naming here). Create a new file named IHaveHealth
in the Assets/Scripts/Interfaces
folder:
internal interface IHaveHealth { void HealthChanged(int amount); void Died(); }
We don’t yet have a class for the Player
object, only PlayerController
. We don’t want to add health to something named controller
because it wouldn’t make sense considering the single-responsibility principle – and the controller’s only concern is movement. Let...