Adonthell  0.4
mapobject.cc
Go to the documentation of this file.
1 /*
2  $Id: mapobject.cc,v 1.6 2002/06/28 12:15:20 gnurou Exp $
3 
4  Copyright (C) 1999/2000/2001 Alexandre Courbot
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 
16 
17 /**
18  * @file mapobject.cc
19  *
20  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
21  * @brief Defines the mapobject class.
22  */
23 
24 
25 #include "mapobject.h"
26 
27 using namespace std;
28 
29 
30 // Public methods.
31 
32 
34 {
35  clear ();
36 }
37 
39 {
40  clear ();
41 }
42 
44 {
45  vector <animation *>::iterator i;
46 
47  for (i = anim.begin (); i != anim.end (); i++)
48  delete (*i);
49  anim.clear ();
51 }
52 
54 {
55  vector <animation *>::iterator i;
56 
57  for (i = anim.begin (); i != anim.end (); i++)
58  (*i)->update ();
59 
60  return true;
61 }
62 
63 void mapobject::draw (s_int16 x, s_int16 y, const drawing_area * da_opt, surface * target) const
64 {
65  vector <animation *>::iterator i;
66 
67  for (i = anim.begin (); i != anim.end (); i++)
68  (*i)->draw (x, y, da_opt, target);
69 }
70 
72  const drawing_area * da_opt, surface * target) const
73 {
74  draw (x - base_x () * MAPSQUARE_SIZE, y - base_y () * MAPSQUARE_SIZE,
75  da_opt, target);
76 }
77 
79 {
80  u_int16 i;
81  u_int16 nbr_of_parts;
82 
83  if (!fileops::get_version (file, 1, 1, ""))
84  return -1;
85 
86  // Clear everything.
87  clear ();
88 
89  // Read all the animations.
90  nbr_of_parts << file;
91  for (i = 0; i < nbr_of_parts; i++)
92  {
93  anim.push_back (new animation);
94  anim.back ()->get (file);
95  anim.back ()->play ();
96  }
97 
99 
100  return 0;
101 }
102 
103 s_int8 mapobject::load (string fname)
104 {
105  igzstream file;
106  s_int8 retvalue = -1;
107 
108  string fdef = MAPOBJECTS_DIR;
109 
110  fdef += fname;
111 
112  file.open (fdef);
113  if (!file.is_open ())
114  return -1;
115  retvalue = get (file);
116  file.close ();
117  return retvalue;
118 }
119 
121 {
122  u_int16 i;
123 
124  fileops::put_version (file, 1);
125 
126  // Write all the animations.
127  nbr_of_animations () >> file;
128  for (i = 0; i < nbr_of_animations (); i++)
129  {
130  anim[i]->put (file);
131  }
132 
134 
135  return 0;
136 }
137 
138 s_int8 mapobject::save (string fname) const
139 {
140  ogzstream file;
141  s_int8 retvalue = -1;
142 
143  string fdef = MAPOBJECTS_DIR;
144 
145  fdef += fname;
146 
147  file.open (fdef);
148  if (!file.is_open ())
149  return -1;
150  retvalue = put (file);
151  file.close ();
152  return retvalue;
153 }
154 
156 {
157  vector <animation *>::iterator i;
158  if (pos > nbr_of_animations ())
159  return -2;
160  i = anim.begin ();
161  while (pos--)
162  i++;
163  anim.insert (i, an);
164  an->play ();
165  return 0;
166 }
167 
169 {
170  vector <animation *>::iterator i;
171 
172  if (pos > nbr_of_animations () - 1)
173  return -2;
174  i = anim.begin ();
175  while (pos--)
176  i++;
177  anim.erase (i);
178  return 0;
179 }
180 
182 {
183  // Clear everything.
184  clear ();
185 
186  // Copy the area.
188 
189  // Copy all animations.
190  vector <animation *>::iterator it;
191  for (it = ((mapobject&) src).anim.begin (); it != ((mapobject&) src).anim.end (); it++)
192  {
193  animation * an = new animation;
194  *an = *(*it);
196  }
197 
198  return *this;
199 }