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

  1. /***************************************************************************
  2.  * eato.cpp  -  eating static plant :P
  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 "../enemies/eato.h"
  17. #include "../core/game_core.h"
  18. #include "../video/animation.h"
  19. #include "../player/player.h"
  20. #include "../video/gl_surface.h"
  21. #include "../core/i18n.h"
  22.  
  23. /* *** *** *** *** *** *** cEato *** *** *** *** *** *** *** *** *** *** *** */
  24.  
  25. cEato :: cEato( float x, float y )
  26. : cEnemy( x, y )
  27. {
  28.     cEato::Init();
  29. }
  30.  
  31. cEato :: cEato( CEGUI::XMLAttributes &attributes )
  32. : cEnemy()
  33. {
  34.     cEato::Init();
  35.     cEato::Create_from_Stream( attributes );
  36. }
  37.  
  38. cEato :: ~cEato( void )
  39. {
  40.     //
  41. }
  42.  
  43. void cEato :: Init( void )
  44. {
  45.     type = TYPE_EATO;
  46.     player_range = 1000;
  47.     posz = 0.087f;
  48.     Set_Rotation_affects_Rect( 1 );
  49.     can_be_ground = 1;
  50.     fire_resistant = 1;
  51.  
  52.     state = STA_STAY;
  53.     Set_Image_Dir( "enemy/eato/brown/" );
  54.     Set_Direction( DIR_UP_LEFT );
  55.  
  56.     walk_count = static_cast<float>( rand() % 4 );
  57.  
  58.     kill_sound = "enemy/eato/die.ogg";
  59.     kill_points = 150;
  60. }
  61.  
  62. cEato *cEato :: Copy( void )
  63. {
  64.     cEato *eato = new cEato( startposx, startposy );
  65.     eato->Set_Image_Dir( img_dir );
  66.     eato->Set_Direction( start_direction );
  67.  
  68.     return eato;
  69. }
  70.  
  71. void cEato :: Create_from_Stream( CEGUI::XMLAttributes &attributes )
  72. {
  73.     // position
  74.     Set_Pos( static_cast<float>(attributes.getValueAsInteger( "posx" )), static_cast<float>(attributes.getValueAsInteger( "posy" )), 1 );
  75.     // image directory
  76.     Set_Image_Dir( attributes.getValueAsString( "image_dir", img_dir ).c_str() );
  77.     // direction
  78.     Set_Direction( Get_Direction_Id( attributes.getValueAsString( "direction", Get_Direction_Name( start_direction ) ).c_str() ) );
  79. }
  80.  
  81. void cEato :: Save_to_Stream( ofstream &file )
  82. {
  83.     // begin enemy
  84.     file << "\t<enemy>" << std::endl;
  85.  
  86.     // name
  87.     file << "\t\t<Property name=\"type\" value=\"eato\" />" << std::endl;
  88.     // position
  89.     file << "\t\t<Property name=\"posx\" value=\"" << static_cast<int>(startposx) << "\" />" << std::endl;
  90.     file << "\t\t<Property name=\"posy\" value=\"" << static_cast<int>(startposy) << "\" />" << std::endl;
  91.     // image directory
  92.     file << "\t\t<Property name=\"image_dir\" value=\"" << img_dir << "\" />" << std::endl;
  93.     // direction
  94.     file << "\t\t<Property name=\"direction\" value=\"" << Get_Direction_Name( start_direction ) << "\" />" << std::endl;
  95.  
  96.     // end enemy
  97.     file << "\t</enemy>" << std::endl;
  98. }
  99.  
  100. void cEato :: Set_Image_Dir( string dir )
  101. {
  102.     if( dir.empty() )
  103.     {
  104.         return;
  105.     }
  106.  
  107.     img_dir = dir;
  108.  
  109.     // remove pixmaps dir
  110.     if( img_dir.find( DATA_DIR "/" GAME_PIXMAPS_DIR "/" ) == 0 )
  111.     {
  112.         img_dir.erase( 0, strlen( DATA_DIR "/" GAME_PIXMAPS_DIR "/" ) );
  113.     }
  114.  
  115.     // clear images
  116.     Clear_Images();
  117.     // set images
  118.     images.push_back( pVideo->Get_Surface( img_dir + "1.png" ) );
  119.     images.push_back( pVideo->Get_Surface( img_dir + "2.png" ) );
  120.     images.push_back( pVideo->Get_Surface( img_dir + "3.png" ) );
  121.     images.push_back( pVideo->Get_Surface( img_dir + "2.png" ) );
  122.     // set start image
  123.     Set_Image( 0, 1 );
  124.  
  125.     Create_Name();
  126. }
  127.  
  128. void cEato :: Set_Direction( ObjectDirection dir )
  129. {
  130.     cEnemy::Set_Direction( dir, 1 );
  131.  
  132.     // clear
  133.     Set_Rotation( 0, 0, 0, 1 );
  134.  
  135.     if( start_direction == DIR_UP_LEFT )
  136.     {
  137.         Set_Rotation_Y( 180, 1 );
  138.     }
  139.     else if( start_direction == DIR_UP_RIGHT )
  140.     {
  141.         // default
  142.     }
  143.     else if( start_direction == DIR_LEFT_UP )
  144.     {
  145.         Set_Rotation_Z( 90, 1 );
  146.         Set_Rotation_X( 180, 1 );
  147.     }
  148.     else if( start_direction == DIR_LEFT_DOWN )
  149.     {
  150.         Set_Rotation_Z( 90, 1 );
  151.     }
  152.     else if( start_direction == DIR_RIGHT_UP )
  153.     {
  154.         Set_Rotation_Z( 270, 1 );
  155.     }
  156.     else if( start_direction == DIR_RIGHT_DOWN )
  157.     {
  158.         Set_Rotation_Z( 270, 1 );
  159.         Set_Rotation_X( 180, 1 );
  160.     }
  161.     else if( start_direction == DIR_DOWN_LEFT )
  162.     {
  163.         Set_Rotation_X( 180, 1 );
  164.     }
  165.     else if( start_direction == DIR_DOWN_RIGHT )
  166.     {
  167.         Set_Rotation_Z( 180, 1 );
  168.     }
  169.  
  170.     Create_Name();
  171. }
  172.  
  173. void cEato :: DownGrade( bool force /* = 0 */ )
  174. {
  175.     Set_Dead( 1 );
  176.     massivetype = MASS_PASSIVE;
  177.     counter = 0;
  178.  
  179.     if( !force )
  180.     {
  181.         // animation
  182.         cParticle_Emitter *anim = new cParticle_Emitter();
  183.         anim->Set_Pos( posx + ( col_rect.w / 2 ), posy + ( col_rect.h / 2 ) );
  184.         Generate_Hit_Animation( anim );
  185.  
  186.         anim->Set_Scale( 0.8f );
  187.         anim->Set_Direction_Range( 0, 360 );
  188.         // add animation
  189.         pAnimation_Manager->Add( anim );
  190.     }
  191.     else
  192.     {
  193.         Set_Rotation_Z( 180 );
  194.     }
  195. }
  196.  
  197. void cEato :: DieStep( void )
  198. {
  199.     counter += pFramerate->speedfactor;
  200.  
  201.     // default death
  202.     if( rotz != 180 )
  203.     {
  204.         Set_Visible( 0 );
  205.     }
  206.     // falling death
  207.     else
  208.     {
  209.         // a little bit upwards first
  210.         if( counter < 5 )
  211.         {
  212.             Move( 0, -5 );
  213.         }
  214.         // if not below the screen fall
  215.         else if( posy < game_res_h + col_rect.h )
  216.         {
  217.             Move( 0, 20 );
  218.         }
  219.         // if below disable
  220.         else
  221.         {
  222.             rotz = 0;
  223.             Set_Visible( 0 );
  224.         }
  225.     }
  226. }
  227.  
  228. void cEato :: Update( void )
  229. {
  230.     cEnemy::Update();
  231.  
  232.     if( !valid_update || !is_Player_range() )
  233.     {
  234.         return;
  235.     }
  236.  
  237.     walk_count += 0.2f * pFramerate->speedfactor;
  238.  
  239.     if( walk_count >= 4 )
  240.     {
  241.         walk_count = 0;
  242.     }
  243.  
  244.     Set_Image( static_cast<int>(walk_count) );
  245. }
  246.  
  247. bool cEato :: Is_Update_Valid( void )
  248. {
  249.     if( dead || freeze_counter )
  250.     {
  251.         return 0;
  252.     }
  253.  
  254.     return 1;
  255. }
  256.  
  257. unsigned int cEato :: Validate_Collision( cSprite *obj )
  258. {
  259.     if( obj->type == TYPE_PLAYER )
  260.     {
  261.         return 2;
  262.     }
  263.     if( obj->type == TYPE_BALL )
  264.     {
  265.         return 2;
  266.     }
  267.  
  268.     return 0;
  269. }
  270.  
  271. void cEato :: Handle_Collision_Player( cObjectCollision *collision )
  272. {
  273.     // unknown direction
  274.     if( collision->direction == DIR_UNDEFINED )
  275.     {
  276.         return;
  277.     }
  278.  
  279.     // only if not invincible
  280.     if( pPlayer->invincible <= 0 )
  281.     {
  282.         // if player is big and not a bottom collision
  283.         if( pPlayer->maryo_type != MARYO_SMALL && ( collision->direction != DIR_BOTTOM ) )
  284.         {
  285.             // todo : create again
  286.             //pAudio->PlaySound( "player/maryo_au.ogg", RID_MARYO_AU );
  287.             pPlayer->Action_Jump( 1 );
  288.         }
  289.  
  290.         pPlayer->DownGrade();
  291.     }
  292. }
  293.  
  294. void cEato :: Editor_Activate( void )
  295. {
  296.     CEGUI::WindowManager &wmgr = CEGUI::WindowManager::getSingleton();
  297.  
  298.     // image dir
  299.     CEGUI::Editbox *editbox = static_cast<CEGUI::Editbox *>(wmgr.createWindow( "TaharezLook/Editbox", "editor_eato_image_dir" ));
  300.     Editor_Add( UTF8_("Image directory"), UTF8_("Directory containing the images"), editbox, 200 );
  301.  
  302.     editbox->setText( img_dir.c_str() );
  303.     editbox->subscribeEvent( CEGUI::Editbox::EventTextChanged, CEGUI::Event::Subscriber( &cEato::Editor_Image_Dir_Key, this ) );
  304.     // init
  305.     Editor_Init();
  306. }
  307.  
  308. bool cEato :: Editor_Image_Dir_Key( const CEGUI::EventArgs &event )
  309. {
  310.     const CEGUI::WindowEventArgs &windowEventArgs = static_cast<const CEGUI::WindowEventArgs&>( event );
  311.     string str_text = static_cast<CEGUI::Editbox *>( windowEventArgs.window )->getText().c_str();
  312.  
  313.     Set_Image_Dir( str_text );
  314.  
  315.     return 1;
  316. }
  317.  
  318. void cEato :: Create_Name( void )
  319. {
  320.     name = "Eato ";
  321.     name += _(Get_Direction_Name( start_direction ).c_str());
  322.  
  323.     if( start_image && !start_image->name.empty() )
  324.     {
  325.         name += " " + start_image->name;
  326.     }
  327. }
  328.