#include "CommonAPI.h"
#include "LessonX.h"
#include
const int GRID_COUNT=12;
const int MAX_LEVEL=3;
const int RIGID_BLOCK=1;
const int BLACK_HOLE=2;
const int GOLF_EXIT=3;
int g_iGridData[GRID_COUNT][GRID_COUNT];
int iControlStartX = 0, iControlStartY = 0;
float g_fGridStartX=-27.5f,g_fGridStartY=-27.5f,g_fGridSize=5.f;
int g_iCurLevel=1,g_iRigidBlockCount=0,g_iBlackHoleCount=0,g_iGolfExitCount=0;
int SpritePosXToIndexX ( const float fPosX )
{
const float fLeftSide = g_fGridStartX - g_fGridSize / 2.f;
const float fRightSide = fLeftSide + g_fGridSize * GRID_COUNT;
if( fPosX < fLeftSide || fPosX > fRightSide )
return -1;
int iIndexX = (int)( (fPosX - fLeftSide) / g_fGridSize );
return iIndexX;
}
int SpritePosYToIndexY( const float fPosY )
{
const float fTopSide = g_fGridStartY - g_fGridSize / 2.f;
const float fBottomSide = fTopSide + g_fGridSize * GRID_COUNT;
if( fPosY < fTopSide || fPosY > fBottomSide )
return -1;
int iIndexY = (int)( (fPosY - fTopSide) / g_fGridSize );
return iIndexY;
}
void MoveSpriteToBlock( const char *szName, const int iIndexX, const int iIndexY )
{
float fPosX = g_fGridStartX + iIndexX * g_fGridSize;
float fPosY = g_fGridStartY + iIndexY * g_fGridSize;
dSetSpritePosition( szName, fPosX, fPosY );
int iLoopX = 0, iLoopY = 0;
for( iLoopY = 0; iLoopY < GRID_COUNT; iLoopY++ )
{
for( int iLoopX = 0; iLoopX < GRID_COUNT; iLoopX++ )
{
if( 0 == g_iGridData[iLoopY][iLoopX] )
{
continue;
}
if( RIGID_BLOCK == g_iGridData[iLoopY][iLoopX] )
{
szName = dMakeSpriteName( "RigidBlock", g_iRigidBlockCount );
dCloneSprite( "RigidBlockTemplate", szName );
MoveSpriteToBlock( szName, iLoopX, iLoopY );
g_iRigidBlockCount++;
}
else if( BLACK_HOLE == g_iGridData[iLoopY][iLoopX] )
{
szName = dMakeSpriteName( "BlackHole", g_iBlackHoleCount );
dCloneSprite( "BlackHoleTemplate", szName );
MoveSpriteToBlock( szName, iLoopX, iLoopY );
g_iBlackHoleCount++;
}
else if( GOLF_EXIT == g_iGridData[iLoopY][iLoopX] )
{
szName = dMakeSpriteName( "GolfExit", g_iGolfExitCount );
dCloneSprite( "GolfExitTemplate", szName );
MoveSpriteToBlock( szName, iLoopX, iLoopY );
g_iGolfExitCount++;
}
dSetSpriteLinearVelocity( "ControlBall", 0.f, 0.f );
MoveSpriteToBlock( "ControlBall", iControlStartX, iControlStartY );
MoveSpriteToBlock( "GolfArrow", iControlStartX, iControlStartY );
dSetSpriteVisible( "GolfArrow", 1 );
}
}
}
int m_iLevelData1[GRID_COUNT][GRID_COUNT] =
{
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, 0, 0, 0},
{0, 0, 0, RIGID_BLOCK, 0, 0, 0, 0, RIGID_BLOCK, 0, 0, 0},
{0, 0, 0, RIGID_BLOCK, 0, 0, 0, 0, RIGID_BLOCK, 0, 0, 0},
{0, 0, 0, RIGID_BLOCK, 0, 0, 0, 0, BLACK_HOLE, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, GOLF_EXIT, RIGID_BLOCK, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
int m_iLevelData2[GRID_COUNT][GRID_COUNT]=
{
{0, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, 0, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, 0},
{0, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, 0},
{0, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, 0},
{0, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, 0},
{0, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, 0},
{0, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, 0},
{0, RIGID_BLOCK, 0, 0, 0, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, 0, 0, RIGID_BLOCK, 0},
{0, RIGID_BLOCK, 0, 0, 0, 0, 0, RIGID_BLOCK, 0, 0, RIGID_BLOCK, 0},
{0, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, GOLF_EXIT, RIGID_BLOCK, RIGID_BLOCK, 0},
{0, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, 0},
{0, RIGID_BLOCK, 0, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, 0, RIGID_BLOCK, 0}
};
int m_iLevelData3[GRID_COUNT][GRID_COUNT]=
{
{0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, RIGID_BLOCK, 0, 0},
{0, 0, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, RIGID_BLOCK},
{RIGID_BLOCK, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, GOLF_EXIT, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, RIGID_BLOCK, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, 0},
{0, 0, 0, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, 0},
{0, 0, 0, 0, BLACK_HOLE, RIGID_BLOCK, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
int g_iPlayState=1;
int g_iMoveState=0;
void Init()
{
int iLoop = 0;
char *szName = NULL;
for( iLoop = 0; iLoop < g_iRigidBlockCount; iLoop++ )
{
szName = dMakeSpriteName( "RigidBlock", iLoop );
dDeleteSprite( szName );
}
for( iLoop = 0; iLoop < g_iBlackHoleCount; iLoop++ )
{
szName = dMakeSpriteName( "BlackHole", iLoop );
dDeleteSprite( szName );
}
for( iLoop = 0; iLoop < g_iGolfExitCount; iLoop++ )
{
szName = dMakeSpriteName( "GolfExit", iLoop );
dDeleteSprite( szName );
}
g_iRigidBlockCount = 0;
g_iBlackHoleCount = 0;
g_iGolfExitCount = 0;
g_iMoveState=0;
switch( g_iCurLevel )
{
case 2:
{
iControlStartX = 5;
iControlStartY = 9;
memcpy( g_iGridData, m_iLevelData2, sizeof(int) * GRID_COUNT * GRID_COUNT );
}
break;
case 3:
{
iControlStartX = 3;
iControlStartY = 6;
memcpy( g_iGridData, m_iLevelData3, sizeof(int) * GRID_COUNT * GRID_COUNT );
}
break;
case 1:
default:
{
iControlStartX = 5;
iControlStartY = 6;
memcpy( g_iGridData, m_iLevelData1, sizeof(int) * GRID_COUNT * GRID_COUNT );
}
break;
};
}
int PASCAL WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
if( !dInitGameEngine( hInstance, lpCmdLine ) )
return 0;
g_iMoveState=0;
dSetWindowTitle("Lesson");
while( dEngineMainLoop() )
{
float fTimeDelta = dGetTimeDelta();
Init();
if( 0 != g_iMoveState )
{
float fPosX = dGetSpritePositionX( "ControlBall" );
float fPosY = dGetSpritePositionY( "ControlBall" );
int iIndexX = SpritePosXToIndexX( fPosX );
int iIndexY = SpritePosYToIndexY( fPosY );
if( iIndexX < 0 || iIndexX >= GRID_COUNT || iIndexY < 0 || iIndexY >= GRID_COUNT )
{
return 0;
}
float fNextPosX = fPosX;
float fNextPosY = fPosY;
if( 1 == g_iMoveState )
{
fNextPosY -= g_fGridSize * 0.5f;
}
else if( 2 == g_iMoveState )
{
fNextPosY += g_fGridSize * 0.5f;
}
else if( 3 == g_iMoveState )
{
fNextPosX -= g_fGridSize * 0.5f;
}
else if( 4 == g_iMoveState )
{
fNextPosX += g_fGridSize * 0.5f;
}
if( RIGID_BLOCK == g_iGridData[iIndexY][iIndexX] )
{
g_iMoveState = 0;
dSetSpriteLinearVelocity( "ControlBall", 0.f, 0.f );
dSetSpriteVisible( "GolfArrow", 1 );
MoveSpriteToBlock( "ControlBall", iIndexX, iIndexY );
MoveSpriteToBlock( "GolfArrow", iIndexX, iIndexY );
}
else if( BLACK_HOLE == g_iGridData[iIndexY][iIndexX] )
{
g_iPlayState = 1;
}
else if( GOLF_EXIT == g_iGridData[iIndexY][iIndexX] )
{
g_iPlayState = 1;
g_iCurLevel++;
if( g_iCurLevel > MAX_LEVEL )
g_iCurLevel = 1;
}
}
GameMainLoop( fTimeDelta );
};
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( 1 != g_iPlayState || 0 != g_iMoveState ){
printf("%d %d\n",g_iPlayState,g_iMoveState);
return ;
}
printf(":%d %d\n",g_iPlayState,g_iMoveState);
float fPosX = dGetSpritePositionX( "ControlBall" );
float fPosY = dGetSpritePositionY( "ControlBall" );
int iIndexX = SpritePosXToIndexX( fPosX );
int iIndexY = SpritePosYToIndexY( fPosY );
if( iIndexX < 0 || iIndexX >= GRID_COUNT || iIndexY < 0 || iIndexY >= GRID_COUNT )
return;
if( KEY_UP == iKey )
{
if( iIndexY > 0 && RIGID_BLOCK == g_iGridData[iIndexY - 1][iIndexX] )
return;
g_iMoveState = 1;
dSetSpriteLinearVelocityY( "ControlBall", -30.f );
dSetSpriteVisible( "GolfArrow", 0 );
}
else if( KEY_DOWN == iKey )
{
if( iIndexY < GRID_COUNT - 1 && RIGID_BLOCK == g_iGridData[iIndexY + 1][iIndexX] )
return;
g_iMoveState = 2;
dSetSpriteLinearVelocityY( "ControlBall", 30.f );
dSetSpriteVisible( "GolfArrow", 0 );
}
else if( KEY_LEFT == iKey )
{
if( iIndexX > 0&&RIGID_BLOCK == g_iGridData[iIndexY][iIndexX -1])
return;
g_iMoveState = 3;
dSetSpriteLinearVelocityX( "ControlBall", -30.f );
dSetSpriteVisible( "GolfArrow", 0 );
}
else if( KEY_RIGHT == iKey )
{
if(iIndexX<GRID_COUNT-1&&RIGID_BLOCK == g_iGridData[iIndexY][iIndexX+ 1])
return;
g_iMoveState = 4;
dSetSpriteLinearVelocityX( "ControlBall", 30.f );
dSetSpriteVisible( "GolfArrow", 0 );
}
OnKeyDown(iKey, iAltPress, iShiftPress, iCtrlPress);
}
void dOnKeyUp( const int iKey )
{
OnKeyUp(iKey);
}
void dOnSpriteColSprite( const char *szSrcName, const char *szTarName )
{
OnSpriteColSprite(szSrcName, szTarName);
}
void dOnSpriteColWorldLimit( const char *szName, const int iColSide )
{
OnSpriteColWorldLimit(szName, iColSide);
}