home *** CD-ROM | disk | FTP | other *** search
/ Game.EXE 2002 May / Game.EXE_05_2002.iso / Alawar / src / Level.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-04  |  5.7 KB  |  244 lines

  1. #include "Level.h"
  2. #include "CellPass.h"
  3. #include "CellWall.h"
  4. #include "Dot.h"
  5. #include "PowerDot.h"
  6. #include "Monster.h"
  7. #include "Cherry.h"
  8. #include <math.h>
  9.  
  10. Level::Level(Scene2D * scene)
  11. :    scene( scene ),
  12.     wall_size_anim( scene, 1, "data/pass" )
  13. {
  14.     load_level( "" );
  15.     creatures.push_back( new Monster( scene, this ) );
  16. }
  17.  
  18. Level::~Level()
  19. {
  20.     Creatures::iterator crit = creatures.begin();
  21.     for( ; crit != creatures.end(); ++crit )
  22.     {
  23.         delete (*crit); (*crit) = 0;
  24.     }
  25.  
  26.     Array2D< Cell * >::iterator ait = cells.begin();
  27.     for( ; ait != cells.end(); ++ait )
  28.     {
  29.         delete (*ait); (*ait) = 0;
  30.     }
  31.  
  32. }
  33.  
  34. static void intersect( Creatures * a, Creatures * b )
  35. {
  36.     Creatures::iterator ait = a->begin();
  37.     for( ; ait != a->end(); ++ait )
  38.     if( (*ait)->is_solid() )
  39.     {
  40.         Creatures::iterator bit = b->begin();
  41.         for( ; bit != b->end(); ++bit )
  42.         if( (*bit)->is_solid() )
  43.         {
  44.             const float a_size = (*ait)->get_size();
  45.             const float b_size = (*bit)->get_size();
  46.             const Coord a_pos = Coord( (*ait)->get_position().get_x(), (*ait)->get_position().get_y() );
  47.             const Coord b_pos = Coord( (*bit)->get_position().get_x(), (*bit)->get_position().get_y() );
  48.             if (( fabs( a_pos.x - b_pos.x ) < ( a_size + b_size )/4 ) && ( fabs( a_pos.y - b_pos.y ) < ( a_size + b_size )/4 ))
  49.             {
  50.                 (*bit)->process_collision( *ait );
  51.                 (*ait)->process_collision( *bit );
  52.             }
  53.         }
  54.     }
  55. }
  56.  
  57. void Level::move( Creature * creature, const String & where )
  58. {
  59.     if ( where == "pacman_start" )
  60.     {
  61.         creature->pos.src = pacman_start;
  62.         creature->pos.dst = pacman_start;
  63.         creature->pos.koef = 0;
  64.         creature->pos.cell_size = get_cell_size();
  65.     }
  66.     if ( where == "monster_start" )
  67.     {
  68.         creature->pos.src = monster_start;
  69.         creature->pos.dst = monster_start;
  70.         creature->pos.koef = 0;
  71.         creature->pos.cell_size = get_cell_size();
  72.     }
  73. }
  74.  
  75. void Level::add_external_creature(Creature * cre)
  76. {
  77.     external_creatures.push_back( cre );
  78. }
  79.  
  80. void Level::life_cycle(float delta_time)
  81. {
  82.     creatures.life_cycle( delta_time );
  83.     external_creatures.life_cycle( delta_time );
  84.  
  85.     intersect( &creatures, &creatures );
  86.     intersect( &creatures, &external_creatures );
  87.     intersect( &external_creatures, &creatures );
  88.     intersect( &external_creatures, &external_creatures );
  89.  
  90.     Creatures::iterator it = creatures.begin();
  91.     while( it != creatures.end() )
  92.         if( !(*it)->is_alive() )
  93.         {
  94.             creatures.process_death( (*it ) );
  95.             external_creatures.process_death( (*it ) );
  96.             delete (*it);
  97.             it = creatures.erase( it );
  98.         }
  99.         else
  100.             ++it;
  101. }
  102.  
  103. bool Level::next_direction(Creature * p, Direction direction)
  104. {
  105.     const int cell_size = get_cell_size();
  106.  
  107.     int x = p->get_position().dst.x / cell_size;
  108.     int y = p->get_position().dst.y / cell_size;
  109.  
  110.     if ( direction == Direction::RIGHT )
  111.     {
  112.         if ( cells[x+1][y]->can_pass( p ) )
  113.         {
  114.             p->pos.src = p->pos.dst;
  115.             p->pos.dst.x = (x+1)*cell_size;
  116.             p->pos.dst.y = y*cell_size;
  117.             p->pos.koef = 0;
  118.             p->pos.cell_size = cell_size;
  119.             return true;
  120.         }
  121.     }
  122.     if ( direction == Direction::LEFT )
  123.     {
  124.         if ( cells[x-1][y]->can_pass( p ) )
  125.         {
  126.             p->pos.src = p->pos.dst;
  127.             p->pos.dst.x = (x-1)*cell_size;
  128.             p->pos.dst.y = y*cell_size;
  129.             p->pos.koef = 0;
  130.             p->pos.cell_size = cell_size;
  131.             return true;
  132.         }
  133.     }
  134.     if ( direction == Direction::UP )
  135.     {
  136.         if ( cells[x][y-1]->can_pass( p ) )
  137.         {
  138.             p->pos.src = p->pos.dst;
  139.             p->pos.dst.x = x*cell_size;
  140.             p->pos.dst.y = (y-1)*cell_size;
  141.             p->pos.koef = 0;
  142.             return true;
  143.         }
  144.     }
  145.     if ( direction == Direction::DOWN )
  146.     {
  147.         if ( cells[x][y+1]->can_pass( p ) )
  148.         {
  149.             p->pos.src = p->pos.dst;
  150.             p->pos.dst.x = x*cell_size;
  151.             p->pos.dst.y = (y+1)*cell_size;
  152.             p->pos.koef = 0;
  153.             return true;
  154.         }
  155.     }
  156.     return false;
  157. }
  158.  
  159. int Level::get_cell_size()const
  160. {
  161.     return wall_size_anim.get_width(0,0);
  162. }
  163.  
  164. bool Level::load_level( const char * level_name )
  165. {
  166.     const int cell_size = get_cell_size();
  167.  
  168.     FILE * file = fopen( "level.txt", "rt" );
  169.     if ( !file )
  170.         return false;
  171.  
  172.     int cx(0), cy(0);
  173.     fscanf( file, "%d %d", &cx, &cy );
  174.     cells = Array2D<Cell *>( cx, cy, 0 );
  175.  
  176.     Array2D<int> temp_field( cx, cy );
  177.  
  178.     int y;
  179.     for( y = 0; y < cy; ++y )
  180.         for( int x = 0; x < cx; ++x )
  181.         {
  182.             int val( '\n' );
  183.             while( val == '\n' )
  184.                 val = fgetc( file );
  185.             if ( val == -1 )
  186.                 return false;
  187.             temp_field[x][y] = val;
  188.         }
  189.  
  190.     for( y = 0; y < cy; ++y )
  191.         for( int x = 0; x < cx; ++x )
  192.         {
  193.             if ( temp_field[x][y] == 'W' )
  194.             {
  195.                 int mask(0);
  196.                 if ( x != cx-1 && temp_field[x+1][y] == 'W' )
  197.                     mask |= Direction::RIGHT;
  198.                 if ( x != 0 && temp_field[x-1][y] == 'W' )
  199.                     mask |= Direction::LEFT;
  200.                 if ( y != cy-1 && temp_field[x][y+1] == 'W' )
  201.                     mask |= Direction::DOWN;
  202.                 if ( y != 0 && temp_field[x][y-1] == 'W' )
  203.                     mask |= Direction::UP;
  204.  
  205.                 cells[x][y] = new CellWall( scene, x, y, mask );
  206.             }
  207.  
  208.             if ( temp_field[x][y] == 'S' )
  209.             {
  210.                 cells[x][y] = new CellPass( scene, x, y );
  211.                 pacman_start = Coord( x*cell_size, y*cell_size );
  212.             }
  213.             if ( temp_field[x][y] == 'M' )
  214.             {
  215.                 cells[x][y] = new CellPass( scene, x, y );
  216.                 monster_start = Coord( x*cell_size, y*cell_size );
  217.             }
  218.             if ( temp_field[x][y] == ' ' )
  219.                 cells[x][y] = new CellPass( scene, x, y );
  220.  
  221.             if ( temp_field[x][y] == '.' )
  222.             {
  223.                 cells[x][y] = new CellPass( scene, x, y );
  224.                 Dot * dot = new Dot( this, scene, x, y );
  225.                 creatures.push_back( dot );
  226.             }
  227.             if ( temp_field[x][y] == '*' )
  228.             {
  229.                 cells[x][y] = new CellPass( scene, x, y );
  230.                 PowerDot * dot = new PowerDot( this, scene, x, y );
  231.                 creatures.push_back( dot );
  232.             }
  233.             if ( temp_field[x][y] == 'C' )
  234.             {
  235.                 cells[x][y] = new CellPass( scene, x, y );
  236.                 Cherry * cherry = new Cherry( this, scene, x, y );
  237.                 creatures.push_back( cherry );
  238.             }
  239.         }
  240.  
  241.     fclose( file );
  242.  
  243.     return true;
  244. }