Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // NOTE: This is just an implementation of the events idea, actual code is subject to change
- // In its own "Event" file:
- typedef std::function<void(Unit*)> UnitProc; // possibly its own class for function composition?
- class Event
- {
- public:
- // ctor
- Event(const UnitFilter &condition, const UnitProc &action, int timesToRun = -1)
- : condProc(condition)
- , execProc(action)
- , runCount(timesToRun)
- {};
- // check and run
- bool run(Unit *pUnit)
- {
- // Check the condition
- if ( !this->isConditionMet(pUnit) )
- return false;
- this->execute(pUnit);
- // decrement the run count, negative value is infinite
- if ( this->runCount > 0 )
- this->runCount--;
- };
- // check condition
- bool isConditionMet(Unit *pUnit)
- {
- if ( this->runCount == 0 || pUnit == nullptr)
- return false;
- // if cond is invalid, then there is no condition
- return !this->condProc.isValid() || this->condProc(pUnit);
- };
- // execute the procedure
- void execute(Unit *pUnit)
- {
- if ( this->execProc && pUnit)
- this->execProc(pUnit);
- };
- private:
- UnitFilter condProc;
- UnitProc execProc;
- int runCount;
- };
- // In UnitImpl:
- UnitImpl
- {
- //...
- Vectorset<Event> eventList;
- //...
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement