【VC开源代码栏目提醒】:网学会员为广大网友收集整理了,GAMEBOARD.CPP,希望对大家有所帮助!
/*****************************************************************************
*
* GameBoard.cpp
*
* Electrical Engineering Faculty - Software Lab
* Spring semester 1998
*
* Tanks game
*
* Module description: Implements the game board game object.
*
*
* Authors: Eran Yariv - 28484475
* Moshe Zur - 24070856
*
*
* Date: 23/09/98
*
******************************************************************************/
#include "stdafx.h"
#include <stdlib.h>
#include <ImageManager.h>
#include <Tanks.h>
#include <GameBoard.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*------------------------------------------------------------------------------
Function: React
Purpose: Returns the game board reaction to the given game object. Since the
game board is a passive object, the reaction can only be blocked or
non-blocked, and the collision detection is done for every map block
the object overlaps.
Input: pTo - the game object we are reacting to
Output: The Reaction object
Remarks:
------------------------------------------------------------------------------*/
CReaction
CGameBoard::React(CGameObject *pTo)
{
switch (pTo->GetType())
{ // We only react to moving object in the lower level and upper level:
case TANK:
case SHELL:
case BULLET:
{
CPoint &ObjPos = pTo->GetPos();
CSize &ObjSize = pTo->GetDimensions();
// Calc the indexes of the map blocks the object is overlapping:
UINT ul = ObjPos.x / MAP_BLOCK_SCALE,
ur = (ObjPos.x + ObjSize.cx -1) / MAP_BLOCK_SCALE,
ut = ObjPos.y / MAP_BLOCK_SCALE,
ub = (ObjPos.y + ObjSize.cy -1) / MAP_BLOCK_SCALE;
TerrainType ter = TERR_EMPTY;
// Init CurrPos and CurrSize to hold the rect we are currently checking against:
CPoint CurrPos;
CSize CurrSize(MAP_BLOCK_SCALE, MAP_BLOCK_SCALE);
UINT x, y;
// Check against every rect we are overlapping:
for (x = ul, CurrPos.x = ul * MAP_BLOCK_SCALE;
x <= ur;
x++, CurrPos.x += MAP_BLOCK_SCALE)
for (y = ut, CurrPos.y = ut * MAP_BLOCK_SCALE;
y <= ub;
y++, CurrPos.y += MAP_BLOCK_SCALE) {
//CRect TempRect(CurrPos, CurrSize);
if (CollidesWith(pTo, CRect(CurrPos, CurrSize))) {
ter = max (ter, ReadMap (x,y));
}
}
return CReaction (0, ter, BONUS_NONE);
}
default:
return 0;
}
}
BOOL
CGameBoard::PickWallSeed (UINT &x, UINT &y)
{
UINT uMaxTries= MAP_X_CELLS * MAP_Y_CELLS * 3;
while (uMaxTries-- > 0)
{
x = TANK_BLOCK_WIDTH + Rand (MAP_X_CELLS - 1 - (TANK_BLOCK_WIDTH * 2));
y = TANK_BLOCK_HEIGHT + Rand (MAP_Y_CELLS - 1 - (TANK_BLOCK_HEIGHT * 2));
BOOL bFoundPlace = TRUE;
for (int i = -TANK_BLOCK_WIDTH; i <= TANK_BLOCK_WIDTH && bFoundPlace ; i++)
for (int j = -TANK_BLOCK_HEIGHT; j <= TANK_BLOCK_HEIGHT && bFoundPlace; j++)
if (TERR_EMPTY != ReadMap(UINT(int(x)+i), UINT(int(y)+j)))
bFoundPlace = FALSE;
if (bFoundPlace)
return TRUE; // Success
}
return FALSE; // Failure
}
void
CGameBoard::PickNextWallBlock (UINT &x, UINT &y, BYTE bDirectionOptions)
{
ASSERT (bDirectionOptions); // You gotta give me something to choose from.....
BYTE b = m_bPrevDir;
if (0 == Rand(8))
b = BYTE(1 << (Rand(4))); // b = 1, 2, 4, or 8
for (;;)
{
if ((b & bDirectionOptions) == DIRECTION_UP)
{
y--;
m_bPrevDir = b;
return;
}
if ((b & bDirectionOptions) == DIRECTION_DOWN)
{
y++;
m_bPrevDir = b;
return;
}
if ((b & bDirectionOptions) == DIRECTION_LEFT)
{
x--;
m_bPrevDir = b;
return;
}
if ((b & bDirectionOptions) == DIRECTION_RIGHT)
{
x++;
m_bPrevDir = b;
return;
}
b = BYTE(1 << (Rand(4))); // b = 1, 2, 4, or 8
}
}
BYTE
CGameBoard::GetNextWallBlockOptions (UINT x, UINT y)
{
BYTE bRes = 0;
int i,j;
BOOL bOK;
// Check the up option:
bOK = FALSE;
if (x >= TANK_BLOCK_WIDTH && x <= (MAP_X_CELLS - 1 - TANK_BLOCK_WIDTH) &&
(y-1) >= TANK_BLOCK_HEIGHT && (y-1) <= (MAP_Y_CELLS - 1 - TANK_BLOCK_HEIGHT))
{
bOK = TRUE;
for (i = -TANK_BLOCK_WIDTH; i <= TANK_BLOCK_WIDTH && bOK; i++)
for (j = -(TANK_BLOCK_HEIGHT + 1) ; j <= -1 && bOK; j++)
if (TERR_EMPTY != ReadMap(UINT(int(x)+i), UINT(int(y)+j)))
bOK = FALSE;
if (bOK)
bRes |= DIRECTION_UP;
}
// Check the down option:
bOK = FALSE;
if (x >= TANK_BLOCK_WIDTH && x <= (MAP_X_CELLS - 1 - TANK_BLOCK_WIDTH) &&
(y+1) >= TANK_BLOCK_HEIGHT && (y+1) <= (MAP_Y_CELLS - 1 - TANK_BLOCK_HEIGHT))
{
bOK = TRUE;
for (i = -TANK_BLOCK_WIDTH; i <= TANK_BLOCK_WIDTH && bOK; i++)
for (j = 1 ; j <= (TANK_BLOCK_HEIGHT + 1) && bOK; j++)
if (TERR_EMPTY != ReadMap(UINT(int(x)+i), UINT(int(y)+j)))
bOK = FALSE;
i