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

  1. #include "Pacman.h"
  2. #include "Level.h"
  3. #include <winuser.rh>
  4.  
  5. Pacman::Pacman( Scene2D * scene )
  6. :    Creature( 0 ),
  7.     anim( scene, 1, "data/pacman" ),
  8.     life( 3 ),
  9.     score( 0 ),
  10.     state( DEAD )
  11. {
  12.     speed = 2;    //∩ε ΦΣσσ ²≥ε Γ±σ Φτ ΦφΦ°ΩΦ
  13. }
  14.  
  15. void Pacman::put_on_level( Level * level )
  16. {
  17.     this->level = 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_up(int scan_code)
  69. {
  70. }
  71.  
  72. void Pacman::key_down(int scan_code)
  73. {
  74.     if ( state == LIVING )
  75.     switch( scan_code )
  76.     {
  77.     case VK_UP:
  78.         next_direction = Direction::UP;
  79.         break;
  80.     case VK_DOWN:
  81.         next_direction = Direction::DOWN;
  82.         break;
  83.     case VK_LEFT:
  84.         next_direction = Direction::LEFT;
  85.         break;
  86.     case VK_RIGHT:
  87.         next_direction = Direction::RIGHT;
  88.         break;
  89.     }
  90.     if( curr_direction.is_opposed( next_direction ) )
  91.     {
  92.         change_direction( next_direction );
  93.         pos.swap_dst();
  94.     }
  95. }
  96.  
  97. int Pacman::get_life()const
  98. {
  99.     return life;
  100. }
  101.  
  102. int Pacman::get_score()const
  103. {
  104.     return score;
  105. }
  106.  
  107. bool Pacman::is_alive()const
  108. {
  109.     return state != DEAD;
  110. }
  111.  
  112. void Pacman::process_death(const Creature * cre)
  113. {}
  114.  
  115. bool Pacman::is_solid()const
  116. {
  117.     return state == LIVING;
  118. }
  119.  
  120. void Pacman::process_collision(const Creature * cre)
  121. {
  122.     if ( cre->get_name() == "Dot" )
  123.     {
  124.         ++score;
  125.         return;
  126.     }
  127.     if ( cre->get_name() == "PowerDot" )
  128.     {
  129.         score += 50;
  130.         internal_score_multiplier = 0;
  131.         return;
  132.     }
  133.     if ( cre->get_name() == "Cat" )
  134.     {
  135.         score += ++internal_score_multiplier*200;
  136.         return;
  137.     }
  138.     if ( cre->get_name() == "Cherry" )
  139.     {
  140.         ++life;
  141.         score += 1000;
  142.         return;
  143.     }
  144.     if( cre->get_name() == "Monster" )
  145.     {
  146.         --life;
  147.         change_state( DYING );
  148.     }
  149. }
  150.  
  151. float Pacman::get_size()const
  152. {
  153.     return anim.get_width(0,0);
  154. }
  155.  
  156. String Pacman::get_name()const
  157. {
  158.     return "Pacman";
  159. }
  160.  
  161. void Pacman::life_cycle(float delta_time)
  162. {
  163.     anim.move( get_position().get_x()-anim.get_width()/2, get_position().get_y()-anim.get_height()/2 );
  164.     anim.life_cycle( delta_time );
  165.     switch( state )
  166.     {
  167.     case CREATING:
  168.         if( anim.is_finished() )
  169.             change_state( LIVING );
  170.         break;
  171.     case LIVING:
  172.         if ( pos.go_to( speed * delta_time ) )
  173.         {
  174.             if( level->next_direction( this, next_direction ) )
  175.             {
  176.                 change_direction( next_direction );
  177.             }
  178.             else
  179.                 if( level->next_direction( this, curr_direction ) )
  180.                 {
  181.                     change_direction( curr_direction );
  182.                 }
  183.         }
  184.         break;
  185.     case DYING:
  186.         if( anim.is_finished() )
  187.             if( life > 0 )
  188.                 change_state( CREATING );
  189.             else
  190.                 change_state( DEAD );
  191.         break;
  192.     case DEAD:
  193.         // :(
  194.         break;
  195.     }
  196. }
  197.