#include "CommonAPI.h"
#include "LessonX.h"
#include
#include
using namespace std;
const int TARGET_COUNT=3;
int g_iFireState=0;
int g_iTargetHit[TARGET_COUNT];
int g_iPlayState=0;
char g_szTargetName[TARGET_COUNT][32] = {"DandanTarget1", "DandanTarget2", "DandanTarget3"};
const float g_fMaxRotation = 350.f;
double g_fGunRotation=350.f;
double g_fGunStrength=0.f;
int g_iCalStrength =0;
int g_fKeyUp=0;
int g_fKeyDown=0;
const float g_fBumbForceY = 10.f;
float g_fRoundTime = 0.f;
int g_iMovdetime=0;
double g_iMovedeadtime=0.f;
void ProcessBumbHit( const int iHitState, const char *szBumbName, const char *szTargetName )
{
for(int iLoop = 0; iLoop <TARGET_COUNT; iLoop++ )
{
if( strcmp(g_szTargetName[iLoop], szTargetName)==0)
{
g_iTargetHit[iLoop]++;
if( 1 == g_iTargetHit[iLoop] )
{
dAnimateSpritePlayAnimation(g_szTargetName[iLoop], "DandanTargetAnimation2", 0 );
}
else
{
dAnimateSpritePlayAnimation(g_szTargetName[iLoop], "DandanTargetAnimation3", 0 );
}
if( g_iTargetHit[iLoop] >= 3 )
dSetSpriteVisible( g_szTargetName[iLoop], 0 );
break;
}
}
float fPosX=dGetSpritePositionX(szBumbName);
float fPosY=dGetSpritePositionY(szBumbName);
if( 1 == iHitState || 2 == iHitState )
{
dPlayEffect( "BumbExplode", 1.f, fPosX, fPosY, 0.f );
}
dDeleteSprite(szBumbName);
g_iMovdetime=1;
}
int IsGameWin()
{
int iLoop = 0;
for( iLoop = 0; iLoop < TARGET_COUNT; iLoop++ )
{
if( g_iTargetHit[iLoop] < 3 )
return 0;
}
return 1;
}
int PASCAL WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
if( !dInitGameEngine( hInstance, lpCmdLine ) )
return 0;
g_iFireState=0;
g_iPlayState=2;
dSetSpriteVisible("win",0);
int iLoop = 0;
float fPosX = 0, fPosY = 0;
for( iLoop = 0; iLoop < TARGET_COUNT; iLoop++ )
{
g_iTargetHit[iLoop] = 0;
fPosX = dRandomRange( 0, 45 );
fPosY = dGetSpritePositionY( g_szTargetName[iLoop] );
dSpriteMoveTo( g_szTargetName[iLoop], fPosX, fPosY, 40.f, 1 );
dSetSpriteVisible( g_szTargetName[iLoop], 1 );
dAnimateSpritePlayAnimation( g_szTargetName[iLoop], "DandanTargetAnimation1",0);
}
dSetWindowTitle("Lesson");
while( dEngineMainLoop() )
{
float fTimeDelta = dGetTimeDelta();
if(IsGameWin()){
dSetSpriteVisible("win",1);
}
if(g_iCalStrength == 1)
{
g_fGunStrength+=3.0f;
g_fGunStrength=min(200.0,g_fGunStrength);
}
g_fRoundTime+=0.1f;
if(g_fKeyUp)
{
g_fGunRotation+=0.15f;
g_fGunRotation=min(g_fGunRotation,350.0);
}
if(g_fKeyDown)
{
g_fGunRotation-=0.15f;
g_fGunRotation=max(g_fGunRotation,280.0);
}
float fVelocityX = dRotationToVectorX( g_fGunRotation ) * g_fGunStrength;
float fVelocityY = dRotationToVectorY( g_fGunRotation ) * g_fGunStrength;
float fMass = dGetSpriteMass("BumbTemplate");
float fHalfTime = - fVelocityY / (g_fBumbForceY / fMass);
float fForceVelY = g_fBumbForceY / fMass;
float fTime = 0.f;
float fSimDelta = 0.0333f;
float fOldPosX=dGetSpriteLinkPointPosX( "DandanGun", 1 );
float fOldPosY=dGetSpriteLinkPointPosY( "DandanGun", 1);
for( fTime = 0.f; fTime < fHalfTime; fTime += fSimDelta )
{
float fNewPosX = fOldPosX + fVelocityX * fSimDelta;
float fNewPosY = fOldPosY + (fVelocityY + fForceVelY * fTime) * fSimDelta;
dDrawLine( fOldPosX, fOldPosY, fNewPosX, fNewPosY, 2.f, 0, 0, 255, 0, 255 );
fOldPosX = fNewPosX;
fOldPosY = fNewPosY;
GameMainLoop( fTimeDelta );
};
dSetSpriteRotation("DandanGun",g_fGunRotation);
dSetTextValueFloat("StrengthText", g_fGunStrength);
dSetTextValueFloat("DegreeText", g_fGunRotation);
if(g_iMovdetime)
{
g_iMovedeadtime+=0.07f;
if(g_iMovdetime&&g_iMovedeadtime>=1.5f)
{
g_iMovdetime=0;
g_iMovedeadtime=0.f;
int iLoop = 0;
float fPosX = 0, fPosY = 0;
for( iLoop = 0; iLoop < TARGET_COUNT; iLoop++ )
{
if( g_iTargetHit[iLoop] >= 3 )
continue;
fPosX = dRandomRange( 0, 45 );
fPosY = dGetSpritePositionY( g_szTargetName[iLoop] );
dSpriteMoveTo( g_szTargetName[iLoop], fPosX, fPosY, 40.f, 1 );
}
}
}
}
dShutdownGameEngine();
return 0;
}
void dOnMouseMove( const float fMouseX, const float fMouseY )
{
OnMouseMove(fMouseX, fMouseY );
}
void dOnMouseClick( const int iMouseType, const float fMouseX, const float fMouseY )
{
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 )
{
if( KEY_SPACE == iKey && 2 == g_iPlayState && 0 == g_iFireState )
{
g_iCalStrength = 1;
}
if(KEY_UP == iKey)
{
g_fKeyUp=1;
}
if(KEY_DOWN == iKey)
{
g_fKeyDown=1;
}
OnKeyDown(iKey, iAltPress, iShiftPress, iCtrlPress);
}
void dOnKeyUp( const int iKey )
{
if( KEY_SPACE == iKey && 2 == g_iPlayState && 0 == g_iFireState && g_fRoundTime>=3.0f)
{
g_fRoundTime=0.f;
g_iCalStrength = 0;
int iLoop = 0 ;
float fGunRotation = g_fGunRotation - 10.f;
float fGunStrength = g_fGunStrength - 10.f;
char *szName = NULL;
float fPosX=dGetSpriteLinkPointPosX( "DandanGun", 1 );
float fPosY=dGetSpriteLinkPointPosY( "DandanGun", 1);
for( iLoop = 0; iLoop < 3; iLoop++ )
{
szName = dMakeSpriteName( "DandanBumb", iLoop );
dCloneSprite( "BumbTemplate", szName );
dSetSpritePosition( szName, fPosX, fPosY);
dSetSpriteLinearVelocityPolar( szName, fGunStrength, fGunRotation );
dSetSpriteConstantForceY( szName, g_fBumbForceY );
fGunRotation += 10.f;
fGunStrength += 10.f;
}
g_fGunStrength=0.f;
}
if(KEY_UP == iKey)
{
g_fKeyUp=0;
}
if(KEY_DOWN == iKey)
{
g_fKeyDown=0;
}
OnKeyUp(iKey);
}
void dOnSpriteColSprite( const char *szSrcName, const char *szTarName )
{
if( 2 != g_iPlayState )
{
printf("returncol\n");
}
if(strstr(szSrcName,"DandanBumb"))
{
ProcessBumbHit(1,szSrcName,szTarName);
}
OnSpriteColSprite(szSrcName, szTarName);
}
void dOnSpriteColWorldLimit( const char *szName, const int iColSide )
{
if( 2 != g_iPlayState )
{
printf("returnlimit\n");
return;
}
if( strstr( szName, "BumbTemplate" ) )
ProcessBumbHit( 0, szName, "" );
OnSpriteColWorldLimit(szName, iColSide);
}