home *** CD-ROM | disk | FTP | other *** search
/ Game.EXE 2002 June / Game.EXE_06_2002.iso / Alawar / src / Pacman.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-26  |  3.1 KB  |  181 lines

  1. #include "Pacman.h"
  2. #include "Level.h"
  3.  
  4. Pacman::Pacman( Scene2D * scene )
  5. :    Creature( 0 ),
  6.     anim( scene, 2, "data/pacman" ),
  7.     life( 3 ),
  8.     score( 0 ),
  9.     state( DEAD )
  10. {
  11.     speed = 2;    //∩ε ΦΣσσ ²≥ε Γ±σ Φτ ΦφΦ°ΩΦ
  12. }
  13.  
  14. void Pacman::put_on_level( Level * level )
  15. {
  16.     this->level = level;
  17.     if( level )
  18.         change_state( CREATING );
  19. }
  20.  
  21. void Pacman::change_state( state_enum new_state )
  22. {
  23. //    switch( state ) // ╧εΩΦφ≤≥ⁿ ∩≡σΣ√Σ≤∙σσ ±ε±≥ε φΦσ
  24. //    {}
  25.     state = new_state;
  26.     switch( state ) // ╟αΘ≥Φ Γ φεΓεσ ±ε±≥ε φΦσ
  27.     {
  28.     case CREATING:
  29.         level->move( this, "pacman_start" );
  30.         anim.start( 5, false );
  31.         anim.move( get_position().get_x()-anim.get_width()/2, get_position().get_y()-anim.get_height()/2 );
  32.         break;
  33.     case LIVING:
  34.         next_direction = Direction::STAND;
  35.         change_direction( Direction::STAND );
  36.         break;
  37.     case DYING:
  38.         anim.start( 4, false );
  39.         break;
  40.     case DEAD:
  41.         break;
  42.     }
  43. }
  44.  
  45. void Pacman::change_direction( Direction dir )
  46. {
  47.     if ( curr_direction != dir )
  48.     {
  49.         switch( dir.get_dir() )
  50.         {
  51.         case Direction::RIGHT:
  52.             anim.start(0,true);
  53.             break;
  54.         case Direction::LEFT:
  55.             anim.start(2,true);
  56.             break;
  57.         case Direction::UP:
  58.             anim.start(1,true);
  59.             break;
  60.         case Direction::DOWN:
  61.             anim.start(3,true);
  62.             break;
  63.         }
  64.     }
  65.     curr_direction = dir;
  66. }
  67.  
  68. void Pacman::key_down(Direction::Dir dir)
  69. {
  70.     if( state == LIVING )
  71.     {
  72.         next_direction = dir;
  73.     }
  74.     if( curr_direction.is_opposed( next_direction ) )
  75.     {
  76.         change_direction( next_direction );
  77.         pos.swap_dst();
  78.     }
  79. }
  80.  
  81. int Pacman::get_life()const
  82. {
  83.     return life;
  84. }
  85.  
  86. int Pacman::get_score()const
  87. {
  88.     return score;
  89. }
  90.  
  91. bool Pacman::is_alive()const
  92. {
  93.     return state != DEAD;
  94. }
  95.  
  96. void Pacman::process_death(const Creature * cre)
  97. {}
  98.  
  99. bool Pacman::is_solid()const
  100. {
  101.     return state == LIVING;
  102. }
  103.  
  104. void Pacman::process_collision(const Creature * cre)
  105. {
  106.     if ( cre->get_name() == "Dot" )
  107.     {
  108.         ++score;
  109.         return;
  110.     }
  111.     if ( cre->get_name() == "PowerDot" )
  112.     {
  113.         score += 50;
  114.         internal_score_multiplier = 0;
  115.         return;
  116.     }
  117.     if ( cre->get_name() == "Cat" )
  118.     {
  119.         score += ++internal_score_multiplier*200;
  120.         return;
  121.     }
  122.     if ( cre->get_name() == "Cherry" )
  123.     {
  124.         ++life;
  125.         score += 1000;
  126.         return;
  127.     }
  128.     if( cre->get_name() == "Monster" )
  129.     {
  130.         --life;
  131.         change_state( DYING );
  132.     }
  133. }
  134.  
  135. float Pacman::get_size()const
  136. {
  137.     return anim.get_width(0,0);
  138. }
  139.  
  140. String Pacman::get_name()const
  141. {
  142.     return "Pacman";
  143. }
  144.  
  145. void Pacman::life_cycle(float delta_time)
  146. {
  147.     anim.move( get_position().get_x()-anim.get_width()/2, get_position().get_y()-anim.get_height()/2 );
  148.     anim.life_cycle( delta_time );
  149.     switch( state )
  150.     {
  151.     case CREATING:
  152.         if( anim.is_finished() )
  153.             change_state( LIVING );
  154.         break;
  155.     case LIVING:
  156.         if ( pos.go_to( speed * delta_time ) )
  157.         {
  158.             if( level->next_direction( this, next_direction ) )
  159.             {
  160.                 change_direction( next_direction );
  161.             }
  162.             else
  163.                 if( level->next_direction( this, curr_direction ) )
  164.                 {
  165.                     change_direction( curr_direction );
  166.                 }
  167.         }
  168.         break;
  169.     case DYING:
  170.         if( anim.is_finished() )
  171.             if( life > 0 )
  172.                 change_state( CREATING );
  173.             else
  174.                 change_state( DEAD );
  175.         break;
  176.     case DEAD:
  177.         // :(
  178.         break;
  179.     }
  180. }
  181.