Adonthell 0.4
time_event.cc
Go to the documentation of this file.
00001 /*
00002    $Id: time_event.cc,v 1.7 2005/01/18 12:43:53 ksterker Exp $
00003 
00004    Copyright (C) 2002/2003/2004 Kai Sterker <kaisterker@linuxgames.com>
00005    Part of the Adonthell Project http://adonthell.linuxgames.com
00006 
00007    Adonthell is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011 
00012    Adonthell is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with Adonthell; if not, write to the Free Software 
00019    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 */
00021 
00022 /**
00023  * @file time_event.cc
00024  *
00025  * @author Kai Sterker
00026  * @brief Implements the time_event class.
00027  */
00028 
00029 #include "time_event.h"
00030 #include "gamedate.h"
00031 
00032 // create a new time event
00033 time_event::time_event (const string & time, bool absolute) : event ()
00034 {
00035     Repeat = 1;
00036     Type = TIME_EVENT;
00037     Absolute = absolute;
00038     Time = gamedate::parse_time (time);
00039     if (!absolute) Time += gamedate::time ();
00040 }
00041 
00042 // specify the interval between two occurances of the event
00043 void time_event::set_repeat (const string & interval, s_int32 count)
00044 {
00045     Interval = gamedate::parse_time (interval);
00046     Repeat = count;
00047 }
00048 
00049 // execute the time event
00050 s_int32 time_event::execute (const event * evnt)
00051 {
00052     // nothing needs be passed to the script; it can get the
00053     // current time from the gametime class if it is needed.
00054     switch (Action)
00055     {
00056         case ACTION_SCRIPT:
00057         {
00058             Script->run ();
00059             break;
00060         }
00061         
00062         case ACTION_PYFUNC:
00063         {
00064             PyFunc->callback_func0 ();
00065             break;
00066         }
00067         
00068         case ACTION_CPPFUNC:
00069         {
00070             Callback ();
00071             break;
00072         }
00073         
00074         default: break;
00075     }
00076     
00077     // when the script needs be repeated, do so.
00078     if (Repeat != 0) Time += Interval;
00079     
00080     return do_repeat ();
00081 }
00082 
00083 // disable the event temporarily
00084 void time_event::pause ()
00085 {
00086     // save time 'til relative event is raised
00087     if (!Absolute) Time -= gamedate::time ();
00088 
00089     event::pause ();
00090 }
00091 
00092 // enable a previously paused event
00093 void time_event::resume ()
00094 {
00095     // restore alarm time for relative event
00096     if (!Absolute) Time += gamedate::time ();
00097 
00098     event::resume ();
00099 }
00100 
00101 // Save time event to file
00102 void time_event::put_state (ogzstream& out) const
00103 {
00104     // save basic event data first
00105     event::put_state (out);
00106     
00107     // save time event data
00108     Time >> out;
00109     Interval >> out;
00110     Absolute >> out;
00111 }
00112 
00113 // load time event from file
00114 bool time_event::get_state (igzstream& in)
00115 {
00116     // get basic event data
00117     event::get_state (in);
00118     
00119     // get time event data
00120     Time << in;
00121     Interval << in;
00122     Absolute << in;
00123     
00124     return true;
00125 }