📄 gameengine.cpp
字号:
/*
============================================================================
Name : GameEngine.cpp
Author :
Version :
Copyright : Your copyright notice
Description : CGameEngine implementation
============================================================================
*/
#include "GameEngine.h"
#include "S0707_MazeAppView.h"
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <S0707_Maze.rsg>
namespace mygame
{
_LIT(KImageScene,"d:\\wwk\\bg.png");
_LIT(KImageSprite,"d:\\wwk\\car.png");
static TInt CELL_WIDTH = 24;//图像单元的宽度
static TInt CELL_HEIGHT = 24;//图像单元的高度
static TInt GRASS = 1; //背景草地
static TInt WALL = 2; //墙
static TInt BORDER = 72; //地图的边大小,到达边后就滚动地图
static const TInt frameNum=6;
static TInt AniSequence[frameNum]={0,1,0,1,0,2};//定义精灵的动画
static TInt delays[frameNum]={1,1,1,1,1,1};//精灵动画每帧间的时间间隔
static const TInt rows=21;
static const TInt cols=21;
//游戏地图 内容帧索引,from 0
static TInt cells[rows][cols] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1},
{1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1},
{1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
{1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}
};
CGameEngine::CGameEngine(const TRect& aRect,CCoeControl* aControl)
{
iBLoadImageOK=EFalse;
iVwX=0;
iVwY=0;
iVwWidth=aRect.Width();
iVwHeight=aRect.Height();
iDirection = ERight;
iCarX=1;
iCarY=1;
iControl=aControl;
}
CGameEngine::~CGameEngine()
{
if (iTimer)
{
iTimer->Cancel();
delete iTimer;
}
delete iImageFactory;
delete iBgLayer;
delete iHero;
delete iCollidLayer;
}
CGameEngine* CGameEngine::NewLC(const TRect& aRect,CCoeControl* aControl)
{
CGameEngine* self = new (ELeave)CGameEngine(aRect,aControl);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CGameEngine* CGameEngine::NewL(const TRect& aRect,CCoeControl* aControl)
{
CGameEngine* self=CGameEngine::NewLC(aRect,aControl);
CleanupStack::Pop(); // self;
return self;
}
void CGameEngine::ConstructL()
{
iImageFactory=CImageFactory::NewL(this);
iImageFactory->AddImageL(KImageScene,EImageScene);
iImageFactory->AddImageL(KImageSprite,EImageHero);
iImageFactory->StartConvertL();
}
void CGameEngine::ConvertCompleted(TInt aCurrent,TInt aCount)
{
iBLoadImageOK=ETrue;
HBufC* textResource = StringLoader::LoadLC( R_STR_CONVERT_SUCCESS );
CAknInformationNote* informationNote =
new ( ELeave ) CAknInformationNote;
informationNote->ExecuteLD( *textResource );
CleanupStack::PopAndDestroy( textResource );
//
CFbsBitmap* bmp;
CFbsBitmap* bmpMask;
//构造背景层
iImageFactory->Image(EImageScene,bmp,bmpMask);
iBgLayer=CTiledLayer::NewL(bmp,bmpMask,CELL_WIDTH,CELL_HEIGHT);
iBgLayer->SetRange(rows,cols,0);
//构造墙层,与背景层用同一张源图片
iCollidLayer=CTiledLayer::NewL(bmp,bmpMask,CELL_WIDTH,CELL_HEIGHT);
iCollidLayer->SetRange(rows,cols,0);
//构造精灵层
/*iHero = CHero::NewL();
iImageFactory->Image(EImageHero,bmp,bmpMask);
iHero->SetSource(bmp,bmpMask,CELL_WIDTH,CELL_HEIGHT);
iHero->SetPosition(iCarX,iCarY);
iHero->SetSequence(AniSequence,frameNum,delays);*/
//填充
for(int row=0;row<rows;row++)
{
for(int col=0;col<cols;col++)
{
if (cells[row][col] == 0)
{
iBgLayer->SetCell(col,row,GRASS);
}
else
{
iCollidLayer->SetCell(col,row,WALL);
}
}
}
iTimer=CPeriodic::NewL(CActive::EPriorityStandard);
iTimer->Start(0,1000000,TCallBack(CallBack,iControl));//立即执行,然后间隔一秒执行
}
void CGameEngine::ConvertFail(TInt aCurrent,TInt aCount,const TDesC& aDes)
{
HBufC* textResource = StringLoader::LoadLC( R_STR_CONVERT_FAIL );
CAknInformationNote* informationNote =
new ( ELeave ) CAknInformationNote;
informationNote->ExecuteLD( *textResource );
CleanupStack::PopAndDestroy( textResource );
}
void CGameEngine::Draw(CWindowGc& aGc) const
{
if (!iBLoadImageOK)
return;
iBgLayer->Draw(aGc);
iCollidLayer->Draw(aGc);
}
TInt CGameEngine::CallBack( TAny* aAny )
{
CS0707_MazeAppView* p=
static_cast<CS0707_MazeAppView*>(aAny);
p->DrawNow();
return KErrNone;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -