ScreenODETest.cpp

Go to the documentation of this file.
00001 #include "global.h"
00002 #include "MercuryDisplay.h"
00003 #include "ScreenODETest.h"
00004 #include "MercuryTheme.h" 
00005 #include "MercuryInput.h"
00006 #include "MercuryUtil.h"
00007 
00008 REGISTER_SCREEN_CLASS( ScreenODETest )
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 
00015 ScreenODETest::~ScreenODETest( )
00016 {
00017     DISPLAY->RemoveLight(&m_light);
00018 //  SOUNDMAN->SetListener(NULL);
00019 }
00020 
00021 void ScreenODETest::Init()
00022 {
00023     MercuryScreen::Init();
00024 
00025     MercuryINI pINI;
00026     pINI.Open( GET_FILE_BY_NAME( "IniFile" ) );
00027 
00028     m_pWorld.Init();
00029     m_pWorld.LoadFromINI( pINI, THEME.GetMetricS( m_name, "WorldName" ) );
00030     AddPerspObject( &m_pWorld );
00031 
00032     m_shpGround.SetName("Ground");
00033     m_shpGround.Init();
00034     m_shpGround.MakeSprite();
00035     m_shpGround.LoadImage( "GRAPHIC:ground2.png" );
00036     m_shpGround.SetScale( MercuryPoint( 900,900,0));
00037     AddPerspObject( &m_shpGround );
00038 
00039     m_pCamera = new MercuryCamera;
00040     m_pCamera->SetName( "Camera" );
00041     m_pCamera->Init();
00042     m_pCamera->SetPosition( MercuryPoint( 500,100,900) );
00043     m_pCamera->SetUp(MercuryPoint(0,0,1));
00044     AddObject( m_pCamera );
00045     
00046 //  SOUNDMAN->SetListener( m_pCamera);
00047 
00048     //Setup a light
00049     m_light.Init();
00050     m_light.SetY(100);
00051     m_light.SetX(-100);
00052     m_light.SetZ(110);
00053     m_light.SetAttenuation(0.001f, Attenuation::LINEAR);
00054     m_light.SetStatic(true);
00055     DISPLAY->AddLight(&m_light);
00056 
00057     m_bFirstUpdate=true;
00058 }
00059 
00060 void ScreenODETest::Update( const float dTime )
00061 {
00062     MercuryScreen::Update( dTime );
00063     if ( m_bFirstUpdate )
00064     {
00065         m_bFirstUpdate = false;
00066         return;
00067     }
00068     MercuryODEObject * z = m_pWorld.GetODEObjectByName( "Control" );
00069 
00070     int dx=0;
00071     int dy=0;
00072     if ( INPUTMAN->IsButtonDown( button_d ) )
00073         dx -=5000;
00074     if ( INPUTMAN->IsButtonDown( button_a ) )
00075         dx += 5000;
00076     if ( INPUTMAN->IsButtonDown( button_w ) )
00077         dy -= 5000;
00078     if ( INPUTMAN->IsButtonDown( button_s ) )
00079         dy += 5000;
00080 
00081     float Amplitude = SQRT(float((dx*dx)+(dy*dy)));
00082     float AngleToGo=ATAN2(float(dx),float(-dy));
00083 
00084     MercuryPoint Target = m_pWorld.GetODEObjectByName( "Control" )->GetPosition();
00085     MercuryPoint From = m_pCamera->GetPosition();
00086     float AngleDiff=ATAN2((From-Target).x,(From-Target).y);
00087 
00088     float RealAngle = AngleToGo - AngleDiff - 1.57f;
00089 
00090     dBodySetForce( z->m_oBody, COS(RealAngle)*Amplitude,SIN(RealAngle)*Amplitude,0);
00091 }
00092 
00093 void ScreenODETest::Draw()
00094 {
00095     MercuryODEObject * c = m_pWorld.GetODEObjectByName( "Control" );
00096 
00097     int x, y;
00098     INPUTMAN->GetCursorPosition(x, y);
00099     m_pCamera->Tweening.StopTweening();
00100 
00101     //The tween adjusts the eye, up never changes, and center is always the control object
00102     m_pCamera->Tweening.AddCommand( ssprintf( "linear,0.4;x,%f;y,%f;z,%f;", 
00103         c->GetX()+COS(float(x)/300)*70, 
00104         c->GetY()+SIN(float(x)/300)*70,
00105         c->GetZ()+(float(y)/300)*70) );
00106     m_pCamera->SetTarget(c->GetPosition());
00107 
00108     MercuryScreen::Draw();
00109 }
00110 
00111 
00112 /* 
00113  * Copyright (c) 2006, Charles Lohr
00114  * All rights reserved.
00115  *
00116  * Redistribution and use in source and binary forms, with or
00117  * without modification, are permitted provided that the following
00118  * conditions are met:
00119  *  -   Redistributions of source code must retain the above
00120  *      copyright notice, this list of conditions and the following disclaimer.
00121  *  -   Redistributions in binary form must reproduce the above copyright
00122  *      notice, this list of conditions and the following disclaimer in
00123  *      the documentation and/or other materials provided with the distribution.
00124  *  -   Neither the name of the Mercury Engine nor the names of its
00125  *      contributors may be used to endorse or promote products derived from
00126  *      this software without specific prior written permission.
00127  *
00128  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00129  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00130  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00131  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00132  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00133  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00134  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00135  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00136  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00137  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00138  */

Hosted by SourceForge.net Logo