home *** CD-ROM | disk | FTP | other *** search
- #include "Level.h"
- #include "CellPass.h"
- #include "CellWall.h"
- #include "Dot.h"
- #include "PowerDot.h"
- #include "Monster.h"
- #include "Cherry.h"
- #include <math.h>
-
- Level::Level(Scene2D * scene)
- : scene( scene ),
- wall_size_anim( scene, 1, "data/pass" )
- {
- load_level( "" );
- creatures.push_back( new Monster( scene, this ) );
- }
-
- Level::~Level()
- {
- Creatures::iterator crit = creatures.begin();
- for( ; crit != creatures.end(); ++crit )
- {
- delete (*crit); (*crit) = 0;
- }
-
- Array2D< Cell * >::iterator ait = cells.begin();
- for( ; ait != cells.end(); ++ait )
- {
- delete (*ait); (*ait) = 0;
- }
-
- }
-
- static void intersect( Creatures * a, Creatures * b )
- {
- Creatures::iterator ait = a->begin();
- for( ; ait != a->end(); ++ait )
- if( (*ait)->is_solid() )
- {
- Creatures::iterator bit = b->begin();
- for( ; bit != b->end(); ++bit )
- if( (*bit)->is_solid() )
- {
- const float a_size = (*ait)->get_size();
- const float b_size = (*bit)->get_size();
- const Coord a_pos = Coord( (*ait)->get_position().get_x(), (*ait)->get_position().get_y() );
- const Coord b_pos = Coord( (*bit)->get_position().get_x(), (*bit)->get_position().get_y() );
- 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 ))
- {
- (*bit)->process_collision( *ait );
- (*ait)->process_collision( *bit );
- }
- }
- }
- }
-
- void Level::move( Creature * creature, const String & where )
- {
- if ( where == "pacman_start" )
- {
- creature->pos.src = pacman_start;
- creature->pos.dst = pacman_start;
- creature->pos.koef = 0;
- creature->pos.cell_size = get_cell_size();
- }
- if ( where == "monster_start" )
- {
- creature->pos.src = monster_start;
- creature->pos.dst = monster_start;
- creature->pos.koef = 0;
- creature->pos.cell_size = get_cell_size();
- }
- }
-
- void Level::add_external_creature(Creature * cre)
- {
- external_creatures.push_back( cre );
- }
-
- void Level::life_cycle(float delta_time)
- {
- creatures.life_cycle( delta_time );
- external_creatures.life_cycle( delta_time );
-
- intersect( &creatures, &creatures );
- intersect( &creatures, &external_creatures );
- intersect( &external_creatures, &creatures );
- intersect( &external_creatures, &external_creatures );
-
- Creatures::iterator it = creatures.begin();
- while( it != creatures.end() )
- if( !(*it)->is_alive() )
- {
- creatures.process_death( (*it ) );
- external_creatures.process_death( (*it ) );
- delete (*it);
- it = creatures.erase( it );
- }
- else
- ++it;
- }
-
- bool Level::next_direction(Creature * p, Direction direction)
- {
- const int cell_size = get_cell_size();
-
- int x = p->get_position().dst.x / cell_size;
- int y = p->get_position().dst.y / cell_size;
-
- if ( direction == Direction::RIGHT )
- {
- if ( cells[x+1][y]->can_pass( p ) )
- {
- p->pos.src = p->pos.dst;
- p->pos.dst.x = (x+1)*cell_size;
- p->pos.dst.y = y*cell_size;
- p->pos.koef = 0;
- p->pos.cell_size = cell_size;
- return true;
- }
- }
- if ( direction == Direction::LEFT )
- {
- if ( cells[x-1][y]->can_pass( p ) )
- {
- p->pos.src = p->pos.dst;
- p->pos.dst.x = (x-1)*cell_size;
- p->pos.dst.y = y*cell_size;
- p->pos.koef = 0;
- p->pos.cell_size = cell_size;
- return true;
- }
- }
- if ( direction == Direction::UP )
- {
- if ( cells[x][y-1]->can_pass( p ) )
- {
- p->pos.src = p->pos.dst;
- p->pos.dst.x = x*cell_size;
- p->pos.dst.y = (y-1)*cell_size;
- p->pos.koef = 0;
- return true;
- }
- }
- if ( direction == Direction::DOWN )
- {
- if ( cells[x][y+1]->can_pass( p ) )
- {
- p->pos.src = p->pos.dst;
- p->pos.dst.x = x*cell_size;
- p->pos.dst.y = (y+1)*cell_size;
- p->pos.koef = 0;
- return true;
- }
- }
- return false;
- }
-
- int Level::get_cell_size()const
- {
- return wall_size_anim.get_width(0,0);
- }
-
- bool Level::load_level( const char * level_name )
- {
- const int cell_size = get_cell_size();
-
- FILE * file = fopen( "level.txt", "rt" );
- if ( !file )
- return false;
-
- int cx(0), cy(0);
- fscanf( file, "%d %d", &cx, &cy );
- cells = Array2D<Cell *>( cx, cy, 0 );
-
- Array2D<int> temp_field( cx, cy );
-
- int y;
- for( y = 0; y < cy; ++y )
- for( int x = 0; x < cx; ++x )
- {
- int val( '\n' );
- while( val == '\n' )
- val = fgetc( file );
- if ( val == -1 )
- return false;
- temp_field[x][y] = val;
- }
-
- for( y = 0; y < cy; ++y )
- for( int x = 0; x < cx; ++x )
- {
- if ( temp_field[x][y] == 'W' )
- {
- int mask(0);
- if ( x != cx-1 && temp_field[x+1][y] == 'W' )
- mask |= Direction::RIGHT;
- if ( x != 0 && temp_field[x-1][y] == 'W' )
- mask |= Direction::LEFT;
- if ( y != cy-1 && temp_field[x][y+1] == 'W' )
- mask |= Direction::DOWN;
- if ( y != 0 && temp_field[x][y-1] == 'W' )
- mask |= Direction::UP;
-
- cells[x][y] = new CellWall( scene, x, y, mask );
- }
-
- if ( temp_field[x][y] == 'S' )
- {
- cells[x][y] = new CellPass( scene, x, y );
- pacman_start = Coord( x*cell_size, y*cell_size );
- }
- if ( temp_field[x][y] == 'M' )
- {
- cells[x][y] = new CellPass( scene, x, y );
- monster_start = Coord( x*cell_size, y*cell_size );
- }
- if ( temp_field[x][y] == ' ' )
- cells[x][y] = new CellPass( scene, x, y );
-
- if ( temp_field[x][y] == '.' )
- {
- cells[x][y] = new CellPass( scene, x, y );
- Dot * dot = new Dot( this, scene, x, y );
- creatures.push_back( dot );
- }
- if ( temp_field[x][y] == '*' )
- {
- cells[x][y] = new CellPass( scene, x, y );
- PowerDot * dot = new PowerDot( this, scene, x, y );
- creatures.push_back( dot );
- }
- if ( temp_field[x][y] == 'C' )
- {
- cells[x][y] = new CellPass( scene, x, y );
- Cherry * cherry = new Cherry( this, scene, x, y );
- creatures.push_back( cherry );
- }
- }
-
- fclose( file );
-
- return true;
- }