home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 February / maximum-cd-2009-02.iso / DiscContents / SMC_1.6_win32.exe / src / objects / star.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2008-06-27  |  4.4 KB  |  211 lines

  1. /***************************************************************************
  2.  * star.cpp  -  jumping star class
  3.  *
  4.  * Copyright (C) 2006 - 2008 Florian Richter
  5.  ***************************************************************************/
  6. /*
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 3 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  14. */
  15.  
  16. #include "../objects/star.h"
  17. #include "../player/player.h"
  18. #include "../core/framerate.h"
  19. #include "../video/animation.h"
  20. #include "../core/math/utilities.h"
  21. #include "../core/i18n.h"
  22.  
  23. /* *** *** *** *** *** *** cjStar *** *** *** *** *** *** *** *** *** *** *** */
  24.  
  25. cjStar :: cjStar( float x, float y )
  26. : cPowerUp( x, y )
  27. {
  28.     type = TYPE_JSTAR;
  29.     massivetype = MASS_PASSIVE;
  30.  
  31.     images.push_back( pVideo->Get_Surface( "game/items/star.png" ) );
  32.  
  33.     anim_counter = 0;
  34.  
  35.     glim_mod = 1;
  36.     glim_counter = 0;
  37.  
  38.     velx = 5;
  39.  
  40.     Set_Image( 0, 1, 0 );
  41.  
  42.     name = _("Star");
  43. }
  44.  
  45. cjStar :: ~cjStar( void )
  46. {
  47.     //
  48. }
  49.  
  50. cjStar *cjStar :: Copy( void )
  51. {
  52.     cjStar *star = new cjStar( startposx, startposy );
  53.  
  54.     return star;
  55. }
  56.  
  57. void cjStar :: Activate( void )
  58. {
  59.     // animation
  60.     Generate_Particles( posx + col_rect.w * 0.5f, posy + col_rect.h * 0.5f, 20 );
  61.  
  62.     // activate star
  63.     pPlayer->Get_Item( TYPE_JSTAR );
  64.  
  65.     // if spawned destroy
  66.     if( spawned )
  67.     {
  68.         Destroy();
  69.     }
  70.     // hide
  71.     else
  72.     {
  73.         Set_Visible( 0 );
  74.     }
  75. }
  76.  
  77. void cjStar :: Update( void )
  78. {
  79.     if( !valid_update || !is_Player_range() )
  80.     {
  81.         return;
  82.     }
  83.  
  84.     if( Col_Box( &col_rect, &pPlayer->col_rect ) )
  85.     {
  86.         Activate();
  87.         return;
  88.     }
  89.  
  90.     // Add Gravitation
  91.     if( vely < 25 )
  92.     {
  93.         Add_Velocity( 0, 1.8f );
  94.     }
  95.     
  96.     // rotate
  97.     if( vely < 0 )
  98.     {
  99.         Add_Rotation_Z( ( 5 - ( vely / 2.5f ) ) * pFramerate->speedfactor );
  100.     }
  101.     // rotate back to 0 if falling
  102.     else
  103.     {
  104.         if( rotz > 5 && rotz <= 175 )
  105.         {
  106.             Add_Rotation_Z( ( 5 - ( vely / 1.2f ) ) * pFramerate->speedfactor );
  107.         }
  108.         else if( rotz < 355 && rotz > 185 )
  109.         {
  110.             Add_Rotation_Z( ( -5 + ( vely / 1.2f ) ) * pFramerate->speedfactor );
  111.         }
  112.     }
  113.  
  114.     // small stars
  115.     anim_counter += 1.1f * pFramerate->speedfactor;
  116.  
  117.     while( anim_counter > 1 )
  118.     {
  119.         Generate_Particles();
  120.         anim_counter--;
  121.     }
  122.  
  123.     // glim animation
  124.     if( glim_mod )
  125.     {
  126.         glim_counter += pFramerate->speedfactor * 0.2f;
  127.  
  128.         if( glim_counter > 1 ) 
  129.         {
  130.             glim_counter = 1;
  131.             glim_mod = 0;
  132.         }
  133.     }
  134.     else
  135.     {
  136.         glim_counter -= pFramerate->speedfactor * 0.2f;
  137.  
  138.         if( glim_counter < 0 ) 
  139.         {
  140.             glim_counter = 0;
  141.             glim_mod = 1;
  142.         }
  143.     }
  144. }
  145.  
  146. void cjStar :: Draw( cSurfaceRequest *request /* = NULL */ )
  147. {
  148.     if( !valid_draw )
  149.     {
  150.         return;
  151.     }
  152.  
  153.     Set_Color_Combine( glim_counter / 0.8f, glim_counter / 0.9f, glim_counter, GL_ADD );
  154.  
  155.     cPowerUp::Draw();
  156. }
  157.  
  158. void cjStar :: Generate_Particles( float x /* = 0 */, float y /* = 0 */, unsigned int quota /* = 2 */ )
  159. {
  160.     if( x == 0 )
  161.     {
  162.         x = posx + Get_Random_Float( 0, col_rect.w * 0.9f );
  163.     }
  164.  
  165.     if( y == 0 )
  166.     {
  167.         y = posy + Get_Random_Float( 0, col_rect.h * 0.9f );
  168.     }
  169.  
  170.     // set particle color
  171.     Color particle_color = orange;
  172.     particle_color.green += static_cast<Uint8>( glim_counter / 5 );
  173.     particle_color.blue += static_cast<Uint8>( glim_counter / 1.5f );
  174.  
  175.     // create particles
  176.     cParticle_Emitter *anim = new cParticle_Emitter();
  177.     anim->Set_Pos( x, y );
  178.     anim->Set_Quota( quota );
  179.     anim->Set_Image( pVideo->Get_Surface( "animation/particles/star.png" ) );
  180.     anim->Set_Time_to_Live( 0.4f );
  181.     anim->Set_Fading_Alpha( 1 );
  182.     anim->Set_Fading_Size( 1 );
  183.     anim->Set_Speed( 1.5f, 0.5f );
  184.     anim->Set_Scale( 0.15f );
  185.     anim->Set_Color( particle_color );
  186.     anim->Set_Blending( BLEND_ADD );
  187.     anim->Set_Const_Rotation_Z( -5, 10 );
  188.     pAnimation_Manager->Add( anim );
  189. }
  190.  
  191. void cjStar :: Handle_Collision_Massive( cObjectCollision *collision )
  192. {
  193.     if( collision->direction == DIR_RIGHT || collision->direction == DIR_LEFT )
  194.     {
  195.         velx = -velx;
  196.     }
  197.     else if( collision->direction == DIR_UP )
  198.     {
  199.         vely = -( vely * 0.3f );
  200.     }
  201.     else if( collision->direction == DIR_DOWN )
  202.     {
  203.         if( ground_object )
  204.         {
  205.             Generate_Particles( posx + rect.w / 2, ground_object->posy, 10 );
  206.         }
  207.  
  208.         vely = -25;
  209.     }
  210. }
  211.