Advertisement
Guest User

Untitled

a guest
Jul 25th, 2012
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. // NOTE: This is just an implementation of the events idea, actual code is subject to change
  2.  
  3. // In its own "Event" file:
  4. typedef std::function<void(Unit*)> UnitProc;  // possibly its own class for function composition?
  5.  
  6. class Event
  7. {
  8. public:
  9.   // ctor
  10.   Event(const UnitFilter &condition, const UnitProc &action, int timesToRun = -1)
  11.   : condProc(condition)
  12.   , execProc(action)
  13.   , runCount(timesToRun)
  14.   {};
  15.   // check and run
  16.   bool run(Unit *pUnit)
  17.   {
  18.     // Check the condition
  19.     if ( !this->isConditionMet(pUnit) )
  20.       return false;
  21.    
  22.     this->execute(pUnit);
  23.    
  24.     // decrement the run count, negative value is infinite
  25.     if ( this->runCount > 0 )
  26.       this->runCount--;
  27.   };
  28.   // check condition
  29.   bool isConditionMet(Unit *pUnit)
  30.   {
  31.     if ( this->runCount == 0 || pUnit == nullptr)
  32.       return false;
  33.      
  34.     // if cond is invalid, then there is no condition
  35.     return !this->condProc.isValid() || this->condProc(pUnit);
  36.   };
  37.  
  38.   // execute the procedure
  39.   void execute(Unit *pUnit)
  40.   {
  41.     if ( this->execProc && pUnit)
  42.       this->execProc(pUnit);
  43.   };
  44. private:
  45.   UnitFilter  condProc;
  46.   UnitProc    execProc;
  47.   int runCount;
  48. };
  49.  
  50. // In UnitImpl:
  51. UnitImpl
  52. {
  53. //...
  54.   Vectorset<Event> eventList;
  55. //...
  56. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement