home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 06 General Architectures / 04 Christian / world2d.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-09  |  4.0 KB  |  199 lines

  1.  
  2. #include "world2d.h"
  3. #include "gametime.h"
  4.  
  5. #include <assert.h>
  6.  
  7. WorldObj::WorldObj()
  8. {
  9.     m_xdir = 0;
  10.     m_ydir = 0;
  11. }
  12.  
  13. void WorldObj::setWorld ( World2D * world, char * name )
  14. {
  15.     m_name  = name;
  16.     m_world = world;
  17. }
  18.  
  19. // setPos -- sets the world object x,y
  20. void WorldObj::setPos ( int x, int y )
  21. {
  22.     m_pos = m_world->setPos ( x, y, this );   
  23.  
  24.     m_xdist = 0.5f;
  25.     m_ydist = 0.5f;
  26. }
  27.  
  28. // getPos -- gets the xy of the object
  29. void WorldObj::getPos ( int & x, int & y )
  30. {
  31.     x = m_pos->m_x;
  32.     y = m_pos->m_y;
  33. }
  34.  
  35. const char * WorldObj::getWOName ()
  36. {
  37.     return m_name.c_str();
  38. }
  39.  
  40. void WorldObj::move ()
  41. {
  42.     float xdist = (m_speed * GameTime::dt * m_xdir);
  43.     float ydist = (m_speed * GameTime::dt * m_ydir);
  44.  
  45.     m_xdist += xdist;
  46.     m_ydist += ydist;
  47.     
  48.     // see if new world coordinates
  49.     int new_x = m_pos->m_x;
  50.     int new_y = m_pos->m_y;
  51.  
  52.     m_lastx = new_x;
  53.     m_lasty = new_y;
  54.  
  55.     if ( m_xdist < 0.0f )
  56.     {        
  57.         int xdist = 1 + (int)abs(m_xdist);
  58.  
  59.         new_x -= xdist;
  60.         m_xdist = 1.0f + (m_xdist + (float)(xdist-1));
  61.  
  62.     }
  63.     else if ( m_xdist >= 1.0f )
  64.     {
  65.         int xdist = 1 + (int)m_xdist;
  66.         new_x += xdist;
  67.         m_xdist = 0.0f + (m_xdist - (float)(xdist-1));
  68.     }
  69.  
  70.     if ( m_ydist < 0.0f )
  71.     {
  72.         int ydist = 1 + (int)abs(m_ydist);
  73.         new_y -= ydist;
  74.         m_ydist = 1.0f + (m_ydist + (float)(ydist-1));
  75.     }
  76.     else if ( m_ydist >= 1.0f )
  77.     {
  78.         int ydist = 1 + (int)m_ydist;
  79.         new_y += ydist;
  80.         m_ydist = 0.0f + (m_ydist - (float)(ydist-1));
  81.     }
  82.  
  83.     if ( new_x < 0 )
  84.         new_x = m_world->m_width + new_x;
  85.     else if ( new_x >= m_world->m_width )
  86.         new_x = 0 + (new_x - m_world->m_width);
  87.  
  88.     if ( new_y < 0 )
  89.         new_y = m_world->m_length + new_y;
  90.     else if ( new_y >= m_world->m_length )
  91.         new_y = 0 + (new_y - m_world->m_length );
  92.  
  93.     // extra clamp in case time is really great
  94.     // like in the case of debugging
  95.     if ( new_y < 0 ) new_y = 0;
  96.     else if ( new_y >= m_world->m_length ) new_y = m_world->m_length - 1;
  97.     if ( new_x < 0 ) new_x = 0;
  98.     else if ( new_x >= m_world->m_width ) new_x = m_world->m_width - 1;
  99.  
  100.     // if new xy position
  101.     if ( new_x != m_pos->m_x || new_y != m_pos->m_y )
  102.     {
  103.         // null out old position
  104.         m_pos->setObject ( NULL );
  105.  
  106.         // set new position
  107.         m_pos = m_world->setPos ( new_x, new_y, this );
  108.     }
  109. }
  110.  
  111. bool WorldObj::inNewPos ()
  112. {
  113.     if ( m_lastx != m_pos->m_x || m_lasty != m_pos->m_y )
  114.         return true;
  115.     else
  116.         return false;
  117. }
  118.  
  119. WorldPos::WorldPos()
  120. {
  121.     m_id   = -1;
  122.     m_obj  = NULL;
  123. }
  124.  
  125. void WorldPos::setObject  ( WorldObj * obj )
  126. {
  127.     m_obj = obj;
  128. }
  129.  
  130. WorldObj * WorldPos::getObj ()
  131. {
  132.     return m_obj;
  133. }
  134.  
  135. World2D::World2D( int width, int length, float tileDist )
  136. {
  137.     m_width  = width;
  138.     m_length = length;
  139.     m_tileDist = tileDist;
  140.  
  141.     m_posArray = (WorldPos **)malloc ( sizeof ( WorldPos * ) * length * width );
  142.  
  143.     int i, num = m_length * m_width;
  144.  
  145.     int x = 0;
  146.     int y = 0;
  147.  
  148.     for ( i=0; i<num; i++ )
  149.     {
  150.         m_posArray[i] = new WorldPos;
  151.  
  152.         m_posArray[i]->m_id   = i;
  153.         m_posArray[i]->m_obj  = NULL;
  154.  
  155.  
  156.         m_posArray[i]->m_x = x;
  157.         m_posArray[i]->m_y = y;
  158.  
  159.         ++x;
  160.         if ( x >= m_width )
  161.         {
  162.             x = 0;
  163.             ++y;
  164.         }
  165.     }
  166. }
  167.  
  168. World2D::~World2D()
  169. {
  170.     int i, num = m_length * m_width;
  171.  
  172.     for ( i=0; i<num; i++ )
  173.     {
  174.         delete m_posArray[i];
  175.     }
  176.  
  177.     free ( m_posArray );
  178. }
  179.  
  180. WorldPos * World2D::setPos ( int x, int y, WorldObj * obj )
  181. {
  182.     // get the pos
  183.     WorldPos * pos = getPos ( x, y );
  184.     
  185.     pos->setObject ( obj );
  186.  
  187.     return pos;
  188. }
  189.  
  190. WorldPos * World2D::getPos ( int x, int y )
  191. {
  192.     assert ( x >= 0 && x <= m_width );
  193.     assert ( y >= 0 && y <= m_length );
  194.  
  195.     int idx = y * m_width + x;
  196.  
  197.     return m_posArray[idx];
  198. }
  199.