• Main Page
  • Related Pages
  • Classes
  • Files
  • File List
  • File Members

mapobject.cc

Go to the documentation of this file.
00001 /*
00002    $Id: mapobject.cc,v 1.6 2002/06/28 12:15:20 gnurou Exp $
00003 
00004    Copyright (C) 1999/2000/2001   Alexandre Courbot
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 
00017 /** 
00018  * @file mapobject.cc
00019  *
00020  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
00021  * @brief Defines the mapobject class.
00022  */
00023 
00024 
00025 #include "mapobject.h"
00026 
00027 using namespace std;  
00028 
00029 
00030 // Public methods.
00031 
00032 
00033 mapobject::mapobject () : mapsquare_walkable_area ()
00034 {
00035     clear (); 
00036 }
00037  
00038 mapobject::~mapobject ()
00039 {
00040     clear ();
00041 }
00042 
00043 void mapobject::clear ()
00044 {
00045     vector <animation *>::iterator i; 
00046     
00047     for (i = anim.begin (); i != anim.end (); i++)
00048         delete (*i);     
00049     anim.clear ();
00050     mapsquare_walkable_area::clear (); 
00051 }   
00052 
00053 bool mapobject::update ()
00054 {
00055     vector <animation *>::iterator i; 
00056     
00057     for (i = anim.begin (); i != anim.end (); i++)
00058         (*i)->update ();
00059 
00060     return true; 
00061 }
00062 
00063 void mapobject::draw (s_int16 x, s_int16 y, const drawing_area * da_opt, surface * target) const
00064 {
00065     vector <animation *>::iterator i; 
00066     
00067     for (i = anim.begin (); i != anim.end (); i++)
00068         (*i)->draw (x, y, da_opt, target);
00069 }
00070 
00071 void mapobject::draw_from_base (s_int16 x, s_int16 y,
00072                                 const drawing_area * da_opt, surface * target) const
00073 {
00074     draw (x - base_x () * MAPSQUARE_SIZE, y - base_y () * MAPSQUARE_SIZE,
00075           da_opt, target);
00076 }
00077 
00078 s_int8 mapobject::get (igzstream & file)
00079 {
00080     u_int16 i;
00081     u_int16 nbr_of_parts;
00082     
00083     if (!fileops::get_version (file, 1, 1, ""))
00084         return -1;
00085     
00086     // Clear everything.
00087     clear ();
00088     
00089     // Read all the animations.
00090     nbr_of_parts << file;
00091     for (i = 0; i < nbr_of_parts; i++)
00092     {
00093         anim.push_back (new animation);
00094         anim.back ()->get (file);
00095         anim.back ()->play ();
00096     }
00097 
00098     mapsquare_walkable_area::get (file); 
00099     
00100     return 0;
00101 }
00102 
00103 s_int8 mapobject::load (string fname)
00104 {
00105     igzstream file;
00106     s_int8 retvalue = -1;
00107 
00108     string fdef = MAPOBJECTS_DIR;
00109 
00110     fdef += fname;
00111 
00112     file.open (fdef);
00113     if (!file.is_open ())
00114         return -1;
00115     retvalue = get (file);
00116     file.close ();
00117     return retvalue;
00118 }
00119 
00120 s_int8 mapobject::put (ogzstream & file) const
00121 {
00122     u_int16 i;
00123     
00124     fileops::put_version (file, 1); 
00125 
00126     // Write all the animations.
00127     nbr_of_animations () >> file;
00128     for (i = 0; i < nbr_of_animations (); i++)
00129     {
00130         anim[i]->put (file);
00131     }
00132 
00133     mapsquare_walkable_area::put (file); 
00134     
00135     return 0;
00136 }
00137 
00138 s_int8 mapobject::save (string fname) const
00139 {
00140     ogzstream file;
00141     s_int8 retvalue = -1;
00142 
00143     string fdef = MAPOBJECTS_DIR;
00144 
00145     fdef += fname;
00146 
00147     file.open (fdef);
00148     if (!file.is_open ())
00149         return -1;
00150     retvalue = put (file);
00151     file.close ();
00152     return retvalue;
00153 }
00154 
00155 s_int8 mapobject::insert_animation (animation * an, u_int16 pos)
00156 {
00157     vector <animation *>::iterator i;
00158     if (pos > nbr_of_animations ())
00159         return -2;
00160     i = anim.begin ();
00161     while (pos--)
00162         i++;
00163     anim.insert (i, an);
00164     an->play ();
00165     return 0;
00166 }
00167 
00168 s_int8 mapobject::delete_animation (u_int16 pos)
00169 {
00170     vector <animation *>::iterator i;
00171 
00172     if (pos > nbr_of_animations () - 1)
00173         return -2;
00174     i = anim.begin ();
00175     while (pos--)
00176         i++;
00177     anim.erase (i);
00178     return 0;
00179 }
00180   
00181 mapobject & mapobject::operator = (const mapobject & src)
00182 {
00183     // Clear everything.
00184     clear (); 
00185 
00186     // Copy the area.
00187     (mapsquare_walkable_area&) (*this) = (mapsquare_walkable_area&) src; 
00188 
00189     // Copy all animations.
00190     vector <animation *>::iterator it;
00191     for (it = ((mapobject&) src).anim.begin (); it != ((mapobject&) src).anim.end (); it++)
00192     {
00193         animation * an = new animation;
00194         *an = *(*it);
00195         insert_animation (an, nbr_of_animations ()); 
00196     }
00197     
00198     return *this;
00199 }

Generated on Mon Sep 12 2011 for Adonthell by  doxygen 1.7.1