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

  1. /***************************************************************************
  2.  * global_effect.cpp  -  class for handling level global effects
  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 "../level/global_effect.h"
  17. #include "../core/framerate.h"
  18. #include "../level/level_editor.h"
  19. #include "../core/camera.h"
  20. #include "../core/game_core.h"
  21. #include "../video/gl_surface.h"
  22.  
  23. /* *** *** *** *** *** cGlobal_effect *** *** *** *** *** *** *** *** *** *** *** *** */
  24.  
  25. cGlobal_effect :: cGlobal_effect( void )
  26. : cParticle_Emitter()
  27. {
  28.     Clear();
  29. }
  30.  
  31. cGlobal_effect :: cGlobal_effect( CEGUI::XMLAttributes &attributes )
  32. : cParticle_Emitter()
  33. {
  34.     Clear();
  35.     Create_from_Stream( attributes );
  36. }
  37.  
  38. cGlobal_effect :: ~cGlobal_effect( void )
  39. {
  40.  
  41. }
  42.  
  43. void cGlobal_effect :: Init_Anim( void )
  44. {
  45.     if( ge_type == GL_EFF_NONE )
  46.     {
  47.         return;
  48.     }
  49.  
  50.     if( ge_type == GL_EFF_FALLING )
  51.     {
  52.         // image doesn't exist
  53.         if( !pVideo->Get_Surface( image_filename ) )
  54.         {
  55.             valid = 0;
  56.         }
  57.         // image exists
  58.         else
  59.         {
  60.             image = pVideo->Get_Surface( image_filename );
  61.             // valid effect
  62.             valid = 1;
  63.         }
  64.     }
  65.  
  66.     // update ahead
  67.     if( valid )
  68.     {
  69.         float old_speedfactor = pFramerate->speedfactor;
  70.         pFramerate->speedfactor = 1;
  71.         // use time to live as seconds
  72.         for( float i = 0; i < speedfactor_fps * time_to_live; i++ )
  73.         {
  74.             Update();
  75.         }
  76.  
  77.         pFramerate->speedfactor = old_speedfactor;
  78.     }
  79. }
  80.  
  81. void cGlobal_effect :: Clear( void )
  82. {
  83.     cParticle_Emitter::Clear();
  84.  
  85.     ge_type = GL_EFF_NONE;
  86.  
  87.     image_filename.clear();
  88.     image = NULL;
  89.     Set_Emitter_Rect( 0, 0, static_cast<float>(game_res_w), 0 );
  90.     Set_Emitter_Time_to_Live( -1 );
  91.     Set_Emitter_Iteration_Interval( 0.3f );
  92.     Set_Pos_Z( 0.12f, 0 );
  93.     Set_Time_to_Live( 7, 0 );
  94.     Set_Scale( 0.2f, 0.2f );
  95.     Set_Speed( 2, 8 );
  96.     Set_Direction_Range( 0, 90 );
  97.     Set_Rotation( 0, 0, 0, 1 );
  98.     Set_Const_Rotation_Z( -5, 10 );
  99.  
  100.     valid = 0;
  101. }
  102.  
  103. void cGlobal_effect :: Create_from_Stream( CEGUI::XMLAttributes &attributes )
  104. {
  105.     // Type
  106.     Set_Type( (GlobalEffectType)attributes.getValueAsInteger( "type", ge_type ) );
  107.     // Image
  108.     Set_Image( attributes.getValueAsString( "image", image_filename ).c_str() );
  109.     // Creation Rect
  110.     Set_Emitter_Rect( static_cast<float>(attributes.getValueAsInteger( "rect_x", static_cast<int>(startposx) )), static_cast<float>(attributes.getValueAsInteger( "rect_y", static_cast<int>(startposy) )), static_cast<float>(attributes.getValueAsInteger( "rect_w", static_cast<int>(rect.w) )), static_cast<float>(attributes.getValueAsInteger( "rect_h", static_cast<int>(rect.h) )) );
  111.     // Z Position
  112.     Set_Pos_Z( attributes.getValueAsFloat( "z", posz ), attributes.getValueAsFloat( "z_rand", posz_rand ) );
  113.     // Time to Live
  114.     if( attributes.exists( "time_to_live" ) )
  115.     {
  116.         Set_Time_to_Live( attributes.getValueAsFloat( "time_to_live", time_to_live ), attributes.getValueAsFloat( "time_to_live_rand", time_to_live_rand ) );
  117.     }
  118.     // if not set uses old Lifetime mod
  119.     else
  120.     {
  121.         Set_Time_to_Live( attributes.getValueAsFloat( "lifetime_mod", 20 ) * 0.3f );
  122.     }
  123.     // Emitter Iteration Interval
  124.     if( attributes.exists( "emitter_iteration_interval" ) )
  125.     {
  126.         Set_Emitter_Iteration_Interval( attributes.getValueAsFloat( "emitter_iteration_interval", emitter_iteration_interval ) );
  127.     }
  128.     // if not set uses old Creation speed ( 0.99.7 and below )
  129.     else
  130.     {
  131.         Set_Emitter_Iteration_Interval( ( 1 / attributes.getValueAsFloat( "creation_speed", 0.3f ) ) * 0.032f );
  132.     }
  133.     // Scale
  134.     Set_Scale( attributes.getValueAsFloat( "scale", size_scale ), attributes.getValueAsFloat( "scale_rand", size_scale_rand ) );
  135.     // Speed
  136.     Set_Speed( attributes.getValueAsFloat( "speed", vel ), attributes.getValueAsFloat( "speed_rand", vel_rand ) );
  137.     // Direction
  138.     Set_Direction_Range( attributes.getValueAsFloat( "dir_range_start", angle_start ), attributes.getValueAsFloat( "dir_range_size", angle_range ) );
  139.     // start rotation
  140.     Set_Rotation( attributes.getValueAsFloat( "rot_x", start_rotx ), attributes.getValueAsFloat( "rot_y", start_roty ), attributes.getValueAsFloat( "rot_z", start_rotz ), 1 );
  141.     // Constant Rotation Z
  142.     Set_Const_Rotation_Z( attributes.getValueAsFloat( "const_rotz", const_rotz ), attributes.getValueAsFloat( "const_rotz_rand", const_rotz_rand ) );
  143. }
  144.  
  145. void cGlobal_effect :: Save_to_Stream( ofstream &file )
  146. {
  147.     if( ge_type == GL_EFF_NONE )
  148.     {
  149.         return;
  150.     }
  151.  
  152.     // begin global effect
  153.     file << "\t<global_effect>" << std::endl;
  154.  
  155.     // type
  156.     file << "\t\t<Property name=\"type\" value=\"" << ge_type << "\" />" << std::endl;
  157.     // image
  158.     string img_filename = image_filename;
  159.  
  160.     if( img_filename.find( DATA_DIR "/" GAME_PIXMAPS_DIR "/" ) == 0 )
  161.     {
  162.         img_filename.erase( 0, strlen( DATA_DIR "/" GAME_PIXMAPS_DIR "/" ) );
  163.     }
  164.  
  165.     file << "\t\t<Property name=\"image\" value=\"" << img_filename << "\" />" << std::endl;
  166.     // rect
  167.     file << "\t\t<Property name=\"rect_x\" value=\"" << startposx << "\" />" << std::endl;
  168.     file << "\t\t<Property name=\"rect_y\" value=\"" << startposy << "\" />" << std::endl;
  169.     file << "\t\t<Property name=\"rect_w\" value=\"" << rect.w << "\" />" << std::endl;
  170.     file << "\t\t<Property name=\"rect_h\" value=\"" << rect.h << "\" />" << std::endl;
  171.     // Z Position
  172.     file << "\t\t<Property name=\"z\" value=\"" << posz << "\" />" << std::endl;
  173.     file << "\t\t<Property name=\"z_rand\" value=\"" << posz_rand << "\" />" << std::endl;
  174.     // Time to Live
  175.     file << "\t\t<Property name=\"time_to_live\" value=\"" << time_to_live << "\" />" << std::endl;
  176.     file << "\t\t<Property name=\"time_to_live_rand\" value=\"" << time_to_live_rand << "\" />" << std::endl;
  177.     // Emitter Iteration Interval
  178.     file << "\t\t<Property name=\"emitter_iteration_interval\" value=\"" << emitter_iteration_interval << "\" />" << std::endl;
  179.     // scale
  180.     file << "\t\t<Property name=\"scale\" value=\"" << size_scale << "\" />" << std::endl;
  181.     file << "\t\t<Property name=\"scale_rand\" value=\"" << size_scale_rand << "\" />" << std::endl;
  182.     // speed
  183.     file << "\t\t<Property name=\"speed\" value=\"" << vel << "\" />" << std::endl;
  184.     file << "\t\t<Property name=\"speed_rand\" value=\"" << vel_rand << "\" />" << std::endl;
  185.     // direction range
  186.     file << "\t\t<Property name=\"dir_range_start\" value=\"" << angle_start << "\" />" << std::endl;
  187.     file << "\t\t<Property name=\"dir_range_size\" value=\"" << angle_range << "\" />" << std::endl;
  188.     // start rotation
  189.     file << "\t\t<Property name=\"rot_x\" value=\"" << start_rotx << "\" />" << std::endl;
  190.     file << "\t\t<Property name=\"rot_y\" value=\"" << start_roty << "\" />" << std::endl;
  191.     file << "\t\t<Property name=\"rot_z\" value=\"" << start_rotz << "\" />" << std::endl;
  192.     // constant rotation Z
  193.     file << "\t\t<Property name=\"const_rotz\" value=\"" << const_rotz << "\" />" << std::endl;
  194.     file << "\t\t<Property name=\"const_rotz_rand\" value=\"" << const_rotz_rand << "\" />" << std::endl;
  195.  
  196.     // end global effect
  197.     file << "\t</global_effect>" << std::endl;
  198. }
  199.  
  200. void cGlobal_effect :: Update( void )
  201. {
  202.     if( editor_level_enabled || !valid )
  203.     {
  204.         return;
  205.     }
  206.  
  207.     // if disabled
  208.     if( ge_type == GL_EFF_NONE )
  209.     {
  210.         return;
  211.     }
  212.     
  213.     Set_Pos( startposx + pActive_Camera->x, startposy + pActive_Camera->y );
  214.     // update particles
  215.     Update_Particles();
  216.     // update particle animation
  217.     cParticle_Emitter::Update();
  218. }
  219.  
  220. void cGlobal_effect :: Update_Particles( void )
  221. {
  222.     GL_rect camera_rect = GL_rect( pActive_Camera->x, pActive_Camera->y, static_cast<float>(game_res_w), static_cast<float>(game_res_h) );
  223.  
  224.     for( ParticleList::iterator itr = objects.begin(), itr_end = objects.end(); itr != itr_end; ++itr )
  225.     {
  226.         // get animation particle pointer
  227.         cParticle *obj = static_cast<cParticle *>(*itr);
  228.         
  229.         GL_rect anim_rect = GL_rect( obj->posx, obj->posy, obj->rect.w, obj->rect.h );
  230.         
  231.         // if not on screen
  232.         if( !Col_Box( &camera_rect, &anim_rect ) )
  233.         {
  234.             // out in left
  235.             if( anim_rect.x + anim_rect.w < camera_rect.x )
  236.             {
  237.                 // move to right
  238.                 obj->Set_Pos_X( camera_rect.x + camera_rect.w - 1 );
  239.             }
  240.             // out in right
  241.             else if( anim_rect.x > camera_rect.x + camera_rect.w )
  242.             {
  243.                 // move to left
  244.                 obj->Set_Pos_X( camera_rect.x + 1 );
  245.             }
  246.             // out on top
  247.             else if( anim_rect.y + anim_rect.h < camera_rect.y )
  248.             {
  249.                 // move to bottom
  250.                 obj->Set_Pos_Y( camera_rect.y + camera_rect.h - 1 );
  251.             }
  252.             // out on bottom
  253.             else if( anim_rect.y > camera_rect.y + camera_rect.h )
  254.             {
  255.                 // move to top
  256.                 obj->Set_Pos_Y( camera_rect.y + 1 );
  257.             }
  258.         }
  259.     }
  260. }
  261.  
  262. void cGlobal_effect :: Draw( void )
  263. {
  264.     cParticle_Emitter::Draw();
  265. }
  266.  
  267. void cGlobal_effect :: Set_Type( GlobalEffectType ntype )
  268. {
  269.     ge_type = ntype;
  270. }
  271.  
  272. void cGlobal_effect :: Set_Type( string ntype )
  273. {
  274.     if( ntype.compare( "Disabled" ) == 0 )
  275.     {
  276.         ge_type = GL_EFF_NONE;
  277.     }
  278.     else if( ntype.compare( "Falling" ) == 0 || ntype.compare( "Default" ) == 0 )
  279.     {
  280.         ge_type = GL_EFF_FALLING;
  281.     }
  282.     else
  283.     {
  284.         printf( "Warning : Unknown Global Effect type %s\n", ntype.c_str() );
  285.     }
  286. }
  287.  
  288. void cGlobal_effect :: Set_Image( string nimg_file )
  289. {
  290.     image_filename = nimg_file;
  291. }
  292.  
  293. string cGlobal_effect :: Get_Type_Name( void )
  294. {
  295.     if( ge_type == GL_EFF_NONE )
  296.     {
  297.         return "Disabled";
  298.     }
  299.     else if( ge_type == GL_EFF_FALLING )
  300.     {
  301.         return "Falling";
  302.     }
  303.  
  304.     return "Unknown";
  305. }
  306.