#include "CommonAPI.h"
#include "LessonX.h"
#include
#include
int g_iPlayState= 0,g_iBallNumber=0;
double g_fRotateTime= 0.f;
double g_fHoleRollTime = 0.f,g_fOldRotation=0.f,fDeltaTime=0.001f;
char g_szBallName[64];
int g_iHoleNumber[8];
char g_szHoleName[8][64];
int PASCAL WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
if( !dInitGameEngine( hInstance, lpCmdLine ) )
return 0;
dSetWindowTitle("Lesson");
int iLoop = 0;
dSetSpriteVisible("win",0);
for( iLoop = 0; iLoop < 8; iLoop++ )
{
g_iHoleNumber[iLoop] = iLoop;
strcpy( g_szHoleName[iLoop], dMakeSpriteName( "BallHole", iLoop ) );
}
int iNewBallNumber = dRandomRange( 0, 7 );
if( iNewBallNumber != g_iBallNumber )
{
dSetSpritePositionY( g_szBallName, 50.f );
g_iBallNumber = iNewBallNumber;
strcpy( g_szBallName, dMakeSpriteName("Ball", g_iBallNumber) );
dSetSpritePositionX( g_szBallName, -30.f );
}
int iPosY = dRandomRange( -25, 25 );
dSetSpritePositionY(g_szBallName, (float)iPosY );
static int iInited = 0;
if( !iInited )
{
iInited = 1;
g_fOldRotation = dGetSpriteRotation( "BallCue" );
dShowCursor( 0 );
}
while( dEngineMainLoop() )
{
float fTimeDelta = dGetTimeDelta();
for(int iLoop = 0; iLoop < 8; iLoop++ )
{
g_iHoleNumber[iLoop] = iLoop;
strcpy( g_szHoleName[iLoop], dMakeSpriteName( "BallHole", iLoop ) );
}
if( 0 == g_iPlayState )
{
float fBallPosX = dGetSpritePositionX( g_szBallName );
float fBallPosY = dGetSpritePositionY( g_szBallName );
float fCuePosX = dGetSpritePositionX( "BallCue" );
float fCuePosY = dGetSpritePositionY( "BallCue" );
float fVectorX = fBallPosX - fCuePosX;
float fVectorY = fBallPosY - fCuePosY;
if( fVectorX > 0.001f || fVectorX < -0.001f || fVectorY > 0.001f || fVectorY < -0.001f )
{
float fVectorSize = sqrt( fVectorX * fVectorX + fVectorY * fVectorY );
float fDirX = fVectorX / fVectorSize;
float fDirY = fVectorY / fVectorSize;
int iLoop = 0;
float fLastPosX = fBallPosX, fLastPosY = fBallPosY;
float fNewPosX = 0.f, fNewPosY = 0.f;
float fPosStep = fVectorSize / 10.f;
for( iLoop = 0; iLoop < 12; iLoop++ )
{
fNewPosX = fLastPosX + fDirX * fPosStep;
fNewPosY = fLastPosY + fDirY * fPosStep;
if( iLoop % 2 == 0 )
dDrawLine( fLastPosX, fLastPosY, fNewPosX, fNewPosY, 2.f, 0, 0, 255, 0, 255 );
fLastPosX = fNewPosX;
fLastPosY = fNewPosY;
}
}
}
else
{
float fVelX = dGetSpriteLinearVelocityX( g_szBallName );
float fVelY = dGetSpriteLinearVelocityY( g_szBallName );
float fSpeedValue = sqrt( fVelX * fVelX + fVelY * fVelY );
if( fSpeedValue < 30.f )
{
float fPosX = dGetSpritePositionX( g_szBallName );
float fPosY = dGetSpritePositionY( g_szBallName );
if( dIsPointInSprite(g_szHoleName[g_iBallNumber], fPosX, fPosY ) )
{
g_iPlayState = 1;
dSetSpriteVisible("win",1);
}
if( fSpeedValue < 0.50f )
{
g_iPlayState = 0;
}
}
}
int iLoop = 0;
int iLastData = 0;
float fTempPosY = 0.f;
float fLastPosY = 0.f;
char szLastName[64];
g_fHoleRollTime += fDeltaTime;
if( g_fHoleRollTime > 1.f )
{
g_fHoleRollTime -= 1.f;
iLastData = g_iHoleNumber[7];
fLastPosY = dGetSpritePositionY( g_szHoleName[7] );
strcpy( szLastName, g_szHoleName[7] );
for( iLoop = 7; iLoop > 0; iLoop-- )
{
g_iHoleNumber[iLoop] = g_iHoleNumber[iLoop - 1];
fTempPosY = dGetSpritePositionY( g_szHoleName[iLoop - 1] );
dSetSpritePositionY( g_szHoleName[iLoop - 1], fLastPosY );
fLastPosY = fTempPosY;
strcpy( g_szHoleName[iLoop], g_szHoleName[iLoop - 1] );
}
g_iHoleNumber[0] = iLastData;
strcpy( g_szHoleName[0], szLastName );
dSetSpritePositionY( g_szHoleName[0], fLastPosY );
}
GameMainLoop( fTimeDelta );
};
dShutdownGameEngine();
return 0;
}
void dOnMouseMove( const float fMouseX, const float fMouseY )
{
dSetSpritePosition( "BallCue", fMouseX, fMouseY );
OnMouseMove(fMouseX, fMouseY );
}
void dOnMouseClick( const int iMouseType, const float fMouseX, const float fMouseY )
{
if( 2 != iMouseType || 0 != g_iPlayState ){
}
g_iPlayState = 1;
g_fRotateTime =0.2f;
dSetSpriteRotation( "BallCue", g_fOldRotation + 10.f );
float fPosX = dGetSpritePositionX( g_szBallName );
float fPosY = dGetSpritePositionY( g_szBallName );
float fVectorX = fPosX - fMouseX;
float fVectorY = fPosY - fMouseY;
fVectorX *= 12.f;
fVectorY *= 12.f;
dSetSpriteLinearVelocity( g_szBallName, fVectorX, fVectorY );
dSetSpriteDamping( g_szBallName, 1.6f );
if( g_fRotateTime > 0.f )
{
g_fRotateTime -= fDeltaTime;
if( g_fRotateTime <= 0.f )
{
dSetSpriteRotation( "BallCue", g_fOldRotation );
}
}
OnMouseClick(iMouseType, fMouseX, fMouseY);
}
void dOnMouseUp( const int iMouseType, const float fMouseX, const float fMouseY )
{
OnMouseUp(iMouseType, fMouseX, fMouseY);
}
void dOnKeyDown( const int iKey, const int iAltPress, const int iShiftPress, const int iCtrlPress )
{
OnKeyDown(iKey, iAltPress, iShiftPress, iCtrlPress);
}
void dOnKeyUp( const int iKey )
{
OnKeyUp(iKey);
}
void dOnSpriteColSprite( const char *szSrcName, const char *szTarName )
{
if(strcmp( szSrcName, g_szBallName ) == 0 || strcmp( szTarName, g_szBallName ) == 0)
{
if( strstr( szSrcName, "VerSide" ) || strstr( szTarName, "VerSide" ) )
{
float fVelX = dGetSpriteLinearVelocityX( g_szBallName );
dSetSpriteLinearVelocityX( g_szBallName, fVelX * -1.f );
}
else if( strstr( szSrcName, "HorSide" ) || strstr( szTarName, "HorSide" ) )
{
float fVelY = dGetSpriteLinearVelocityY( g_szBallName );
dSetSpriteLinearVelocityY( g_szBallName, fVelY * -1.f );
}
}
OnSpriteColSprite(szSrcName, szTarName);
}
void dOnSpriteColWorldLimit( const char *szName, const int iColSide )
{
OnSpriteColWorldLimit(szName, iColSide);
}