⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mopoidgrid.cpp

📁 一款大砖块的游戏
💻 CPP
字号:
/*
* ============================================================================
*  Name     : TMopoidGrid and TBrick from MopoidGrid.cpp
*  Part of  : Mopoid
*  Created  : 16.01.2004 by Andreas Jakl / Mopius - http://www.mopius.com/
*  Description:
*     Handles the grid containing bricks.
*  Version  : 1.02
*  Copyright: 
*     'Mopoid' is free software; you can redistribute
*     it and/or modify it under the terms of the GNU General Public License
*     as published by the Free Software Foundation; either version 2 of
*     the License, or (at your option) any later version.
* 
*     'Mopoid' is distributed in the hope that it
*     will be useful, but WITHOUT ANY WARRANTY; without even the implied
*     warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*     See the GNU General Public License for more details.
* 
*     You should have received a copy of the GNU General Public License
*     along with 'Mopoid'; if not, write to the Free Software
*     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
* ============================================================================
*/

#include "MopoidGrid.h"

// --- TBrick
TBrick::TBrick(void)
:
	iBrickType(EBrickInactive)
{}

TBrick::TBrick(TBrickType aBrickType)
:
	iBrickType(aBrickType)
{}




// --- TGrid
TMopoidGrid::TMopoidGrid()
:
    iBrickSize(0,0),
    iUpperLeft(0,0)
{
}

// -----------------------------------------------------------------------
void TMopoidGrid::Init()
{
    // Fill array with blocks
    for (TInt x=0; x<GRID_COLS; x++)
    {
        for (TInt y=0; y<GRID_ROWS; y++)
        {
            iBricks[x][y].iBrickType = TBrick::EBrickInactive;
        }
    }
    if (iBrickSize.iWidth > 0)
    {
        iGridRect.iTl = iUpperLeft;
        iGridRect.iBr = TPoint(iUpperLeft.iX + iBrickSize.iWidth * GRID_COLS, 
                               iUpperLeft.iY + iBrickSize.iHeight * GRID_ROWS);
    }
}

// -----------------------------------------------------------------------
TInt TMopoidGrid::LoadLevel(TBrick aBricks[GRID_COLS][GRID_ROWS])
{
    TInt numBricks = 0;
    // Fill array with blocks
    for (TInt y=0; y<GRID_ROWS; y++)
    {
        for (TInt x=0; x<GRID_COLS; x++)
        {
            iBricks[x][y] = aBricks[x][y];
            if (aBricks[x][y].iBrickType != TBrick::EBrickInactive && 
                aBricks[x][y].iBrickType != TBrick::EBrickIndestructible )
                numBricks++;
        }
    }
    return numBricks;
}


// -----------------------------------------------------------------------
TBool TMopoidGrid::IsXYInGrid(const TPoint aXY) const
{
    return iGridRect.Contains(aXY);
}

// -----------------------------------------------------------------------
TPoint TMopoidGrid::ConvertGridToXY(const TPoint aXYGrid) const
{
    return ConvertGridToXY(aXYGrid.iX, aXYGrid.iY);
}

// -----------------------------------------------------------------------
TPoint TMopoidGrid::ConvertGridToXY(const TInt aXGrid, const TInt aYGrid) const
{
    TPoint realPos;
    realPos.iX = aXGrid * iBrickSize.iWidth + iUpperLeft.iX;
    realPos.iY = aYGrid * iBrickSize.iHeight + iUpperLeft.iY;
    return realPos;
}

// -----------------------------------------------------------------------
TPoint TMopoidGrid::ConvertXYToGrid(const TPoint aXY) const
{
    TPoint gridPos;
    gridPos.iX = (aXY.iX - iUpperLeft.iX) / iBrickSize.iWidth;
    gridPos.iY = (aXY.iY - iUpperLeft.iY) / iBrickSize.iHeight;
    return gridPos;
}

// -----------------------------------------------------------------------
TPoint TMopoidGrid::ConvertXYToGrid(const TInt aX, const TInt aY) const
{
    return ConvertXYToGrid(TPoint(aX, aY));
}

// -----------------------------------------------------------------------
TBool TMopoidGrid::IsBrickAtPos(const TPoint aXYGrid) const
{
    return IsBrickAtPos(aXYGrid.iX, aXYGrid.iY);
}

// -----------------------------------------------------------------------
TBool TMopoidGrid::IsBrickAtPos(const TInt aXGrid, const TInt aYGrid) const
{
    if (aXGrid < 0 || aXGrid > GRID_COLS ||
        aYGrid < 0 || aYGrid > GRID_ROWS)
        return EFalse;

    return iBricks[aXGrid][aYGrid].IsActive();
}

// -----------------------------------------------------------------------
TBool TMopoidGrid::DrawBrickAtPos(const TInt aXGrid, const TInt aYGrid) const
{
    return ( (iBricks[aXGrid][aYGrid].iBrickType != TBrick::EBrickInactive) && 
             (iBricks[aXGrid][aYGrid].iBrickType != TBrick::EBrickInvisible) );
}

// -----------------------------------------------------------------------
TBrick::TBrickType TMopoidGrid::GetBrickTypeAtPos(const TPoint aXYGrid) const
{
    return iBricks[aXYGrid.iX][aXYGrid.iY].iBrickType;
}

// -----------------------------------------------------------------------
TBrick::TBrickType TMopoidGrid::GetBrickTypeAtPos(const TInt aXGrid, const TInt aYGrid) const
{
    return iBricks[aXGrid][aYGrid].iBrickType;
}

// -----------------------------------------------------------------------
TBool TMopoidGrid::HitBrickAtPos(const TPoint aXYGrid)
{
    return HitBrickAtPos(aXYGrid.iX, aXYGrid.iY);
}

// -----------------------------------------------------------------------
TBool TMopoidGrid::HitBrickAtPos(const TInt aXGrid, const TInt aYGrid)
{
    TBool deleteBrick = EFalse;
    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::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;
        deleteBrick = ETrue;
        }
        break;
    }
    return deleteBrick;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -