-
Notifications
You must be signed in to change notification settings - Fork 14
Game Events and MonoBehaviours
Christian Oeing edited this page Jan 8, 2017
·
1 revision
Whenever the game state changes, you might want to update your visualization.
The GameEventBehaviour is a great base class to extend if you want to listen to specific events. You can derive from this class and add the mono behaviour to your Unity GameObject (e.g. your knight):
[RequireComponent(typeof(EntityConfigurationBehaviour))]
public class PlayAnimationOnDamage : GameEventBehaviour
{
/// <summary>
/// Game entity associated with this Unity game object.
/// </summary>
private EntityConfigurationBehaviour entityConfigurationBehaviour;
protected override void Awake()
{
base.Awake();
// Get the game entity associated with this Unity game object.
this.entityConfigurationBehaviour =
this.GetComponent<EntityConfigurationBehaviour>();
}
protected override void RegisterListeners()
{
base.RegisterListeners();
// Register as listener for the DamageTaken event.
this.RegisterListener(RPGGameEvent.DamageTaken, this.OnDamageTaken);
}
private void OnDamageTaken(GameEvent e)
{
var data = (DamageTakenData)e.EventData;
// Check if it's us who's taken damage.
if (data.EntityId == this.entityConfigurationBehaviour.EntityId)
{
// Play hit animation.
this.animation.Play("HitAnimation");
}
}
}
You can override the RegisterListeners method to register a listener for specific game events. In this example, the game object will play an animation whenever the associated entity takes damage.