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

  1. /***************************************************************************
  2.  * static.cpp  -  static enemy
  3.  *
  4.  * Copyright (C) 2007 - 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 "../enemies/static.h"
  17. #include "../core/game_core.h"
  18. #include "../player/player.h"
  19. #include "../level/level.h"
  20. #include "../gui/hud.h"
  21. #include "../video/gl_surface.h"
  22. #include "../core/sprite_manager.h"
  23. #include "../core/i18n.h"
  24.  
  25. /* *** *** *** *** *** *** cStaticEnemy *** *** *** *** *** *** *** *** *** *** *** */
  26.  
  27. cStaticEnemy :: cStaticEnemy( float x, float y )
  28. : cEnemy( x, y )
  29. {
  30.     cStaticEnemy::Init();
  31. }
  32.  
  33. cStaticEnemy :: cStaticEnemy( CEGUI::XMLAttributes &attributes )
  34. : cEnemy()
  35. {
  36.     cStaticEnemy::Init();
  37.     cStaticEnemy::Create_from_Stream( attributes );
  38. }
  39.  
  40. cStaticEnemy :: ~cStaticEnemy( void )
  41. {
  42.     //
  43. }
  44.  
  45. void cStaticEnemy :: Init( void )
  46. {
  47.     type = TYPE_STATIC_ENEMY;
  48.     posz = 0.094f;
  49.  
  50.     Set_Rotation_Speed( 0 );
  51.     Set_Static_Image( "enemy/static/blocks/spike_1/2_grey.png" );
  52.     Create_Name();
  53. }
  54.  
  55. cStaticEnemy *cStaticEnemy :: Copy( void )
  56. {
  57.     cStaticEnemy *static_enemy = new cStaticEnemy( startposx, startposy );
  58.     static_enemy->Set_Static_Image( img_filename );
  59.     static_enemy->Set_Rotation_Speed( rotation_speed );
  60.     return static_enemy;
  61. }
  62.  
  63. void cStaticEnemy :: Create_from_Stream( CEGUI::XMLAttributes &attributes )
  64. {
  65.     // position
  66.     Set_Pos( static_cast<float>(attributes.getValueAsInteger( "posx" )), static_cast<float>(attributes.getValueAsInteger( "posy" )), 1 );
  67.     // rotation speed
  68.     Set_Rotation_Speed( static_cast<float>( attributes.getValueAsFloat( "rotation_speed", -7.5f ) ) );
  69.     // image
  70.     Set_Static_Image( attributes.getValueAsString( "image", "enemy/static/saw/default.png" ).c_str() );
  71. }
  72.  
  73. void cStaticEnemy :: Save_to_Stream( ofstream &file )
  74. {
  75.     // begin enemy
  76.     file << "\t<enemy>" << std::endl;
  77.  
  78.     // name
  79.     file << "\t\t<Property name=\"type\" value=\"static\" />" << std::endl;
  80.     // position
  81.     file << "\t\t<Property name=\"posx\" value=\"" << static_cast<int>(startposx) << "\" />" << std::endl;
  82.     file << "\t\t<Property name=\"posy\" value=\"" << static_cast<int>(startposy) << "\" />" << std::endl;
  83.     // rotation speed
  84.     file << "\t\t<Property name=\"rotation_speed\" value=\"" << rotation_speed << "\" />" << std::endl;
  85.     // image
  86.     file << "\t\t<Property name=\"image\" value=\"" << img_filename << "\" />" << std::endl;
  87.  
  88.     // end enemy
  89.     file << "\t</enemy>" << std::endl;
  90. }
  91.  
  92. void cStaticEnemy :: Set_Rotation_Speed( float speed )
  93. {
  94.     rotation_speed = speed;
  95. }
  96.  
  97. void cStaticEnemy :: Set_Static_Image( string filename )
  98. {
  99.     if( filename.empty() )
  100.     {
  101.         return;
  102.     }
  103.  
  104.     Clear_Images();
  105.  
  106.     img_filename = filename;
  107.  
  108.     // remove pixmaps dir
  109.     if( img_filename.find( DATA_DIR "/" GAME_PIXMAPS_DIR "/" ) == 0 )
  110.     {
  111.         img_filename.erase( 0, strlen( DATA_DIR "/" GAME_PIXMAPS_DIR "/" ) );
  112.     }
  113.  
  114.     images.push_back( pVideo->Get_Surface( filename ) );
  115.     Set_Image( 0, 1 );
  116.     Create_Name();
  117. }
  118.  
  119. void cStaticEnemy :: DownGrade( bool force /* = 0 */ )
  120. {
  121.     Set_Dead( 1 );
  122.     massivetype = MASS_PASSIVE;
  123.     counter = 0;
  124.     velx = 0;
  125.     vely = 0;
  126.     Set_Scale_Directions( 1, 1, 1, 1 );
  127.  
  128.     // falling death
  129.     Set_Rotation_Z( 180 );
  130. }
  131.  
  132. void cStaticEnemy :: DieStep( void )
  133. {
  134.     counter += pFramerate->speedfactor * 0.1f;
  135.  
  136.     // falling death
  137.  
  138.     // a little bit upwards first
  139.     if( counter < 0.3 )
  140.     {
  141.         Move( 0, -5 );
  142.     }
  143.     // if not below the screen fall
  144.     else if( posy < game_res_h + col_rect.h )
  145.     {
  146.         Move( 0, 20 );
  147.  
  148.         Add_Scale( -pFramerate->speedfactor * 0.01f );
  149.     }
  150.     // if below disable
  151.     else
  152.     {
  153.         rotz = 0;
  154.         Set_Scale( 1 );
  155.         Set_Visible( 0 );
  156.     }
  157. }
  158.  
  159. void cStaticEnemy :: Update( void )
  160. {
  161.     cEnemy::Update();
  162.  
  163.     if( !valid_update || !is_Player_range() )
  164.     {
  165.         return;
  166.     }
  167.  
  168.     if( rotation_speed )
  169.     {
  170.         // update rotation
  171.         Add_Rotation_Z( rotation_speed * pFramerate->speedfactor );
  172.     }
  173. }
  174.  
  175. bool cStaticEnemy :: Is_Update_Valid( void )
  176. {
  177.     if( dead || freeze_counter )
  178.     {
  179.         return 0;
  180.     }
  181.  
  182.     return 1;
  183. }
  184.  
  185. unsigned int cStaticEnemy :: Validate_Collision( cSprite *obj )
  186. {
  187.     if( obj->massivetype == MASS_MASSIVE )
  188.     {
  189.         if( obj->type == TYPE_ROKKO )
  190.         {
  191.             return 0;
  192.         }
  193.         if( obj->type == TYPE_GEE )
  194.         {
  195.             return 0;
  196.         }
  197.         if( obj->type == TYPE_TURTLE_BOSS )
  198.         {
  199.             return 0;
  200.         }
  201.  
  202.         if( obj->sprite_array == ARRAY_ENEMY )
  203.         {
  204.             return 1;
  205.         }
  206.  
  207.         return 2;
  208.     }
  209.  
  210.     return 0;
  211. }
  212.  
  213. void cStaticEnemy :: Handle_Collision_Player( cObjectCollision *collision )
  214. {
  215.     pPlayer->DownGrade();
  216. }
  217.  
  218. void cStaticEnemy :: Handle_Collision_Enemy( cObjectCollision *collision )
  219. {
  220.     // invalid
  221.     if( collision->number < 0 )
  222.     {
  223.         return;
  224.     }
  225.  
  226.     cEnemy *enemy = static_cast<cEnemy *>(pActive_Sprite_Manager->Get_Pointer( collision->number ));
  227.  
  228.     // already dead
  229.     if( enemy->dead )
  230.     {
  231.         return;
  232.     }
  233.  
  234.     // kill enemy
  235.     pAudio->Play_Sound( enemy->kill_sound );
  236.     pointsdisplay->Add_Points( enemy->kill_points, posx + image->w / 3, posy - 5, "", static_cast<Uint8>(255), 1 );
  237.     enemy->DownGrade( 1 );
  238. }
  239.  
  240. void cStaticEnemy :: Editor_Activate( void )
  241. {
  242.     CEGUI::WindowManager &wmgr = CEGUI::WindowManager::getSingleton();
  243.  
  244.     // image
  245.     CEGUI::Editbox *editbox = static_cast<CEGUI::Editbox *>(wmgr.createWindow( "TaharezLook/Editbox", "editor_static_enemy_image" ));
  246.     Editor_Add( UTF8_("Image"), UTF8_("Image filename"), editbox, 200 );
  247.  
  248.     editbox->setText( img_filename.c_str() );
  249.     editbox->subscribeEvent( CEGUI::Editbox::EventTextChanged, CEGUI::Event::Subscriber( &cStaticEnemy::Editor_Image_Key, this ) );
  250.  
  251.     // speed
  252.     editbox = static_cast<CEGUI::Editbox *>(wmgr.createWindow( "TaharezLook/Editbox", "editor_static_enemy_rotation_speed" ));
  253.     Editor_Add( UTF8_("Rotation Speed"), UTF8_("Rotation Speed"), editbox, 120 );
  254.  
  255.     editbox->setText( float_to_string( rotation_speed ) );
  256.     editbox->subscribeEvent( CEGUI::Editbox::EventKeyUp, CEGUI::Event::Subscriber( &cStaticEnemy::Editor_Rotation_Speed_Key, this ) );
  257.  
  258.     // init
  259.     Editor_Init();
  260. }
  261.  
  262. bool cStaticEnemy :: Editor_Image_Key( const CEGUI::EventArgs &event )
  263. {
  264.     const CEGUI::WindowEventArgs &windowEventArgs = static_cast<const CEGUI::WindowEventArgs&>( event );
  265.     string str_text = static_cast<CEGUI::Editbox *>( windowEventArgs.window )->getText().c_str();
  266.  
  267.     Set_Static_Image( str_text );
  268.  
  269.     return 1;
  270. }
  271.  
  272. bool cStaticEnemy :: Editor_Rotation_Speed_Key( const CEGUI::EventArgs &event )
  273. {
  274.     const CEGUI::WindowEventArgs &windowEventArgs = static_cast<const CEGUI::WindowEventArgs&>( event );
  275.     string str_text = static_cast<CEGUI::Editbox *>( windowEventArgs.window )->getText().c_str();
  276.  
  277.     Set_Rotation_Speed( string_to_float( str_text ) );
  278.  
  279.     return 1;
  280. }
  281.  
  282. void cStaticEnemy :: Create_Name( void )
  283. {
  284.     name = "Static Enemy";
  285.  
  286.     if( start_image && !start_image->name.empty() )
  287.     {
  288.         name += " " + start_image->name;
  289.     }
  290. }
  291.