Adonthell 0.4

time_event_handler.cc

Go to the documentation of this file.
00001 /*
00002    $Id: time_event_handler.cc,v 1.6 2003/01/20 00:15:41 ksterker Exp $
00003 
00004    Copyright (C) 2002/2003 Kai Sterker <kaisterker@linuxgames.com>
00005    Part of the Adonthell Project http://adonthell.linuxgames.com
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 /**
00016  * @file time_event_handler.cc
00017  *
00018  * @author Kai Sterker
00019  * @brief Implements the time_event_handler class.
00020  */
00021 
00022 #include <algorithm>
00023 #include "gamedate.h"
00024 #include "time_event.h"
00025 #include "time_event_handler.h"
00026 
00027 
00028 // See whether a matching event is registered and execute the
00029 // according script(s) 
00030 void time_event_handler::raise_event (const event * e)
00031 {
00032     s_int32 repeat;
00033     event *evt;
00034 
00035     // As long as matching events are in the list
00036     while (!Events.empty () && Events.front ()->equals (e))
00037     {
00038         evt = Events.front ();
00039 
00040         // we remove the event in any case, as it needs to be
00041         // re-registered at a new position if it repeats
00042         Events.erase (Events.begin ());
00043         evt->set_registered (false);
00044 
00045         // events that don't repeat are destroyed automatically
00046         repeat = evt->execute (e);
00047 
00048         // re-register event if it needs be repeated
00049         if (repeat) register_event (evt);
00050         else delete evt;
00051     }
00052     
00053     return;
00054 }
00055 
00056 // Unregister an event
00057 void time_event_handler::remove_event (event *e)
00058 {
00059     vector<event*>::iterator i;
00060 
00061     // Search for the event we want to remove
00062     i = find (Events.begin (), Events.end (), e);
00063 
00064     // found? -> get rid of it :)
00065     if (i != Events.end ()) Events.erase (i);
00066 }
00067 
00068 // register an event with the handler
00069 void time_event_handler::register_event (event *e)
00070 {
00071     vector<event*>::iterator i = Events.begin ();
00072 
00073     // search for the proper place to insert new event
00074     while (i != Events.end ())
00075     {
00076         // skip events that are raised earlier than e
00077         if (((time_event *) e)->time () <= ((time_event *) (*i))->time ()) break;
00078         i++;
00079     }
00080 
00081     Events.insert (i, e);
00082 }