📄 level.cpp
字号:
void CLevel::DrawRadar(const TRect& aRect)
{
IBackBuffer& backbuffer = iApp.BackBuffer();
// NOTE!: assuming GRAPHICS_FORMAT_XRGB8888 backbuffer and graphics
if ( backbuffer.GetColorFormat() != GRAPHICS_FORMAT_XRGB8888)
{
// invalid format
return;
}
iApp.BlendRect(aRect);
// back buffer is already locked during the main loop
TUint32* bbpointer = (TUint32*)backbuffer.GetAddress();
TUint32 stride = backbuffer.GetStride() >> 2;
// move destination pointer to right location
bbpointer += aRect.iTl.iY * stride + aRect.iTl.iX;
const TReal64 radarwidth = (TReal64)aRect.Width();
const TReal64 radarheight = (TReal64)aRect.Height();
const TReal64 mapsizeinpixelsx = (TReal64)(KMapBlockWidth * iMapWidth);
const TReal64 mapsizeinpixelsy = (TReal64)(KMapBlockHeight * iMapHeight);
// get ghost positions and interpolate to radar screen
TInt i;
TPoint radarpos;
TInt tmp;
for (i=0; i<iGhosts.Count(); i++)
{
CSpriteBase* sprite = iGhosts[i];
radarpos.iX = (TInt) (sprite->Position().iX / mapsizeinpixelsx * radarwidth);
radarpos.iY = (TInt)(sprite->Position().iY / mapsizeinpixelsy * radarheight);
tmp = radarpos.iY * stride + radarpos.iX;
bbpointer[tmp] = 0x00FF0000;
bbpointer[tmp - 1] = 0x00FF0000;
bbpointer[tmp + 1] = 0x00FF0000;
bbpointer[tmp - stride] = 0x00FF0000;
bbpointer[tmp + stride] = 0x00FF0000;
}
// get level goal position and interpolate to radar screen
radarpos.iX = (TInt) ((iGoalBlock.iX * KMapBlockWidth) / mapsizeinpixelsx * radarwidth);
radarpos.iY = (TInt)((iGoalBlock.iY * KMapBlockHeight) / mapsizeinpixelsy * radarheight);
tmp = radarpos.iY * stride + radarpos.iX;
bbpointer[tmp] = 0x00FFFF44;
bbpointer[tmp - 1] = 0x00FFFF44;
bbpointer[tmp + 1] = 0x00FFFF44;
bbpointer[tmp - stride] = 0x00FFFF44;
bbpointer[tmp + stride] = 0x00FFFF44;
// get player position and interpolate to radar screen
radarpos.iX = (TInt) (iPlayer->Position().iX / mapsizeinpixelsx * radarwidth);
radarpos.iY = (TInt)(iPlayer->Position().iY / mapsizeinpixelsy * radarheight);
tmp = radarpos.iY * stride + radarpos.iX;
bbpointer[tmp] = 0x0033FF33;
bbpointer[tmp - 1] = 0x0033FF33;
bbpointer[tmp + 1] = 0x0033FF33;
bbpointer[tmp - stride] = 0x0033FF33;
bbpointer[tmp + stride] = 0x0033FF33;
}
TInt CLevel::CreateTreeLayers( const TInt aNumLayers,
IBitmap* aBitmap,
IBitmap* aMask,
const TReal64 aHeight,
RArray<TTreeLayer>* aLayerArray)
{
// create tree layers
TInt i;
TInt rotationangle = 48;
CSize layersize = aBitmap->GetSize();
TReal64 layerheight = aHeight / aNumLayers;
ReturnCode ret;
for (i=0; i<aNumLayers; i++)
{
CPoint middle(layersize.mX >> 1, layersize.mY >> 1);
// create temporary layer bitmap
IBitmap* treebitmap = NULL;
IGraphicsContext* context = NULL;
TInt error = iApp.CreateBitmapAndContext( TSize(layersize.mX, layersize.mY),
treebitmap,
context);
if (error != KErrNone)
{
return error;
}
// scale original tree image to layer size
ret = treebitmap->Lock();
ret = aBitmap->Lock();
context->Clear(KTransparentColor);
ret = context->Scale( CRect(0, 0, layersize.mX, layersize.mY),
*aBitmap);
ret = aBitmap->Unlock();
ret = treebitmap->Unlock();
// do the same with mask images
IBitmap* treemask = NULL;
IGraphicsContext* maskcontext = NULL;
if (aMask)
{
// create temporary layer bitmap
ret = CRuntime::CreateInstance( treemask );
if ( ret != OK )
{
return KErrNoMemory;
}
ret = treemask->Reconfigure( layersize, aMask->GetColorFormat() );
// create temporary layer context
ret = CRuntime::CreateInstance( maskcontext );
if ( ret != OK )
{
treemask->Release();
return KErrNoMemory;
}
// scale original tree mask image to layer size
maskcontext->SetGraphicsDevice( *treemask );
ret = treemask->Lock();
ret = aMask->Lock();
context->Clear(KTransparentColor);
ret = maskcontext->Scale( CRect(0, 0, layersize.mX, layersize.mY),
*aMask);
ret = aMask->Unlock();
ret = treemask->Unlock();
}
// create layer bitmaps
TTreeLayer layer;
layer.iBitmap = NULL;
layer.iMask = NULL;
layer.iHeight = layerheight;
ret = CRuntime::CreateInstance( layer.iBitmap );
if ( ret != OK )
{
return KErrNoMemory;
}
if (aMask)
{
ret = CRuntime::CreateInstance( layer.iMask );
}
if (layer.iBitmap)
{
ret = layer.iBitmap->Reconfigure( layersize, aBitmap->GetColorFormat() );
}
if (layer.iMask)
{
ret = layer.iMask->Reconfigure( layersize, aMask->GetColorFormat() );
}
context->SetGraphicsDevice( *layer.iBitmap );
// rotate original bitmap to new one
ret = layer.iBitmap->Lock();
context->Clear(KTransparentColor);
ret = treebitmap->Lock();
ret = context->Rotate( middle,
*treebitmap,
rotationangle,
middle);
ret = treebitmap->Unlock();
ret = layer.iBitmap->Unlock();
treebitmap->Release();
// rotate original mask to new one
if (treemask)
{
context->SetGraphicsDevice( *layer.iMask );
ret = layer.iMask->Lock();
context->Clear(KTransparentColor);
ret = treemask->Lock();
ret = context->Rotate( middle,
*treemask,
rotationangle,
middle);
ret = treemask->Unlock();
ret = layer.iMask->Unlock();
treemask->Release();
}
context->Release();
// add layer to array
aLayerArray->Append(layer);
// update next layer parameters
layersize.mX -= aBitmap->GetSize().mX / aNumLayers;
layersize.mY -= aBitmap->GetSize().mY / aNumLayers;
rotationangle += 31;
if (rotationangle >= 360)
{
rotationangle -= 360;
}
layerheight += aHeight / aNumLayers;
if (layersize.mX < 10 || layersize.mY < 10)
{
// layer is too small, no point to continue...
break;
}
}
return KErrNone;
}
void CLevel::ReleaseTreeLayers(RArray<TTreeLayer>& aLayerArray)
{
// delete tree layers
TInt i;
for (i=0; i<aLayerArray.Count(); i++)
{
TTreeLayer& layer = aLayerArray[i];
if (layer.iBitmap)
{
layer.iBitmap->Release();
}
if (layer.iMask)
{
layer.iMask->Release();
}
}
aLayerArray.Reset();
aLayerArray.Close();
}
void CLevel::AddTree( const TPoint& aPos,
RArray<TTreeLayer>* aLayerArray)
{
// create new tree
CTree* tree = new CTree(iApp);
if (tree == NULL)
{
return;
}
// add layers to it
TInt i;
for (i=0; i<aLayerArray->Count(); i++)
{
tree->AddLayer(&(*aLayerArray)[i]);
}
// set position
tree->SetPosition(TVector2(aPos));
// add to sprite array
iTrees.Append(tree);
}
void CLevel::AddBush(const TPoint& aPos)
{
// create new sprite for the grass
CSprite* grass = new (ELeave) CSprite(iApp);
// add grass animation frames
TSpriteFrame frame;
frame.iBitmap = iApp.Bitmap(KBitmapGrass1);
frame.iMask = iApp.Mask(KBitmapGrass1);
grass->AddFrame(frame);
frame.iBitmap = iApp.Bitmap(KBitmapGrass2);
frame.iMask = iApp.Mask(KBitmapGrass2);
grass->AddFrame(frame);
frame.iBitmap = iApp.Bitmap(KBitmapGrass3);
frame.iMask = iApp.Mask(KBitmapGrass3);
grass->AddFrame(frame);
frame.iBitmap = iApp.Bitmap(KBitmapGrass4);
frame.iMask = iApp.Mask(KBitmapGrass4);
grass->AddFrame(frame);
// set animation speed and frame sequence
grass->SetAnimationSpeed((TReal64)iApp.RandInt(3, 10));
const TUint32 sequence[6] = {0,1,2,3,2,1 };
grass->SetAnimationSequence(sequence, 6);
// set grass position
grass->SetPosition(TVector2(aPos));
// add to sprite array
iSprites.Append(grass);
}
void CLevel::AddGhost( const TPoint& aStartPos,
const TPoint& aEndPos,
const TInt aExtraParam,
const TInt aBitmapIndex)
{
CGhost* ghost = new CGhost(iApp, *this);
if (ghost == NULL)
{
return;
}
TInt error;
error = ghost->Init( iApp.Bitmap(aBitmapIndex),
iApp.Mask(aBitmapIndex));
if (error != KErrNone)
{
delete ghost;
return;
}
// set ghost AI parameters
ghost->SetAIParams( iPlayer,
aStartPos,
aEndPos,
aExtraParam,
aBitmapIndex);
iGhosts.Append(ghost);
}
void CLevel::AddBonus( const TPoint& aPos,
const TInt aBitmapIndex,
const TUint32 aId)
{
// create new sprite for the grass
CSprite* bonus = new (ELeave) CSprite(iApp);
// add bonus image frame
TSpriteFrame frame;
frame.iBitmap = iApp.Bitmap(aBitmapIndex);
frame.iMask = iApp.Mask(aBitmapIndex);
bonus->AddFrame(frame);
// set bonus position
bonus->SetPosition(TVector2(aPos));
// set bonus id
bonus->SetId(aId);
// add to sprite array
iBonuses.Append(bonus);
}
TSize CLevel::SizeInPixels() const
{
TSize size(KMapBlockWidth * iMapWidth, KMapBlockHeight * iMapHeight);
return size;
}
TSize CLevel::SizeInBlocks() const
{
TSize size(iMapWidth, iMapHeight);
return size;
}
TInt CLevel::IdFromCoordinate(const TPoint& aCoordinate)
{
// find block indices
TInt blockx = aCoordinate.iX / KMapBlockWidth;
TInt blocky = aCoordinate.iY / KMapBlockHeight;
if ( blockx < 0 || blockx >= iMapWidth ||
blocky < 0 || blocky >= iMapHeight)
{
// out of map
return -1;
}
// get the block id number and return
TMapBlock *block = &iBlocks[blocky * iMapWidth + blockx];
return block->iID;
}
TPoint CLevel::BlockIndicesFromCoordinate(const TPoint& aCoordinate)
{
// find block indices
TPoint indices;
indices.iX = aCoordinate.iX / KMapBlockWidth;
indices.iY = aCoordinate.iY / KMapBlockHeight;
if ( indices.iX < 0 || indices.iX >= iMapWidth ||
indices.iY < 0 || indices.iY >= iMapHeight)
{
// out of map
return TPoint(-1, -1);
}
return indices;
}
TPoint CLevel::SpriteBlock(CSpriteBase& aSprite)
{
TPoint pos( (TInt)(aSprite.Position().iX), (TInt)(aSprite.Position().iY) );
return BlockIndicesFromCoordinate(pos);
}
ELevelState CLevel::LevelState() const
{
return iLevelState;
}
TReal64 CLevel::RemainingMoney() const
{
return iRemainingMoney;
}
void CLevel::BlinkBacklight()
{
if (iBackLight)
{
// make backlight blink, if not blinking already
if (iBlinkCount == 0)
{
iBlinkCount = 8;
iBlinkInterval = KBacklightBlinkInterval;
}
}
}
void CLevel::KeyDown(TUint32 aKeyCode)
{
if (aKeyCode == INPUT_KEY_5)
{
iShowFPS = !iShowFPS;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -