📄 mopoidgrid.cpp
字号:
/*
========================================================================
Name : MopoidGrid.cpp
Author :
Copyright :
Description : Handles the grid that contains the bricks.
License :
========================================================================
*/
#include "MopoidGrid.h"
// --- TBrick
TBrick::TBrick(void) :
iBrickType(EBrickInactive)
{
}
TBrick::TBrick(TBrickType aBrickType) :
iBrickType(aBrickType)
{
}
// --- TGrid
CMopoidGrid::CMopoidGrid() :
iSettings(NULL)
{
}
CMopoidGrid::~CMopoidGrid()
{
DeleteGrid();
}
// -----------------------------------------------------------------------
void CMopoidGrid::SetSettingsPointer(CMopoidSettings* aSettings)
{
iSettings = aSettings;
}
// -----------------------------------------------------------------------
void CMopoidGrid::InitL()
{
DeleteGrid();
// Create the new grid using the current size
iCurGridCols = iSettings->iGridCols;
iCurGridRows = iSettings->iGridRows;
CreateGridL();
// Calculate the sprite size of the bricks according to the screen resolution
// and the number of columns
iSettings->SetGridSize(iCurGridCols, iCurGridRows);
// Calculate size of the grid
if (iSettings->iBrickSize.iWidth > 0)
{
iGridRect.iTl = iSettings->iGridTopLeft;
iGridRect.iBr = TPoint(iSettings->iGridTopLeft.iX
+ iSettings->iBrickSize.iWidth * iCurGridCols,
iSettings->iGridTopLeft.iY + iSettings->iBrickSize.iHeight
* iCurGridRows);
}
}
// -----------------------------------------------------------------------
TBool CMopoidGrid::IsXYInGrid(const TPoint aXY) const
{
return iGridRect.Contains(aXY);
}
// -----------------------------------------------------------------------
TPoint CMopoidGrid::ConvertGridToXY(const TPoint aXYGrid) const
{
return ConvertGridToXY(aXYGrid.iX, aXYGrid.iY);
}
// -----------------------------------------------------------------------
TPoint CMopoidGrid::ConvertGridToXY(const TInt aXGrid, const TInt aYGrid) const
{
TPoint realPos;
realPos.iX = aXGrid * iSettings->iBrickSize.iWidth
+ iSettings->iGridTopLeft.iX;
realPos.iY = aYGrid * iSettings->iBrickSize.iHeight
+ iSettings->iGridTopLeft.iY;
return realPos;
}
// -----------------------------------------------------------------------
TPoint CMopoidGrid::ConvertXYToGrid(const TPoint aXY) const
{
TPoint gridPos;
gridPos.iX = (aXY.iX - iSettings->iGridTopLeft.iX)
/ iSettings->iBrickSize.iWidth;
gridPos.iY = (aXY.iY - iSettings->iGridTopLeft.iY)
/ iSettings->iBrickSize.iHeight;
return gridPos;
}
// -----------------------------------------------------------------------
TPoint CMopoidGrid::ConvertXYToGrid(const TInt aX, const TInt aY) const
{
return ConvertXYToGrid(TPoint(aX, aY));
}
// -----------------------------------------------------------------------
TBool CMopoidGrid::IsBrickAtPos(const TPoint aXYGrid) const
{
return IsBrickAtPos(aXYGrid.iX, aXYGrid.iY);
}
// -----------------------------------------------------------------------
TBool CMopoidGrid::IsBrickAtPos(const TInt aXGrid, const TInt aYGrid) const
{
if (aXGrid < 0 || aXGrid >= iCurGridCols || aYGrid < 0 || aYGrid
>= iCurGridRows)
return EFalse;
return iBricks[aXGrid][aYGrid].IsActive();
}
// -----------------------------------------------------------------------
TBool CMopoidGrid::DrawBrickAtPos(const TInt aXGrid, const TInt aYGrid) const
{
if (aXGrid < 0 || aXGrid >= iCurGridCols || aYGrid < 0 || aYGrid
>= iCurGridRows)
return EFalse;
return ( (iBricks[aXGrid][aYGrid].iBrickType != TBrick::EBrickInactive)
&& (iBricks[aXGrid][aYGrid].iBrickType != TBrick::EBrickInvisible) );
}
// -----------------------------------------------------------------------
TBrick::TBrickType CMopoidGrid::GetBrickTypeAtPos(const TPoint aXYGrid) const
{
if (aXYGrid.iX < 0 || aXYGrid.iX >= iCurGridCols || aXYGrid.iY < 0
|| aXYGrid.iY >= iCurGridRows)
return TBrick::EBrickInactive;
return iBricks[aXYGrid.iX][aXYGrid.iY].iBrickType;
}
// -----------------------------------------------------------------------
TBrick::TBrickType CMopoidGrid::GetBrickTypeAtPos(const TInt aXGrid,
const TInt aYGrid) const
{
if (aXGrid < 0 || aXGrid >= iCurGridCols || aYGrid < 0 || aYGrid
>= iCurGridRows)
return TBrick::EBrickInactive;
return iBricks[aXGrid][aYGrid].iBrickType;
}
// -----------------------------------------------------------------------
TBool CMopoidGrid::HitBrickAtPosL(const TPoint aXYGrid)
{
return HitBrickAtPosL(aXYGrid.iX, aXYGrid.iY);
}
// -----------------------------------------------------------------------
TBool CMopoidGrid::HitBrickAtPosL(const TInt aXGrid, const TInt aYGrid)
{
TBool deleteBrick = EFalse;
if (aXGrid >= 0 && aXGrid < iCurGridCols && aYGrid >= 0 && aYGrid
< iCurGridRows)
{
switch (iBricks[aXGrid][aYGrid].iBrickType)
{
case TBrick::EBrickInactive:
case TBrick::EBrickIndestructible:
// Do nothing
break;
case TBrick::EBrickDouble:
{
// Hit once - convert to normal brick.
iBricks[aXGrid][aYGrid].iBrickType = TBrick::EBrickNormal;
}
break;
case TBrick::EBrickMoving:
{
// Delete the brick that was hit
iBricks[aXGrid][aYGrid].iBrickType = TBrick::EBrickInactive;
iNumBricks --;
deleteBrick = ETrue;
// Create a new brick one position above the previous one
// (if the field is still free).
TPoint newBrick(aXGrid, aYGrid - 1);
if (newBrick.iY >= 0)
{
// Check if a brick is already present at this location
// before moving this one up
if (!IsBrickAtPos(newBrick))
{
SetBrickAtPos(newBrick, TBrick::EBrickMoving);
}
}
}
break;
case TBrick::EBrickHarderBrick:
{
// Find a random normal brick and increase its strength
// (upgrades a normal brick to a double brick)
TPoint targetBrick = FindBrickExceptPosL(aXGrid, aYGrid,
TBrick::EBrickNormal);
if (targetBrick.iX != -1 && targetBrick.iY != -1)
{
SetBrickAtPos(targetBrick, TBrick::EBrickDouble);
}
}
break;
case TBrick::EBrickSofterBrick:
{
// Find a random indestructible brick and transform
// it into a normal brick
TPoint targetBrick = FindBrickExceptPosL(aXGrid, aYGrid,
TBrick::EBrickIndestructible);
if (targetBrick.iX != -1 && targetBrick.iY != -1)
{
SetBrickAtPos(targetBrick, TBrick::EBrickNormal);
}
}
break;
case TBrick::EBrickInvisible:
{
// Convert to normal brick -> make visible!
iBricks[aXGrid][aYGrid].iBrickType = TBrick::EBrickNormal;
}
break;
case TBrick::EBrickNormal:
{
// Remove brick
iBricks[aXGrid][aYGrid].iBrickType = TBrick::EBrickInactive;
iNumBricks --;
deleteBrick = ETrue;
}
break;
}
}
return deleteBrick;
}
// -----------------------------------------------------------------------
TPoint CMopoidGrid::FindBrickExceptPosL(const TInt aXGrid, const TInt aYGrid,
const TBrick::TBrickType aBrickType)
{
// Count how many bricks of the specified type are still remaining
// and save them in a dynamic array
RArray<TPoint> foundBricks(20);
CleanupClosePushL(foundBricks);
TInt x, y;
for (x = 0; x < iCurGridCols; x++)
{
for (y = 0; y < iCurGridRows; y++)
{
if (GetBrickTypeAtPos(x, y) == aBrickType && !(aXGrid == x && aYGrid
== y))
{
foundBricks.AppendL(TPoint(x, y));
}
}
}
TPoint chooseBrick;
if (foundBricks.Count() > 0)
{
TInt chooseBrickNum = Math::Rand(iSettings->iRandSeed)
% foundBricks.Count();
chooseBrick = foundBricks[chooseBrickNum];
}
else
{
chooseBrick.SetXY(-1, -1);
}
CleanupStack::PopAndDestroy(&foundBricks);
return chooseBrick;
}
// -----------------------------------------------------------------------
TInt CMopoidGrid::GetNumBricks()
{
return iNumBricks;
}
// -----------------------------------------------------------------------
void CMopoidGrid::CreateGridL()
{
// Create new 2D dynamic arrays.
iBricks = new (ELeave) TBrick* [iCurGridCols];
for (TInt i=0; i<iCurGridCols; i++)
{
*(iBricks+i) = new (ELeave) TBrick[iCurGridRows];
}
iNumBricks = 0;
}
// -----------------------------------------------------------------------
void CMopoidGrid::SetBrickAtPos(const TPoint aXYGrid,
TBrick::TBrickType aBrickType)
{
if (aXYGrid.iX >= 0 || aXYGrid.iX < iCurGridCols || aXYGrid.iY >= 0
|| aXYGrid.iY < iCurGridRows)
{
if ((!iBricks[aXYGrid.iX][aXYGrid.iY].IsActive()
|| iBricks[aXYGrid.iX][aXYGrid.iY].iBrickType
== TBrick::EBrickIndestructible) && aBrickType
!= TBrick::EBrickInactive && aBrickType
!= TBrick::EBrickIndestructible && aBrickType != TBrick::EBrickHarderBrick && aBrickType != TBrick::EBrickSofterBrick)
{
iNumBricks ++;
}
iBricks[aXYGrid.iX][aXYGrid.iY].iBrickType = aBrickType;
}
}
// -----------------------------------------------------------------------
void CMopoidGrid::DeleteGrid()
{
// If the grid exists, delete it.
if (iBricks)
{
for (TInt i=0; i<iCurGridCols; i++)
{
delete[] *(iBricks+i);
}
delete[] iBricks;
iBricks = NULL;
}
iNumBricks = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -