Adonthell  0.4
time_event.cc
Go to the documentation of this file.
1 /*
2  $Id: time_event.cc,v 1.7 2005/01/18 12:43:53 ksterker Exp $
3 
4  Copyright (C) 2002/2003/2004 Kai Sterker <kaisterker@linuxgames.com>
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  Adonthell is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Adonthell is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Adonthell; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
22 /**
23  * @file time_event.cc
24  *
25  * @author Kai Sterker
26  * @brief Implements the time_event class.
27  */
28 
29 #include "time_event.h"
30 #include "gamedate.h"
31 
32 // create a new time event
33 time_event::time_event (const string & time, bool absolute) : event ()
34 {
35  Repeat = 1;
36  Type = TIME_EVENT;
37  Absolute = absolute;
38  Time = gamedate::parse_time (time);
39  if (!absolute) Time += gamedate::time ();
40 }
41 
42 // specify the interval between two occurances of the event
43 void time_event::set_repeat (const string & interval, s_int32 count)
44 {
45  Interval = gamedate::parse_time (interval);
46  Repeat = count;
47 }
48 
49 // execute the time event
51 {
52  // nothing needs be passed to the script; it can get the
53  // current time from the gametime class if it is needed.
54  switch (Action)
55  {
56  case ACTION_SCRIPT:
57  {
58  Script->run ();
59  break;
60  }
61 
62  case ACTION_PYFUNC:
63  {
65  break;
66  }
67 
68  case ACTION_CPPFUNC:
69  {
70  Callback ();
71  break;
72  }
73 
74  default: break;
75  }
76 
77  // when the script needs be repeated, do so.
78  if (Repeat != 0) Time += Interval;
79 
80  return do_repeat ();
81 }
82 
83 // disable the event temporarily
85 {
86  // save time 'til relative event is raised
87  if (!Absolute) Time -= gamedate::time ();
88 
89  event::pause ();
90 }
91 
92 // enable a previously paused event
94 {
95  // restore alarm time for relative event
96  if (!Absolute) Time += gamedate::time ();
97 
98  event::resume ();
99 }
100 
101 // Save time event to file
103 {
104  // save basic event data first
105  event::put_state (out);
106 
107  // save time event data
108  Time >> out;
109  Interval >> out;
110  Absolute >> out;
111 }
112 
113 // load time event from file
115 {
116  // get basic event data
117  event::get_state (in);
118 
119  // get time event data
120  Time << in;
121  Interval << in;
122  Absolute << in;
123 
124  return true;
125 }