// Take A: Centralized control and monitoring of event messages. // Uses a Timer class that is started and set off and can be queried for its // condition and expiration progress (normalized time from 0 to 1) // inherits stuff from the generic archetype class GameObject: // class Event : public GameObject { protected: Timer m_timer; Timer m_delay; bool m_expired = false; bool m_started = false; public: Event() : GameObject(0,"") {} Event(float duration, Timer::timer_type_t timer_type, std::string name = "", float delay = 0.0f) : GameObject(0,name), m_timer(duration, timer_type), m_delay(delay) { } virtual Event* clone() const { return new Event(*this); } void trigger() { m_delay.start(); }; virtual void update(float dt); virtual void draw(); bool isActive() { return m_started && !m_expired; } bool hasStarted() { return m_started; } bool hasExpired() { return m_expired; } }; // ... // Example event specialization: class EventEnable : public Event { protected: class GameObject* m_object = nullptr; bool m_activate; public: EventEnable(GameObject* which, bool active, float delay = 0.0f); void update(float dt) override; Event* clone() const override { return new EventEnable(*this); } }; // ------------- example definitions: void Event::update(float dt) { if ((float)m_delay < 1.0f) { m_started = false; } else if (!m_started) { m_timer.start(); m_started = true; m_expired = false; } if (m_started) { (float)m_timer; if (m_timer.isRunning()) { m_active = true; } else { m_active = false; m_expired = true; } } } EventEnable::EventEnable(GameObject* which, bool active, float delay) : Event(0.0f, Timer::TIMER_ONCE, "", delay), m_activate(active), m_object(which) { } void EventEnable::update(float dt) { Event::update(dt); if (!hasExpired() || !m_object) return; m_object->setActive(m_activate); m_active = false; } // Integration with your global "game" class: // // It shoulf have a std::list m_events; // and an void addEvent(class Event * evt); method to insert // new events into the container. // In the update method of the "game" class, you should call // a void processEvents() method to call the ""update" method on the events. // Once an event "has expired" (this is set to true, when the task of the event is done, // after its delay and scheduled execution - see Event code above), // the processEvents method should remove the expired event(s) from the "active" list. // Take B: Distributed event system. // In this alternative event messaging approach, objects dispatch event messages to a central queue // which only monitors their delay. This can be a sorted linear container, facilitating removal // from the tail of all event messages that are ready to be executed. // The "fired" events are then added to local queues of the destination objects ("dispatched") and // handled locally by the recipient. // Use Take A if you do not expect too many events. Use Take B if you expect hundreds of events per second. // Decentralized event handling can be easier to handle (iterate, sort, remove, execute in shorter lists) and // potentially paralleliable.