Ufo.hpp
上传用户:jiameide
上传日期:2010-03-01
资源大小:525k
文件大小:15k
- //*******************************************************
- //* INVASION - By Maur韈io Teichmann RItter 1998 *
- //* Please, respect the copyright *
- //*******************************************************
- // OVNI is the same thing as UFO but in portuguese
- // since i磎 brazilian, i think that was better to use the "local" name
- // Here a define a class for each one of the game objects (UFO, BULLET, EXTRA)
- class Ovni {
- private:
- int x;
- int y;
- int mov;
- int AmDown;
- int inc;
- int iExtra;
- int iFrame;
- RECT rcLastPos;
- int ArgPosX;
- int ArgPosY;
- int iExplosionFrame;
- int iQtdeDown;
- int iState;
- int iStr;
- Ovni* Next;
- public:
- int iType;
- DWORD dwLastShoot;
- Ovni()
- {
- inc = 1;
- x = 0;
- y = 0;
- AmDown = 0;
- mov = 0;
- iState = 0;
- iExplosionFrame = 0;
- dwLastShoot = 1000 + (rand() % 500);
- iType = 0;
- iStr = 1;
- ArgPosY = 0;
- ArgPosX = 0;
- iQtdeDown = 80;
- }
- int GetStr()
- {
- return iStr;
- }
- void SetStr(int iQtde)
- {
- iStr = iQtde;
- }
-
- void LowStr(int iQtde)
- {
- iStr -= iQtde;
- }
- void SetQtdeDown(int iNewQtde)
- {
- iQtdeDown = iNewQtde;
- }
- int CreateExtra(int iFlags)
- {
- int iPercent;
- iPercent = rand() % 150;
- iExtra = 0;
- if ((iPercent == 0 || iPercent == 25 || iPercent == 45) &&
- (iFlags != 2))
- iExtra = 2;
- if (iPercent > 0 && iPercent < 11)
- iExtra = 3;
- if (iPercent > 10 && iPercent < 25)
- iExtra = 1;
- if (iPercent > 25 && iPercent < 45)
- iExtra = 4;
- if (iPercent == 45 || iPercent == 46)
- iExtra = 5;
- return iExtra;
- }
- int GetExtra()
- {
- return iExtra;
- }
- void SetAmDown(int newAmDown)
- {
- AmDown = newAmDown;
- }
- int GetAmDown(void)
- {
- return AmDown;
- }
- void SetInc(int newInc)
- {
- inc = newInc;
- }
- int GetInc(void)
- {
- return inc;
- }
- void SetMov(int newmov)
- {
- mov = newmov;
- }
- int GetMov(void)
- {
- return mov;
- }
- void SetXY(int newx, int newy)
- {
- x = newx;
- y = newy;
- rcLastPos.top = y;
- rcLastPos.left = x;
- rcLastPos.bottom = y + 70;
- rcLastPos.right = x + 70;
- if (rcLastPos.bottom > 460)
- rcLastPos.bottom = 460;
- if (rcLastPos.left > 640)
- rcLastPos.left = 640;
- }
- Ovni* GetNext(void)
- {
- return Next;
- }
- void SetNext(Ovni* nxUfo)
- {
- Next = nxUfo;
- }
- int GetX(void)
- {
- return x;
- }
- int GetY(void)
- {
- return y;
- }
- void SetState(int iNewState)
- {
- iState = iNewState;
- }
- void Crash()
- {
- switch (iType)
- {
- case 0:
- inc = inc * (-1);
- break;
- case 2:
- if (mov == 1)
- mov = 0;
- else
- mov = 1;
- break;
- }
- }
- int Move(int iFlags)
- {
- rcLastPos.top = y;
- rcLastPos.left = x;
- rcLastPos.bottom = y + 70;
- rcLastPos.right = x + 70;
- if (rcLastPos.bottom > 460)
- rcLastPos.bottom = 460;
- if (rcLastPos.right > 640)
- rcLastPos.right = 640;
- if (rcLastPos.left < 0)
- rcLastPos.left = 0;
- if (rcLastPos.top < 0)
- rcLastPos.top = 0;
- switch(iType)
- {
- case 0:
- switch (mov)
- {
- case 0:
- x += inc;
- if (y > 300)
- {
- if (inc > 0)
- {
- if (x>650)
- {
- x = 11;
- y = -70;
- mov = 1;
- inc = 1;
- AmDown = 0;
- }
- }
- else
- {
- if (x<-80)
- {
- x = 560;
- y = -70;
- mov = 1;
- inc = 1;
- AmDown = 0;
- }
- }
- }
- else
- {
- if (x == 560 ||
- x == 10)
- {
- AmDown = 0;
- mov = 1;
- inc = 1;
- }
- }
- break;
- case 1:
- y += inc;
- AmDown += inc;
- if (y > 0)
- if (AmDown >= iQtdeDown)
- {
- mov = 0;
- if (x == 0)
- {
- inc = abs(inc);
- }
- if (x == 560)
- {
- inc = abs(inc) * (-1);
- }
- }
- break;
- }
- break;
- case 1:
- switch(mov)
- {
- case 0:
- x -= inc;
- y -= inc;
- if (x <= 0)
- {
- x = 0;
- mov = 1;
- }
- if (y <= 0)
- {
- y = 0;
- mov = 3;
- }
- break;
- case 1:
- x += inc;
- y -= inc;
- if (x >= 560)
- {
- x = 560;
- mov = 0;
- }
- if (y <= 0)
- {
- y = 0;
- mov = 2;
- }
- break;
- case 2:
- x += inc;
- y += inc;
- if (x >= 560)
- {
- x = 560;
- mov = 3;
- }
- if (y >= 300)
- {
- y = 300;
- mov = 1;
- }
- break;
- case 3:
- x -= inc;
- y += inc;
- if (x <= 0)
- {
- x = 0;
- mov = 2;
- }
- if (y >= 300)
- {
- y = 300;
- mov = 0;
- }
- break;
- }
- break;
- case 2:
- switch(mov)
- {
- case 0:
- if (x <= iFlags )
- {
- if (iFlags != NULL)
- mov = 1;
- else
- x += inc;
- }
- else
- x +=inc;
- if (x >= 650)
- {
- x = -80;
- mov = 0;
- }
- break;
- case 1:
- if (x >= iFlags)
- if (iFlags != NULL)
- mov = 0;
- else
- x -= inc;
- else
- x -=inc;
- if (x <= -80)
- {
- x = 650;
- mov = 1;
- }
- break;
- }
- break;
- }
- return TRUE;
- }
- BOOL isExploding()
- {
- if (iState == 0)
- return FALSE;
- else
- return TRUE;
- }
- BOOL DropExtra()
- {
- if (iState == 1 &&
- iExplosionFrame == 12 &&
- iExtra != 0
- )
- return TRUE;
- else
- return FALSE;
- }
- BOOL DestroyMe()
- {
- if (iState == 1 &&
- iExplosionFrame == 20)
- return TRUE;
- return FALSE;
- }
- /* BOOL Repaint(LPDIRECTDRAWSURFACE7 lpDest, LPDIRECTDRAWSURFACE7 lpSource, int iFlags)
- {
- HRESULT hRet;
- if (iFlags == 1 ||
- (iState == 1 && iExplosionFrame > 0))
- {
- rcLastPos.top = y - 2;
- rcLastPos.left = x - 2;
- rcLastPos.bottom = y + 74;
- rcLastPos.right = x + 74;
- if (rcLastPos.bottom > 460)
- rcLastPos.bottom = 460;
- if (rcLastPos.right > 640)
- rcLastPos.right = 640;
- if (rcLastPos.top < 0)
- rcLastPos.top = 0;
- if (rcLastPos.left < 0)
- rcLastPos.left = 0;
- }
- while (1)
- {
- hRet = lpDest->BltFast(rcLastPos.left, rcLastPos.top, lpSource, &rcLastPos, DDBLTFAST_NOCOLORKEY | DDBLTFAST_WAIT);
- if (hRet == DD_OK)
- {
- break;
- }
- if (hRet == DDERR_SURFACELOST)
- {
- return FALSE;
- }
- if (hRet != DDERR_WASSTILLDRAWING)
- break;
- }
- return TRUE;
- }
- */
- BOOL Draw(LPDIRECTDRAWSURFACE7 lpDest, LPDIRECTDRAWSURFACE7 lpSource, int iFlags)
- {
- RECT rcRect;
- HRESULT hRet;
- int iDiffRight;
- int iDiffLeft;
- int iDiffBottom;
- switch(iState)
- {
- case 0:
- rcRect.left = ArgPosX;
- rcRect.top = ArgPosY;
- rcRect.right = ArgPosX + 70;
- rcRect.bottom = ArgPosY + 70;
-
- if (x+70>640)
- {
- iDiffRight = x+70-640;
- rcRect.right -= iDiffRight;
- }
- else
- iDiffLeft = 0;
- if (x<0)
- {
- iDiffLeft = x * (-1);
- rcRect.left += iDiffLeft;
- }
- else
- iDiffLeft = 0;
- if (y < 0)
- {
- iDiffBottom = y * (-1);
- rcRect.top += iDiffBottom;
- }
- else
- iDiffBottom = 0;
- break;
- case 1:
- iDiffBottom = 0;
- if (iExplosionFrame < 5)
- {
- ArgPosX = iExplosionFrame * 70;
- ArgPosY = 0;
- }
- if (iExplosionFrame > 4 &&
- iExplosionFrame < 9)
- {
- ArgPosX = (iExplosionFrame-5) * 70;
- ArgPosY = 70;
- }
- if (iExplosionFrame > 9 &&
- iExplosionFrame < 14)
- {
- ArgPosX = (iExplosionFrame-10) * 70;
- ArgPosY = 140;
- }
- if (iExplosionFrame > 14 &&
- iExplosionFrame < 20)
- {
- ArgPosX = (iExplosionFrame-15) * 70;
- ArgPosY = 210;
- }
- rcRect.left = ArgPosX;
- rcRect.top = ArgPosY;
- rcRect.right = ArgPosX + 70;
- rcRect.bottom = ArgPosY + 70;
- if (x+70>640)
- {
- iDiffRight = x+70-640;
- rcRect.right -= iDiffRight;
- }
- else
- iDiffLeft = 0;
- if (x<0)
- {
- iDiffLeft = x * (-1);
- rcRect.left += iDiffLeft;
- }
- else
- iDiffLeft = 0;
- if (y < 0)
- {
- iDiffBottom = y * (-1);
- rcRect.top += iDiffBottom;
- }
- else
- iDiffBottom = 0;
- iExplosionFrame++;
- break;
- }
- while (1)
- {
- hRet = lpDest->BltFast(x+iDiffLeft, y+iDiffBottom, lpSource, &rcRect, TRUE);
- if (hRet == DD_OK)
- {
- hRet = TRUE;
- break;
- }
- if (hRet == DDERR_SURFACELOST)
- {
- hRet = FALSE;
- break;
- }
- if (hRet != DDERR_WASSTILLDRAWING)
- {
- hRet = TRUE;
- break;
- }
- }
-
- ArgPosX += 70;
- if (ArgPosX == 350)
- {
- ArgPosX = 0;
- ArgPosY += 70;
- if (ArgPosY == 700)
- ArgPosY = 0;
- }
- return hRet;
- }
- };
- class Bullet
- {
- private:
- int x;
- int y;
- RECT rcLastPos;
- int iType;
- Bullet* pNext;
- Bullet* pPrev;
- public:
- int frame;
- Bullet()
- {
- x = 0;
- y = 0;
- pNext = NULL;
- pPrev = NULL;
- frame = 0;
- iType = 0;
- rcLastPos.left = 0;
- rcLastPos.top = 0;
- rcLastPos.right = 0;
- rcLastPos.bottom = 0;
- };
- int GetType()
- {
- return iType;
- }
- void SetType(int iNewType)
- {
- iType = iNewType;
- }
- int GetX()
- {
- return x;
- }
- int GetY()
- {
- return y;
- }
- int GetPower()
- {
- switch(iType)
- {
- case 0:
- return 2;
- break;
- case 2:
- return 1;
- break;
- }
- return 1;
- }
-
- void Move(int Qtde)
- {
- rcLastPos.left = x;
- rcLastPos.top = y;
- rcLastPos.right = x + 20;
- rcLastPos.bottom = y + 20;
- if (rcLastPos.top < 0)
- rcLastPos.top = 0;
- if (rcLastPos.bottom > 455)
- rcLastPos.bottom = 455;
- y -= Qtde;
- }
- void SetXY(int nx, int ny)
- {
- rcLastPos.left = nx;
- rcLastPos.top = ny;
- rcLastPos.right = nx + 20;
- rcLastPos.bottom = ny + 20;
- x = nx;
- y = ny;
- }
- Bullet* GetNext()
- {
- return pNext;
- }
- Bullet* GetPrev()
- {
- return pPrev;
- }
- void SetNext(Bullet* nNext)
- {
- pNext = nNext;
- }
- void SetPrev(Bullet* nPrev)
- {
- pPrev = nPrev;
- }
- BOOL Draw(LPDIRECTDRAWSURFACE7 lpOrigin, LPDIRECTDRAWSURFACE7 lpSource)
- {
- RECT rcRect;
- HRESULT hRet;
- int iClipTop;
- int iClipBottom;
-
- if (y < 0)
- iClipTop = y * -1;
- else
- iClipTop = 0;
- if (y+20 > 455)
- iClipBottom = y+20-455;
- else
- iClipBottom = 0;
- if (iType == 2)
- frame = 0;
- rcRect.left = frame * 20;
- rcRect.top = 0 + iClipTop;
- rcRect.right = frame * 20 + 20;
- rcRect.bottom = 20 - iClipBottom;
- frame++;
- if (frame == 11 && iType == 0)
- frame = 0;
- if (frame == 20 && iType == 1)
- frame = 0;
-
- while (1)
- {
- hRet = lpOrigin->BltFast(x + iClipTop, y, lpSource, &rcRect, DDBLTFAST_SRCCOLORKEY);
-
- if (hRet == DD_OK)
- {
- break;
- }
- if (hRet == DDERR_SURFACELOST)
- {
- return FALSE;
- }
- if (hRet != DDERR_WASSTILLDRAWING)
- break;
- }
- return TRUE;
- }
- // Flags:
- //
- // 1 - Draw background
- // 2 - Draw bullet
- /*
-
- BOOL Repaint(LPDIRECTDRAWSURFACE7 lpOrigin, LPDIRECTDRAWSURFACE7 lpSource, int iFlags)
- {
- HRESULT hRet;
- int iCorrection = 0;
- if (iFlags == 1)
- {
- rcLastPos.top = y;
- rcLastPos.left = x;
- rcLastPos.bottom = y + 20;
- rcLastPos.right = x + 20;
- if (rcLastPos.bottom > 460)
- rcLastPos.bottom = 460;
- }
- while (1)
- {
- hRet = lpOrigin->BltFast(rcLastPos.left, rcLastPos.top, lpSource, &rcLastPos, DDBLTFAST_NOCOLORKEY | DDBLTFAST_WAIT);
- if (hRet == DD_OK)
- {
- break;
- }
- if (hRet == DDERR_SURFACELOST)
- {
- return FALSE;
- }
- if (hRet != DDERR_WASSTILLDRAWING)
- break;
- }
- return TRUE;
- }
- */
- };
- //处理奖子类型的类,奖子定义在图extras.bmp中,它是一副500×125的图像,有五种类型的奖子,每种奖子有20帧,
- //平均每个奖子是25*25的分辨率。
- class Extra
- {
- private:
- int x;
- int y;
- RECT rcLastPos;
- int iType; //奖子类型,从extras.bmp可以看出共有5种类型
-
- Extra* pNext;
- Extra* pPrev;
- public:
- int frame;//帧编号,从extras.bmp可以看出共有20帧
- Extra()
- {
- x = 0;
- y = 0;
- pNext = NULL;
- pPrev = NULL;
- frame = 0;
- iType = 0;
- rcLastPos.left = 0;
- rcLastPos.top = 0;
- rcLastPos.right = 0;
- rcLastPos.bottom = 0;
- };
- int GetType()
- {
- return iType;
- }
- void SetType(int iNewType)
- {
- iType = iNewType;
- }
- int GetX()
- {
- return x;
- }
- int GetY()
- {
- return y;
- }
- void Move(int Qtde)
- {
- rcLastPos.left = x;
- rcLastPos.top = y;
- rcLastPos.right = x + 25;
- rcLastPos.bottom = y + 25;
- if (rcLastPos.bottom > 455)
- rcLastPos.bottom = 455;
- //Qtde是一个负值,因为这种奖子都是垂直下落的,即y值需要不断增加
- y -= Qtde;
- }
- void SetXY(int nx, int ny)
- {
- rcLastPos.left = nx;
- rcLastPos.top = ny;
- rcLastPos.right = nx + 25;
- rcLastPos.bottom = ny + 25;
- x = nx;
- y = ny;
- }
- Extra* GetNext()
- {
- return pNext;
- }
- Extra* GetPrev()
- {
- return pPrev;
- }
- void SetNext(Extra* nNext)
- {
- pNext = nNext;
- }
- void SetPrev(Extra* nPrev)
- {
- pPrev = nPrev;
- }
- BOOL Draw(LPDIRECTDRAWSURFACE7 lpOrigin, LPDIRECTDRAWSURFACE7 lpSource)
- {
- RECT rcRect;
- HRESULT hRet;
- int iClipTop;
- int iClipBottom;
-
- if (y < 0)
- iClipTop = y * -1;
- else
- iClipTop = 0;
- if (y+25 > 455)
- iClipBottom = y+25-455;
- else
- iClipBottom = 0;
- rcRect.left = frame * 25;
- rcRect.top = ((iType - 1) * 25) + iClipTop;
- rcRect.right = frame * 25 + 25;
- rcRect.bottom = ((iType - 1) * 25) + 25 - iClipBottom;
- //帧数编号加1
- frame++;
- if (frame == 20)
- frame = 0;
-
- while (1)
- {
- hRet = lpOrigin->BltFast(x , y, lpSource, &rcRect, DDBLTFAST_SRCCOLORKEY);
-
- if (hRet == DD_OK)
- {
- break;
- }
- if (hRet == DDERR_SURFACELOST)
- {
- return FALSE;
- }
- if (hRet != DDERR_WASSTILLDRAWING)
- break;
- }
- return TRUE;
- }
- // Flags:
- //
- // 1 - Draw background
- // 2 - Draw bullet
- /*
- BOOL Repaint(LPDIRECTDRAWSURFACE lpDest, LPDIRECTDRAWSURFACE lpSource, int iFlags)
- {
- HRESULT hRet;
- int iCorrection = 0;
- if (iFlags == 1)
- {
- rcLastPos.top = y;
- rcLastPos.left = x;
- rcLastPos.bottom = y + 25;
- rcLastPos.right = x + 25;
- if (rcLastPos.bottom > 455)
- rcLastPos.bottom = 455;
- }
- while (1)
- {
- hRet = lpDest->BltFast(rcLastPos.left, rcLastPos.top, lpSource, &rcLastPos, DDBLTFAST_NOCOLORKEY | DDBLTFAST_WAIT);
- if (hRet == DD_OK)
- {
- break;
- }
- if (hRet == DDERR_SURFACELOST)
- {
- return FALSE;
- }
- if (hRet != DDERR_WASSTILLDRAWING)
- break;
- }
- return TRUE;
- }
- */
- };
- // The alpha strucute is used to map the alphabet bitmap, so that we can
- // bli the alphabet to the screen
- struct Alpha
- {
- unsigned int start;
- unsigned int end;
- };
- const Alpha AA[40] =
- {
- {0,14},{19,27},{31,46},{47,61},{65,78},{80,94},
- {96,110},{114,125},{128,143},{145,159},{160,175},
- {176,191},{193,206},{207,222},{224,237},{238,252},{253,267},
- {269,284},{285,291},{292,307},{308,323},{324,338},{340,364},
- {365,380},{382,397},{398,413},{414,429},{430,444},{447,460},
- {464,475},{477,492},{494,508},{509,533},{533,548},{550,564},
- {564,579},{579,599},{600,610},{611,615},{617,620}
- };