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

  1. /***************************************************************************
  2.  * level_entry.cpp  -  entry point to enter a level
  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 "../objects/level_entry.h"
  17. #include "../player/player.h"
  18. #include "../core/game_core.h"
  19. #include "../core/camera.h"
  20. #include "../audio/audio.h"
  21. #include "../core/framerate.h"
  22. #include "../core/main.h"
  23. #include "../video/gl_surface.h"
  24. #include "../video/font.h"
  25. #include "../video/renderer.h"
  26. #include "../level/level.h"
  27. #include "../core/i18n.h"
  28.  
  29. /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
  30.  
  31. cLevel_Entry :: cLevel_Entry( float x, float y )
  32. : cImageObjectSprite( x, y )
  33. {
  34.     cLevel_Entry::Init();
  35. }
  36.  
  37. cLevel_Entry :: cLevel_Entry( CEGUI::XMLAttributes &attributes )
  38. : cImageObjectSprite()
  39. {
  40.     cLevel_Entry::Init();
  41.     cLevel_Entry::Create_from_Stream( attributes );
  42. }
  43.  
  44. cLevel_Entry :: ~cLevel_Entry( void )
  45. {
  46.     if( editor_entry_name )
  47.     {
  48.         delete editor_entry_name;
  49.         editor_entry_name = NULL;
  50.     }
  51. }
  52.  
  53. void cLevel_Entry :: Init( void )
  54. {
  55.     sprite_array = ARRAY_ACTIVE;
  56.     type = TYPE_LEVEL_ENTRY;
  57.     massivetype = MASS_PASSIVE;
  58.     editor_posz = 0.111f;
  59.     player_range = 1000;
  60.  
  61.     rect.w = 10;
  62.     rect.h = 20;
  63.     col_rect.w = rect.w;
  64.     col_rect.h = rect.h;
  65.     start_rect.w = rect.w;
  66.     start_rect.h = rect.h;
  67.  
  68.     Set_Direction( DIR_UP );
  69.  
  70.     editor_color = lightblue;
  71.     editor_color.alpha = 128;
  72.  
  73.     editor_entry_name = NULL;
  74. }
  75.  
  76. cLevel_Entry *cLevel_Entry :: Copy( void )
  77. {
  78.     cLevel_Entry *level_entry = new cLevel_Entry( startposx, startposy );
  79.     level_entry->Set_Direction( start_direction );
  80.     level_entry->Set_Name( entry_name );
  81.  
  82.     return level_entry;
  83. }
  84.  
  85. void cLevel_Entry :: Create_from_Stream( CEGUI::XMLAttributes &attributes )
  86. {
  87.     // position
  88.     Set_Pos( static_cast<float>(attributes.getValueAsInteger( "posx" )), static_cast<float>(attributes.getValueAsInteger( "posy" )), 1 );
  89.     // name
  90.     Set_Name( attributes.getValueAsString( "name" ).c_str() );
  91.     // direction
  92.     Set_Direction( Get_Direction_Id( attributes.getValueAsString( "direction", Get_Direction_Name( start_direction ) ).c_str() ) );
  93. }
  94.  
  95. void cLevel_Entry :: Save_to_Stream( ofstream &file )
  96. {
  97.     // begin level_entry
  98.     file << "\t<level_entry>" << std::endl;
  99.  
  100.     // position
  101.     file << "\t\t<Property name=\"posx\" value=\"" << static_cast<int>(startposx) << "\" />" << std::endl;
  102.     file << "\t\t<Property name=\"posy\" value=\"" << static_cast<int>(startposy) << "\" />" << std::endl;
  103.     // direction
  104.     file << "\t\t<Property name=\"direction\" value=\"" << Get_Direction_Name( start_direction ) << "\" />" << std::endl;
  105.     // name
  106.     if( !entry_name.empty() )
  107.     {
  108.         file << "\t\t<Property name=\"name\" value=\"" << string_to_xml_string( entry_name ) << "\" />" << std::endl;
  109.     }
  110.  
  111.     // end level_entry
  112.     file << "\t</level_entry>" << std::endl;
  113. }
  114.  
  115. void cLevel_Entry :: Set_Direction( ObjectDirection dir )
  116. {
  117.     // already set
  118.     if( direction == dir )
  119.     {
  120.         return;
  121.     }
  122.  
  123.     cImageObjectSprite::Set_Direction( dir, 1 );
  124.  
  125.     Create_Name();
  126. }
  127.  
  128. void cLevel_Entry :: Create_Name( void )
  129. {
  130.     name = _("Level Entry");
  131.  
  132.     if( direction == DIR_UP )
  133.     {
  134.         name += " U";
  135.     }
  136.     else if( direction == DIR_LEFT )
  137.     {
  138.         name += " L";
  139.     }
  140.     else if( direction == DIR_DOWN )
  141.     {
  142.         name += " D";
  143.     }
  144.     else if( direction == DIR_RIGHT )
  145.     {
  146.         name += " R";
  147.     }
  148. }
  149.  
  150. void cLevel_Entry :: Draw( cSurfaceRequest *request /* = NULL */ )
  151. {
  152.     if( !valid_draw )
  153.     {
  154.         return;
  155.     }
  156.  
  157.     // draw color rect
  158.     pVideo->Draw_Rect( col_rect.x - pActive_Camera->x, col_rect.y - pActive_Camera->y, col_rect.w, col_rect.h, editor_posz, &editor_color );
  159.  
  160.     // draw entry name
  161.     if( editor_entry_name )
  162.     {
  163.         // create request
  164.         cSurfaceRequest *surface_request = new cSurfaceRequest();
  165.         // blit
  166.         editor_entry_name->Blit( col_rect.x + col_rect.w + 5 - pActive_Camera->x, col_rect.y - pActive_Camera->y, editor_posz, surface_request );
  167.         surface_request->shadow_pos = 2;
  168.         surface_request->shadow_color = lightgreyalpha64;
  169.         // add request
  170.         pRenderer->Add( surface_request );
  171.     }
  172. }
  173.  
  174. void cLevel_Entry :: Activate( void )
  175. {
  176.     pAudio->Play_Sound( "leave_pipe.ogg" );
  177.     
  178.     pPlayer->Stop_Ducking();
  179.     pPlayer->Reset_on_Ground();
  180.  
  181.     // set position
  182.     pPlayer->Set_Pos( Get_Player_Pos_X(), Get_Player_Pos_Y() );
  183.     // set image
  184.     if( direction == DIR_UP || direction == DIR_DOWN )
  185.     {
  186.         pPlayer->Set_Image( MARYO_IMG_FALL + pPlayer->direction );
  187.     }
  188.     else if( direction == DIR_LEFT || direction == DIR_RIGHT )
  189.     {
  190.         pPlayer->Set_Image( pPlayer->direction );
  191.  
  192.         // set rotation
  193.         if( direction == DIR_RIGHT )
  194.         {
  195.             pPlayer->Set_Rotation_Z( 90 );
  196.         }
  197.         else if( direction == DIR_LEFT )
  198.         {
  199.             pPlayer->Set_Rotation_Z( 270 );
  200.         }
  201.     }
  202.  
  203.     // change position z to be before massive for the animation
  204.     float player_posz = pPlayer->posz;
  205.     pPlayer->posz = 0.0799f;
  206.  
  207.     // move slowly
  208.     for( float i = 0; i < ( speedfactor_fps * 0.9f ); i += pFramerate->speedfactor )
  209.     {
  210.         if( direction == DIR_DOWN )
  211.         {
  212.             pPlayer->Move( 0, 2.8f );
  213.         }
  214.         else if( direction == DIR_UP )
  215.         {
  216.             pPlayer->Move( 0, -2.8f );
  217.         }
  218.         else if( direction == DIR_RIGHT )
  219.         {
  220.             pPlayer->Move( 2, 0 );
  221.         }
  222.         else if( direction == DIR_LEFT )
  223.         {
  224.             pPlayer->Move( -2, 0 );
  225.         }
  226.  
  227.         // center camera
  228.         pActive_Camera->Center();
  229.         // keep global effect particles on screen
  230.         pActive_Level->pGlobal_effect->Update_Particles();
  231.         // draw
  232.         Draw_Game();
  233.  
  234.         pVideo->Render();
  235.         pFramerate->Update();
  236.     }
  237.  
  238.     // set position z back
  239.     pPlayer->posz = player_posz;
  240.  
  241.     if( direction == DIR_RIGHT || direction == DIR_LEFT )
  242.     {
  243.         pPlayer->Set_Rotation_Z( 0 );
  244.     }
  245.  
  246.     pPlayer->Collisions_Clear();
  247. }
  248.  
  249. float cLevel_Entry :: Get_Player_Pos_X( void )
  250. {
  251.     // left
  252.     if( direction == DIR_LEFT )
  253.     {
  254.         return col_rect.x - pPlayer->col_pos.y + col_rect.w;
  255.     }
  256.     // right
  257.     else if( direction == DIR_RIGHT )
  258.     {
  259.         return col_rect.x - pPlayer->col_pos.y - col_rect.w - pPlayer->col_rect.w;
  260.     }
  261.  
  262.     // up/down
  263.     return col_rect.x - pPlayer->col_pos.x + ( col_rect.w * 0.5f ) - ( pPlayer->col_rect.w * 0.5f );
  264. }
  265.  
  266. float cLevel_Entry :: Get_Player_Pos_Y( void )
  267. {
  268.     // up
  269.     if( direction == DIR_UP )
  270.     {
  271.         return col_rect.y - pPlayer->col_pos.y + col_rect.h;
  272.     }
  273.     // down
  274.     else if( direction == DIR_DOWN )
  275.     {
  276.         return col_rect.y - pPlayer->col_pos.y - 5 - pPlayer->rect.h;
  277.     }
  278.  
  279.     // left/right
  280.     return col_rect.y - pPlayer->col_pos.y + ( col_rect.h * 0.5f ) - ( pPlayer->col_rect.h * 0.5f );
  281. }
  282.  
  283. void cLevel_Entry :: Set_Name( string str_name )
  284. {
  285.     // delete editor image
  286.     if( editor_entry_name )
  287.     {
  288.         delete editor_entry_name;
  289.         editor_entry_name = NULL;
  290.     }
  291.  
  292.     // Set new name
  293.     entry_name = str_name;
  294.  
  295.     // if empty don't create editor image
  296.     if( entry_name.empty() )
  297.     {
  298.         return;
  299.     }
  300.  
  301.     editor_entry_name = pFont->Render_Text( pFont->font_small, entry_name, white );
  302. }
  303.  
  304. bool cLevel_Entry :: Is_Draw_Valid( void )
  305. {
  306.     // if editor not enabled
  307.     if( !editor_enabled )
  308.     {
  309.         return 0;
  310.     }
  311.  
  312.     // not visible on the screen
  313.     if( !visible || !Is_Visible_on_Screen() )
  314.     {
  315.         return 0;
  316.     }
  317.  
  318.     return 1;
  319. }
  320.  
  321. void cLevel_Entry :: Editor_Activate( void )
  322. {
  323.     CEGUI::WindowManager &wmgr = CEGUI::WindowManager::getSingleton();
  324.  
  325.     // direction
  326.     CEGUI::Combobox *combobox = static_cast<CEGUI::Combobox *>(wmgr.createWindow( "TaharezLook/Combobox", "level_entry_direction" ));
  327.     Editor_Add( UTF8_("Direction"), UTF8_("The direction to come out"), combobox, 100, 105 );
  328.  
  329.     combobox->addItem( new CEGUI::ListboxTextItem( "up" ) );
  330.     combobox->addItem( new CEGUI::ListboxTextItem( "down" ) );
  331.     combobox->addItem( new CEGUI::ListboxTextItem( "right" ) );
  332.     combobox->addItem( new CEGUI::ListboxTextItem( "left" ) );
  333.     combobox->setText( Get_Direction_Name( start_direction ) );
  334.  
  335.     combobox->subscribeEvent( CEGUI::Combobox::EventListSelectionAccepted, CEGUI::Event::Subscriber( &cLevel_Entry::Editor_Direction_Select, this ) );
  336.  
  337.     // destination entry
  338.     CEGUI::Editbox *editbox = static_cast<CEGUI::Editbox *>(wmgr.createWindow( "TaharezLook/Editbox", "level_entry_name" ));
  339.     Editor_Add( UTF8_("Name"), UTF8_("Name for identification"), editbox, 150 );
  340.  
  341.     editbox->setText( entry_name.c_str() );
  342.     editbox->subscribeEvent( CEGUI::Editbox::EventKeyUp, CEGUI::Event::Subscriber( &cLevel_Entry::Editor_Name_Key, this ) );
  343.  
  344.     // init
  345.     Editor_Init();
  346. }
  347.  
  348. bool cLevel_Entry :: Editor_Direction_Select( const CEGUI::EventArgs &event )
  349. {
  350.     const CEGUI::WindowEventArgs &windowEventArgs = static_cast<const CEGUI::WindowEventArgs&>( event );
  351.     CEGUI::ListboxItem *item = static_cast<CEGUI::Combobox *>( windowEventArgs.window )->getSelectedItem();
  352.  
  353.     Set_Direction( Get_Direction_Id( item->getText().c_str() ) );
  354.  
  355.     return 1;
  356. }
  357.  
  358. bool cLevel_Entry :: Editor_Name_Key( const CEGUI::EventArgs &event )
  359. {
  360.     const CEGUI::WindowEventArgs &windowEventArgs = static_cast<const CEGUI::WindowEventArgs&>( event );
  361.     string str_text = static_cast<CEGUI::Editbox *>( windowEventArgs.window )->getText().c_str();
  362.  
  363.     Set_Name( str_text );
  364.  
  365.     return 1;
  366. }
  367.