ScreenSound.cpp

Go to the documentation of this file.
00001 #include "global.h"
00002 #include "ScreenSound.h"
00003 #include "MercuryScreenManager.h"
00004 #include "MercuryInput.h"
00005 #include "MercuryLog.h"
00006 #include "MercuryDisplay.h"
00007 #include "MercuryTheme.h"
00008 #include "MercurySoundManager.h"
00009 
00010 KeyMappingWithCode( button_a, "0-97" );
00011 KeyMappingWithCode( button_s, "0-115" );
00012 KeyMappingWithCode( button_d, "0-100" );
00013 KeyMappingWithCode( button_w, "0-119" );
00014 KeyMappingWithCode( button_up, "0-273" );
00015 KeyMappingWithCode( button_down, "0-274" );
00016 
00017 #include "MercuryObjectFactory.h"
00018 REGISTER_SCREEN_CLASS( ScreenSound )
00019 
00020 #define     MC_INPUTMAPPED      1       //convention: MESSAGE CODE
00021 #define     MC_INPUTUNMAPPED    2   
00022 
00023 ScreenSound::~ScreenSound()
00024 {
00025     DISPLAY->RemoveLight(&m_light);
00026     SAFE_DELETE(m_sound);
00027     SAFE_DELETE(m_ball);
00028 }
00029 
00030 void ScreenSound::Init()
00031 {
00032     MercuryScreen::Init();  
00033 
00034     m_sound = SOUNDMAN->Load(THEME.GetMetricS(GetName(), "Wav"));
00035 //  SOUNDMAN->Register(&sound);
00036     AddObject( m_sound );
00037 
00038     x = 0;
00039     y = 0;
00040 
00041     totalTime = 0;
00042 
00043     m_left = false;
00044     m_right = true;
00045     m_up = false;
00046     m_down = false;
00047 
00048     m_paddle.SetName( "Paddle" );
00049     m_paddle.Init();
00050     m_paddle.LoadImage( GET_GRAPHIC_BY_NAME( "Paddle" ) );
00051     m_paddle.MoveX( -300 );
00052     m_paddle.MoveY( 39 );
00053     m_paddle.MoveZ( -500 );
00054     m_paddle.SetDrawOrder( -1 );
00055     AddObject( &m_paddle );
00056 
00057     //top bar
00058     m_bar1.SetName( "Bar" );
00059     m_bar1.Init();
00060     m_bar1.LoadImage( GET_GRAPHIC_BY_NAME( "Bar" ) );
00061     m_bar1.MoveX( 0 );
00062     m_bar1.MoveY( 39 );
00063     m_bar1.MoveZ( 5 );
00064     m_bar1.SetDrawOrder( -1 );
00065     AddObject( &m_bar1 );
00066 
00067     //bottom bar
00068     m_bar2.SetName( "Bar" );
00069     m_bar2.Init();
00070     m_bar2.LoadImage( GET_GRAPHIC_BY_NAME( "Bar" ) );
00071     m_bar2.MoveX( 0 );
00072     m_bar2.MoveY( -39 );
00073     m_bar2.MoveZ( 5 );
00074     m_bar2.SetDrawOrder( -1 );
00075     AddObject( &m_bar2 );
00076 
00077     //right bar
00078     m_bar3.SetName( "Bar" );
00079     m_bar3.Init();
00080     m_bar3.LoadImage( GET_GRAPHIC_BY_NAME( "Bar" ) );
00081     m_bar3.MoveX( 52 );
00082     m_bar3.MoveY( 0 );
00083     m_bar3.MoveZ( 5 );
00084     m_bar3.MoveRotZ( 90 );
00085     m_bar3.SetDrawOrder( -1 );
00086     AddObject( &m_bar3 );
00087 
00088     m_ball = new MercuryLoadableModel;
00089     m_ball->SetName( "ball" );
00090     m_ball->Init();
00091     m_ball->Load(GET_MODEL_BY_NAME( "Model" )); 
00092     m_ball->MoveX( 0 );
00093     m_ball->MoveY( 0 );
00094     m_ball->MoveZ( -500 );
00095     m_ball->SetDrawOrder( -2 );
00096     AddObject( m_ball );
00097     
00098     //causes sound to be too far away from listner
00099     //m_ball->AddObject(&sound);
00100 
00101     m_camera.SetName( "camera" );
00102     m_camera.Init();
00103     m_camera.SetEye( MercuryPoint( 0,0,100 ) ); //place the eye
00104     m_camera.SetTarget( m_ball->GetPosition() ); //look at the ball
00105     this->AddObject( &m_camera );
00106 
00107     //Setup a light
00108     m_light.Init();
00109     m_light.SetY(100);
00110     m_light.SetX(-100);
00111     m_light.SetZ(110);
00112     m_light.SetAttenuation(0.001f, Attenuation::LINEAR);
00113 //  m_light.SetStatic(true);
00114     DISPLAY->AddLight(&m_light);
00115 
00116     this->RegisterMessage( MC_INPUTMAPPED, "mappedinput" );
00117 }
00118 
00119 void ScreenSound::Message( int Message, PStack & data, const MString & name )
00120 {   
00121     InputMessageStruct c(data);
00122     switch ( Message ) 
00123     {
00124     case MC_INPUTMAPPED:
00125         {
00126             if ( c.code == button_a )
00127             {
00128                 if ( c.type == IET_DOWN )
00129                 {
00130                     
00131                 }
00132                 if ( c.type == IET_RELEASE )
00133                 {
00134                     
00135                 }
00136             }
00137         }
00138     };
00139 }
00140 
00141 void ScreenSound::Update( const float dTime )
00142 {
00143     totalTime += dTime;
00144     /*
00145     int x,y;
00146     INPUTMAN->GetCursorPosition( x, y );
00147     m_ball->SetRotX( float(y)/3.0f );
00148     m_ball->SetRotY( float(x)/3.0f );
00149     */
00150     
00151     /*
00152     int x,y;
00153     INPUTMAN->GetCursorPosition( x, y );
00154     m_camera.SetY( float(y)/3.0f );
00155     m_camera.SetX( float(x)/3.0f );
00156     */
00157 
00158     //bounce off top
00159     if(m_ball->GetY() >= 213)
00160     {
00161         m_up = false;
00162         m_down = true;
00163         y = -y;
00164         m_sound->Play();
00165     }
00166     //bounce off bottom
00167     else if(m_ball->GetY() <= -215)
00168     {
00169         m_down = false;
00170         m_up = true;
00171         y = -y;
00172         m_sound->Play();
00173     }
00174     //bounce off right
00175     if(m_ball->GetX() >= 290)
00176     {
00177         m_right = false;
00178         m_left = true;
00179         x = -x;     
00180         m_sound->Play();
00181     }
00182     //boounce off paddle
00183     else if( m_ball->GetX() <= m_paddle.GetX() + 49
00184         &&   m_ball->GetX() >= m_paddle.GetX() - 49
00185         &&   m_ball->GetY() <= m_paddle.GetY() + 60
00186         &&   m_ball->GetY() >= m_paddle.GetY() - 60 
00187         )
00188     {
00189         //m_left = false;
00190         //m_right = true;
00191         //x = -x;       
00192     
00193         if( m_ball->GetX() <= m_paddle.GetX() + 49
00194         &&  m_ball->GetX() >= m_paddle.GetX() - 49
00195         &&  m_ball->GetY() <= m_paddle.GetY() + 60
00196         &&  m_ball->GetY() >= m_paddle.GetY() + 20 )
00197         {
00198             m_up = true;
00199             m_down = false;
00200             m_right = true;
00201             m_left = false;
00202 
00203             x = -x;
00204             y = -y;
00205         }
00206         else if( m_ball->GetX() <= m_paddle.GetX() + 49
00207         &&       m_ball->GetX() >= m_paddle.GetX() - 49
00208         &&       m_ball->GetY() <= m_paddle.GetY() + 19
00209         &&       m_ball->GetY() >= m_paddle.GetY() - 21 )
00210         {
00211             m_up = false;
00212             m_down = false;
00213             m_right = true;
00214             m_left = false;
00215 
00216             x = -x;
00217             y = -y;
00218         }
00219         else if( m_ball->GetX() <= m_paddle.GetX() + 49
00220         &&       m_ball->GetX() >= m_paddle.GetX() - 49
00221         &&       m_ball->GetY() <= m_paddle.GetY() - 22
00222         &&       m_ball->GetY() >= m_paddle.GetY() - 60 )
00223         {
00224             m_up = false;
00225             m_down = true;
00226             m_right = true;
00227             m_left = false;
00228 
00229             x = -x;
00230             y = -y;
00231         }
00232 
00233 
00234         //top and bottom collision
00235         /*if( m_ball->GetY() <= m_paddle.GetY() + 60
00236         &&  m_ball->GetY() >= m_paddle.GetY() - 60 )
00237         {
00238             if(m_up)
00239             {
00240                 m_up = false;
00241                 m_down = true;
00242             }           
00243             else if(m_down)
00244             {
00245                 m_down = false;
00246                 m_up = true;
00247             }
00248             
00249 
00250             y = -y;
00251         }
00252         */
00253         m_sound->Play();
00254     }
00255 
00256     if(m_right == true)
00257     {
00258         x += ( float(.1) );     
00259     }
00260     else if(m_left == true)
00261     {   
00262         x -= ( float(.1) );     
00263     }
00264 
00265     if(m_up)
00266     {
00267         y += ( float(.1) );
00268     }
00269     else if(m_down)
00270     {
00271         y -= ( float(.1) );
00272     }
00273 
00274     m_ball->MoveX( x * dTime);
00275     m_ball->MoveY( y * dTime);
00276 
00277     if ( INPUTMAN->IsButtonDown( button_a ) )
00278     {
00279         //m_ball->MoveX(-1);
00280 
00281     }
00282     if ( INPUTMAN->IsButtonDown( button_d ) )
00283     {
00284         //m_ball->MoveX(1);
00285     }
00286     if ( INPUTMAN->IsButtonDown( button_w ) && (m_paddle.GetY() + 30) < 210 )
00287     {
00288         //m_ball->MoveY(1);
00289         m_paddle.MoveY(500 * dTime );
00290     }
00291     if ( INPUTMAN->IsButtonDown( button_s ) && (m_paddle.GetY() + 30) > -150 )
00292     {
00293         //m_ball->MoveY(-1);
00294         m_paddle.MoveY(-500 * dTime );
00295     }
00296     if ( INPUTMAN->IsButtonDown( button_up ) )
00297     {
00298         //m_ball->MoveZ(1);
00299     }
00300     if ( INPUTMAN->IsButtonDown( button_down ) )
00301     {
00302         //m_ball->MoveZ(-1);
00303     }
00304 
00305     //reset ball
00306     if(m_ball->GetX() < -600)
00307     {       
00308         x = 0;
00309         y = 0;
00310 
00311         m_left = false;
00312         m_right = true;
00313         m_up = false;
00314         m_down = false;
00315 
00316         m_ball->SetX( 0 );
00317         m_ball->SetY( 0 );
00318     }
00319 
00320     MercuryScreen::Update( dTime );
00321 }
00322 
00323 void ScreenSound::Draw()
00324 {
00325 //  m_camera.Draw(); //This will be done automatically if the camera is added as an object
00326 
00327     /*
00328     // follow the ball
00329     int x, y;
00330     INPUTMAN->GetCursorPosition(x, y);
00331     m_camera.Tweening.StopTweening();
00332     
00333     //The tween adjusts the eye, up never changes, and center is always the control object
00334     m_camera.Tweening.AddCommand( ssprintf( "linear,0.4;x,%f;y,%f;z,%f;", 
00335         m_ball->GetX()+COS(float(x)/300)*70, 
00336         m_ball->GetY()+SIN(float(x)/300)*70,
00337         m_ball->GetZ()+(float(y)/300)*70) );
00338     m_camera.SetDirection(m_ball->GetPosition());
00339     */
00340 
00341     MercuryScreen::Draw();
00342 }
00343 
00344 /*
00345  * (c) 2006 Anthony Varner
00346  * All rights reserved.
00347  * 
00348  * Permission is hereby granted, free of charge, to any person obtaining a
00349  * copy of this software and associated documentation files (the
00350  * "Software"), to deal in the Software without restriction, including
00351  * without limitation the rights to use, copy, modify, merge, publish,
00352  * distribute, and/or sell copies of the Software, and to permit persons to
00353  * whom the Software is furnished to do so, provided that the above
00354  * copyright notice(s) and this permission notice appear in all copies of
00355  * the Software and that both the above copyright notice(s) and this
00356  * permission notice appear in supporting documentation.
00357  * 
00358  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00359  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00360  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
00361  * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
00362  * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
00363  * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
00364  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
00365  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00366  * PERFORMANCE OF THIS SOFTWARE.
00367  */

Hosted by SourceForge.net Logo