Now that we have the core elements of the pattern in place, we can write some code to test our RaceEventBus class. For reasons of brevity, I have removed all the behavior code in each client class to focus on the use of the pattern:
- For starters, we are going to write a countdown timer that subscribes to the COUNTDOWN race event type. Once the COUNTDOWN event is published, it will trigger a 3-second countdown to the start of the race. And at the exact moment the count reaches its end, it will publish the START event to signal the beginning of the race:
using UnityEngine;
using System.Collections;
namespace Chapter.EventBus
{
public class CountdownTimer : MonoBehaviour
{
private float _currentTime;
private float duration = 3.0f;
void OnEnable() {
RaceEventBus.Subscribe(
RaceEventType.COUNTDOWN, StartTimer);
}
void OnDisable() {
RaceEventBus.Unsubscribe(
RaceEventType.COUNTDOWN...