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

  1. /***************************************************************************
  2.  * goldpiece.cpp  -  goldpiece class
  3.  *
  4.  * Copyright (C) 2003 - 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/goldpiece.h"
  17. #include "../core/game_core.h"
  18. #include "../player/player.h"
  19. #include "../audio/audio.h"
  20. #include "../core/framerate.h"
  21. #include "../video/animation.h"
  22. #include "../gui/hud.h"
  23. #include "../user/savegame.h"
  24. #include "../core/math/utilities.h"
  25. #include "../core/i18n.h"
  26.  
  27. /* *** *** *** *** *** *** cGoldpiece *** *** *** *** *** *** *** *** *** *** *** */
  28.  
  29. cGoldpiece :: cGoldpiece( float x, float y )
  30. : cImageObjectSprite( x, y )
  31. {
  32.     cGoldpiece::Init();
  33. }
  34.  
  35. cGoldpiece :: cGoldpiece( CEGUI::XMLAttributes &attributes )
  36. : cImageObjectSprite()
  37. {
  38.     cGoldpiece::Init();
  39.     cGoldpiece::Create_from_Stream( attributes );
  40. }
  41.  
  42. cGoldpiece :: ~cGoldpiece( void )
  43. {
  44.     //
  45. }
  46.  
  47. void cGoldpiece :: Init( void )
  48. {
  49.     sprite_array = ARRAY_ACTIVE;
  50.     massivetype = MASS_PASSIVE;
  51.     type = TYPE_GOLDPIECE;
  52.     posz = 0.041f;
  53.  
  54.     Set_Gold_Color( COL_YELLOW );
  55.  
  56.     // small random counter
  57.     counter = Get_Random_Float( 0, 5 );
  58. }
  59.  
  60. cGoldpiece *cGoldpiece :: Copy( void )
  61. {
  62.     cGoldpiece *goldpiece = new cGoldpiece( startposx, startposy );
  63.     goldpiece->Set_Gold_Color( color_type );
  64.  
  65.     return goldpiece;
  66. }
  67.  
  68. void cGoldpiece :: Create_from_Stream( CEGUI::XMLAttributes &attributes )
  69. {
  70.     // position
  71.     Set_Pos( static_cast<float>(attributes.getValueAsInteger( "posx" )), static_cast<float>(attributes.getValueAsInteger( "posy" )), 1 );
  72.     // gold color
  73.     Set_Gold_Color( Get_Color_Id( attributes.getValueAsString( "color", Get_Color_Name( color_type ) ).c_str() ) );
  74. }
  75.  
  76. void cGoldpiece :: Save_to_Stream( ofstream &file )
  77. {
  78.     // begin item
  79.     file << "\t<item>" << std::endl;
  80.  
  81.     // type
  82.     file << "\t\t<Property name=\"type\" value=\"goldpiece\" />" << std::endl;
  83.     // position
  84.     file << "\t\t<Property name=\"posx\" value=\"" << static_cast<int>(startposx) << "\" />" << std::endl;
  85.     file << "\t\t<Property name=\"posy\" value=\"" << static_cast<int>(startposy) << "\" />" << std::endl;
  86.     // color
  87.     file << "\t\t<Property name=\"color\" value=\"" << Get_Color_Name( color_type ) << "\" />" << std::endl;
  88.  
  89.     // end item
  90.     file << "\t</item>" << std::endl;
  91. }
  92.  
  93. void cGoldpiece :: Load_from_Savegame( cSave_Level_Object *save_object )
  94. {
  95.     // visible
  96.     bool save_visible = string_to_int( save_object->Get_Value( "visible" ) ) > 0;
  97.     Set_Visible( save_visible );
  98. }
  99.  
  100. cSave_Level_Object *cGoldpiece :: Save_to_Savegame( void )
  101. {
  102.     // only save if needed
  103.     if( visible )
  104.     {
  105.         return NULL;
  106.     }
  107.  
  108.     cSave_Level_Object *save_object = new cSave_Level_Object();
  109.  
  110.     // default values
  111.     save_object->type = type;
  112.     save_object->properties.push_back( cSave_Level_Object_Property( "posx", int_to_string( static_cast<int>(startposx) ) ) );
  113.     save_object->properties.push_back( cSave_Level_Object_Property( "posy", int_to_string( static_cast<int>(startposy) ) ) );
  114.  
  115.     // visible
  116.     save_object->properties.push_back( cSave_Level_Object_Property( "visible", int_to_string( visible ) ) );
  117.  
  118.     return save_object;
  119. }
  120.  
  121. void cGoldpiece :: Set_Gold_Color( DefaultColor ncolor )
  122. {
  123.     color_type = ncolor;
  124.  
  125.     // clear images
  126.     Clear_Images();
  127.  
  128.     if( color_type == COL_RED )
  129.     {
  130.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/1.png" ) );
  131.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/2.png" ) );
  132.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/3.png" ) );
  133.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/4.png" ) );
  134.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/5.png" ) );
  135.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/6.png" ) );
  136.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/7.png" ) );
  137.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/8.png" ) );
  138.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/9.png" ) );
  139.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/10.png" ) );
  140.  
  141.         name = _("Red Goldpiece");
  142.     }
  143.     // default = yellow
  144.     else
  145.     {
  146.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/1.png" ) );
  147.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/2.png" ) );
  148.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/3.png" ) );
  149.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/4.png" ) );
  150.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/5.png" ) );
  151.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/6.png" ) );
  152.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/7.png" ) );
  153.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/8.png" ) );
  154.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/9.png" ) );
  155.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/10.png" ) );
  156.  
  157.         name = _("Goldpiece");
  158.     }
  159.  
  160.     Set_Image( 0, 1 );
  161. }
  162.  
  163. void cGoldpiece :: Activate( void )
  164. {
  165.     // animation
  166.     cAnimation_Goldpiece *anim = new cAnimation_Goldpiece( posx + ( col_rect.w / 10 ), posy + ( col_rect.h / 10 ) );
  167.  
  168.     // gold
  169.     unsigned int points = 0;
  170.  
  171.     if( color_type == COL_RED )
  172.     {
  173.         golddisplay->Add_Gold( 5 );
  174.         points = 100;
  175.         anim->Set_Color( Color( static_cast<Uint8>(255), 240, 20, 200 ) );
  176.     }
  177.     else
  178.     {
  179.         golddisplay->Add_Gold( 1 );
  180.         points = 5;
  181.     }
  182.  
  183.     pAnimation_Manager->Add( anim );
  184.  
  185.     // if jumping double the points
  186.     if( type == TYPE_JGOLDPIECE )
  187.     {
  188.         points *= 2;
  189.     }
  190.  
  191.     pointsdisplay->Add_Points( points, posx + col_rect.w / 2, posy + 2 );
  192.  
  193.     // if spawned destroy
  194.     if( spawned )
  195.     {
  196.         Destroy();
  197.     }
  198.     // hide
  199.     else
  200.     {
  201.         Set_Visible( 0 );
  202.     }
  203. }
  204.  
  205. void cGoldpiece :: Update( void )
  206. {
  207.     if( !valid_update || !Is_Visible_on_Screen() )
  208.     {
  209.         return;
  210.     }
  211.  
  212.     // todo : move to internal collision
  213.     if( Col_Box( &col_rect, &pPlayer->col_rect ) )
  214.     {
  215.         pAudio->Play_Sound( "item/goldpiece_1.ogg" );
  216.         Activate();
  217.         return;
  218.     }
  219.  
  220.     counter += 0.4f * pFramerate->speedfactor;
  221.  
  222.     if( counter >= 10 )
  223.     {
  224.         counter = 0;
  225.     }
  226.  
  227.     Set_Image( static_cast<int>(counter) );
  228. }
  229.  
  230. void cGoldpiece :: Draw( cSurfaceRequest *request /* = NULL */ )
  231. {
  232.     if( !valid_draw )
  233.     {
  234.         return;
  235.     }
  236.  
  237.     // don't draw in leveleditor if spawned ingame
  238.     if( editor_level_enabled && spawned )
  239.     {
  240.         return;
  241.     }
  242.  
  243.     cImageObjectSprite::Draw( request );
  244. }
  245.  
  246. bool cGoldpiece :: Is_Update_Valid( void )
  247. {
  248.     // if not visible
  249.     if( !visible )
  250.     {
  251.         return 0;
  252.     }
  253.  
  254.     return 1;
  255. }
  256.  
  257. void cGoldpiece :: Check_on_Ground( void )
  258. {
  259.     if( type == TYPE_GOLDPIECE )
  260.     {
  261.         return;
  262.     }
  263.  
  264.     cImageObjectSprite::Check_on_Ground();
  265. }
  266.  
  267. /* *** *** *** *** *** *** cJGoldpiecee *** *** *** *** *** *** *** *** *** *** *** */
  268.  
  269. cJGoldpiece :: cJGoldpiece( float x, float y )
  270. : cGoldpiece( x, y )
  271. {
  272.     type = TYPE_JGOLDPIECE;
  273.     spawned = 1;
  274.  
  275.     Set_Gold_Color( COL_YELLOW );
  276.  
  277.     vely = -18;
  278. }
  279.  
  280. cJGoldpiece :: ~cJGoldpiece( void )
  281. {
  282.     //
  283. }
  284.  
  285. void cJGoldpiece :: Update( void )
  286. {
  287.     if( !visible )
  288.     {
  289.         return;
  290.     }
  291.  
  292.     // add velocity downwards
  293.     if( vely < 8 )
  294.     {
  295.         Add_Velocity( 0, 1.62f );
  296.     }
  297.     // finished animation
  298.     else
  299.     {
  300.         Activate();
  301.     }
  302.  
  303.     counter += pFramerate->speedfactor * 0.5f;
  304.  
  305.     if( counter >= 10 )
  306.     {
  307.         counter = 0;
  308.     }
  309.  
  310.     Set_Image( static_cast<int>(counter) );
  311. }
  312.  
  313. unsigned int cJGoldpiece :: Validate_Collision( cSprite *obj )
  314. {
  315.     return 0;
  316. }
  317.  
  318. /* *** *** *** *** *** *** cFGoldpiecee *** *** *** *** *** *** *** *** *** *** *** */
  319.  
  320. cFGoldpiece :: cFGoldpiece( float x, float y, ObjectDirection dir /* = DIR_NOTHING */ )
  321. : cGoldpiece( x, y )
  322. {
  323.     type = TYPE_FGOLDPIECE;
  324.     spawned = 1;
  325.     player_range = 2000;
  326.     counter = static_cast<float>( rand() % 5 );
  327.  
  328.     // set a random direction
  329.     if( dir != DIR_LEFT && dir != DIR_RIGHT )
  330.     {
  331.         if( rand() % 2 != 1 )
  332.         {
  333.             dir = DIR_LEFT;
  334.         }
  335.         else
  336.         {
  337.             dir = DIR_RIGHT;
  338.         }
  339.     }
  340.  
  341.     direction = dir;
  342.  
  343.     if( direction == DIR_RIGHT )
  344.     {
  345.         velx = 5;
  346.     }
  347.     else
  348.     {
  349.         velx = -5;
  350.     }
  351.  
  352.     Set_Gold_Color( COL_YELLOW );
  353. }
  354.  
  355. cFGoldpiece :: ~cFGoldpiece( void )
  356. {
  357.     //
  358. }
  359.  
  360. void cFGoldpiece :: Set_Gold_Color( DefaultColor ncolor )
  361. {
  362.     color_type = ncolor;
  363.  
  364.     // clear images
  365.     Clear_Images();
  366.  
  367.     if( color_type == COL_RED )
  368.     {
  369.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/1_falling.png" ) );
  370.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/2_falling.png" ) );
  371.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/3_falling.png" ) );
  372.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/4_falling.png" ) );
  373.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/5_falling.png" ) );
  374.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/6_falling.png" ) );
  375.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/7_falling.png" ) );
  376.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/8_falling.png" ) );
  377.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/9_falling.png" ) );
  378.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/red/10_falling.png" ) );
  379.     }
  380.     // default = yellow
  381.     else
  382.     {
  383.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/1_falling.png" ) );
  384.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/2_falling.png" ) );
  385.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/3_falling.png" ) );
  386.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/4_falling.png" ) );
  387.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/5_falling.png" ) );
  388.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/6_falling.png" ) );
  389.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/7_falling.png" ) );
  390.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/8_falling.png" ) );
  391.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/9_falling.png" ) );
  392.         images.push_back( pVideo->Get_Surface( "game/items/goldpiece/yellow/10_falling.png" ) );
  393.     }
  394.  
  395.     Set_Image( 0, 1 );
  396. }
  397.  
  398. void cFGoldpiece :: Update( void )
  399. {
  400.     if( !valid_update || !is_Player_range() )
  401.     {
  402.         return;
  403.     }
  404.  
  405.     if( Col_Box( &col_rect, &pPlayer->col_rect ) )
  406.     {
  407.         pAudio->Play_Sound( "item/goldpiece_1.ogg" );
  408.         Activate();
  409.         return;
  410.     }
  411.  
  412.     // Add Gravitation
  413.     if( !ground_object && vely < 25 )
  414.     {
  415.         Add_Velocity( 0, 1.2f );
  416.     }
  417.  
  418.     counter += 0.5f * pFramerate->speedfactor;
  419.  
  420.     if( counter >= 10 )
  421.     {
  422.         counter = 0;
  423.     }
  424.  
  425.     Set_Image( static_cast<int>(counter) );
  426. }
  427.  
  428. unsigned int cFGoldpiece :: Validate_Collision( cSprite *obj )
  429. {
  430.     // basic validation checking
  431.     int basic_valid = Validate_Collision_Ghost( obj );
  432.  
  433.     // found valid collision
  434.     if( basic_valid > -1 )
  435.     {
  436.         return basic_valid;
  437.     }
  438.  
  439.     if( obj->massivetype == MASS_MASSIVE )
  440.     {
  441.         // ignore player
  442.         if( obj->type == TYPE_PLAYER )
  443.         {
  444.             return 0;
  445.         }
  446.         // ignore enemies
  447.         if( obj->sprite_array == ARRAY_ENEMY )
  448.         {
  449.             return 0;
  450.         }
  451.         // ignore fireballs
  452.         if ( obj->type == TYPE_BALL )
  453.         {
  454.             return 0;
  455.         }
  456.  
  457.         return 2;
  458.     }
  459.     if( obj->massivetype == MASS_HALFMASSIVE )
  460.     {
  461.         // if moving downwards and object is on top
  462.         if( vely >= 0 && Is_on_Top( obj ) )
  463.         {
  464.             return 2;
  465.         }
  466.     }
  467.  
  468.     return 0;
  469. }
  470.  
  471. void cFGoldpiece :: Handle_Collision_Massive( cObjectCollision *collision )
  472. {
  473.     if( collision->direction == DIR_RIGHT || collision->direction == DIR_LEFT )
  474.     {
  475.         Turn_Around( collision->direction );
  476.     }
  477.     else if( collision->direction == DIR_UP )
  478.     {
  479.         vely = -( vely * 0.3f );
  480.     }
  481.     else if( collision->direction == DIR_DOWN )
  482.     {
  483.         // minimal value for a jump
  484.         if( vely > 0.5f )
  485.         {
  486.             vely = -( vely * 0.5f );
  487.  
  488.             // maximum value for a jump
  489.             if( vely > 10 )
  490.             {
  491.                 vely = 10;
  492.             }
  493.         }
  494.         else
  495.         {
  496.             vely = 0;
  497.         }
  498.     }
  499. }
  500.  
  501. void cFGoldpiece :: Handle_Collision_Box( ObjectDirection cdirection, GL_rect *r2 )
  502. {
  503.     // if unsupported collision direction
  504.     if( cdirection != DIR_DOWN && cdirection != DIR_LEFT && cdirection != DIR_RIGHT )
  505.     {
  506.         return;
  507.     }
  508.  
  509.     if( cdirection == DIR_DOWN )
  510.     {
  511.         vely = -30;
  512.  
  513.         // left
  514.         if( posx > r2->x && velx < 0 )
  515.         {
  516.             Turn_Around( DIR_LEFT );
  517.         }
  518.         // right
  519.         else if( posx < r2->x && velx > 0 )
  520.         {
  521.             Turn_Around( DIR_RIGHT );
  522.         }
  523.     }
  524.     else if( cdirection == DIR_LEFT || cdirection == DIR_RIGHT )
  525.     {
  526.         vely = -13;
  527.         Turn_Around( cdirection );
  528.     }
  529.  
  530.     Reset_on_Ground();
  531. }
  532.