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

  1. /***************************************************************************
  2.  * spinbox.cpp  -  spinning box
  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/spinbox.h"
  17. #include "../core/obj_manager.h"
  18. #include "../core/framerate.h"
  19. #include "../core/game_core.h"
  20.  
  21. /* *** *** *** *** *** *** *** *** cSpinBox *** *** *** *** *** *** *** *** *** */
  22.  
  23. cSpinBox :: cSpinBox( float x, float y )
  24. : cBaseBox( x, y )
  25. {
  26.     cSpinBox::Init();
  27. }
  28.  
  29. cSpinBox :: cSpinBox( CEGUI::XMLAttributes &attributes )
  30. : cBaseBox()
  31. {
  32.     cSpinBox::Init();
  33.     cSpinBox::Create_from_Stream( attributes );
  34. }
  35.  
  36. cSpinBox :: ~cSpinBox( void )
  37. {
  38.  
  39. }
  40.  
  41. void cSpinBox :: Init( void )
  42. {
  43.     type = TYPE_SPINBOX;
  44.     box_type = type;
  45.     player_range = 5000;
  46.  
  47.     spin_counter = 0;
  48.     spin = 0;
  49.  
  50.     // default is infinite times activate-able
  51.     Set_Useable_Count( -1, 1 );
  52.     // Spinbox Animation
  53.     Set_Animation( "Spin" );
  54.  
  55.     // no animation
  56.     anim_counter_max = 1;
  57.  
  58.     // editor image
  59.     item_image = pVideo->Get_Surface( "game/arrow/small/white/up.png" );
  60.  
  61.     Create_Name();
  62. }
  63.  
  64. cSpinBox *cSpinBox :: Copy( void )
  65. {
  66.     cSpinBox *spinbox = new cSpinBox( startposx, startposy );
  67.     spinbox->Set_Invisible( box_invisible );
  68.     spinbox->Set_Useable_Count( start_useable_count, 1 );
  69.  
  70.     return spinbox;
  71. }
  72.  
  73. void cSpinBox :: Create_from_Stream( CEGUI::XMLAttributes &attributes )
  74. {
  75.     cBaseBox::Create_from_Stream( attributes );
  76. }
  77.  
  78. void cSpinBox :: Save_to_Stream( ofstream &file )
  79. {
  80.     // begin box
  81.     file << "\t<box>" << std::endl;
  82.  
  83.     cBaseBox::Save_to_Stream( file );
  84.  
  85.     // end box
  86.     file << "\t</box>" << std::endl;
  87. }
  88.  
  89. void cSpinBox :: Activate( void )
  90. {
  91.     // already spinning
  92.     if( spin )
  93.     {
  94.         return;
  95.     }
  96.  
  97.     spin = 1;
  98.     Update_Valid_Update();
  99.     // passive box for spinning
  100.     massivetype = MASS_PASSIVE;
  101.  
  102.     counter = static_cast<float>(anim_counter_min);
  103.     spin_counter = 0;
  104.     // set the BaseBox maximum animation image
  105.     anim_counter_max = 6;
  106. }
  107.  
  108. void cSpinBox :: Stop( void )
  109. {
  110.     // already stopped spinning
  111.     if( !spin )
  112.     {
  113.         return;
  114.     }
  115.  
  116.     // disabled image
  117.     if( !useable_count )
  118.     {
  119.         Set_Image( 0, 0, 0 );
  120.     }
  121.     // default image
  122.     else
  123.     {
  124.         Set_Image( 1, 0, 0 );
  125.     }
  126.     // reset
  127.     spin = 0;
  128.     Update_Valid_Update();
  129.     counter = static_cast<float>(anim_counter_min);
  130.     spin_counter = 0;
  131.  
  132.     // back to a massive box
  133.     massivetype = MASS_MASSIVE;
  134.     // set the BaseBox maximum animation image
  135.     anim_counter_max = 1;
  136. }
  137.  
  138. void cSpinBox :: Update( void )
  139. {
  140.     if( !valid_update || !is_Player_range() )
  141.     {
  142.         return;
  143.     }
  144.  
  145.     cBaseBox::Update();
  146.  
  147.     if( spin )
  148.     {
  149.         spin_counter += pFramerate->speedfactor;
  150.  
  151.         // spinning animation finished
  152.         if( counter == 1 )
  153.         {
  154.             // spinning time finished
  155.             if( spin_counter > speedfactor_fps * 5 )
  156.             {
  157.                 // reset spin counter
  158.                 spin_counter = 0;
  159.                 // for collision check and if spinning stopped back to a massive box
  160.                 massivetype = MASS_MASSIVE;
  161.                 // collision data
  162.                 cObjectCollisionType col_list = Collision_Check( &col_rect, COLLIDE_ONLY_CHECK );
  163.                 
  164.                 // check if spinning should continue
  165.                 bool spin_again = 0;
  166.  
  167.                 // colliding with player or enemy
  168.                 if( col_list.find( TYPE_PLAYER ) || col_list.find( ARRAY_ENEMY ) )
  169.                 {
  170.                     spin_again = 1;
  171.                 }
  172.                 // colliding with an active object
  173.                 else if( col_list.find( ARRAY_ACTIVE ) )
  174.                 {
  175.                     cSprite *col_obj = col_list.find( ARRAY_ACTIVE );
  176.                     
  177.                     // check for items
  178.                     if( col_obj->type == TYPE_MUSHROOM_LIVE_1 || col_obj->type == TYPE_MUSHROOM_DEFAULT || 
  179.                         col_obj->type == TYPE_MUSHROOM_POISON || col_obj->type == TYPE_MUSHROOM_BLUE || col_obj->type == TYPE_MUSHROOM_GHOST || 
  180.                         col_obj->type == TYPE_FIREPLANT || col_obj->type == TYPE_JSTAR || col_obj->type == TYPE_FGOLDPIECE )
  181.                     {
  182.                         // found blocking active object
  183.                         spin_again = 1;
  184.                     }
  185.                 }
  186.  
  187.                 // continue spinning
  188.                 if( spin_again )
  189.                 {
  190.                     // spin some time again
  191.                     spin_counter = speedfactor_fps * 2;
  192.                     // passive for spinning
  193.                     massivetype = MASS_PASSIVE;
  194.                 }
  195.                 // finished spinning
  196.                 else
  197.                 {
  198.                     Stop();
  199.                 }
  200.             }
  201.         }
  202.     }
  203. }
  204.  
  205. bool cSpinBox :: Is_Update_Valid( void )
  206. {
  207.     if( spin )
  208.     {
  209.         return 1;
  210.     }
  211.  
  212.     return cBaseBox::Is_Update_Valid();
  213. }
  214.