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

📄 mopoidlevels.cpp

📁 mopoid game symbian os application development
💻 CPP
字号:
/*
========================================================================
 Name        : MopoidLevels.cpp
 Author      :
 Copyright   : 
 Description : Loads and manages level definitions.
 License     : 
 
========================================================================
*/
#include "MopoidLevels.h"

TMopoidLevels::TMopoidLevels() :
	KNewLine(0x000A) // Newline (UNIX!)
	{

	}

TInt TMopoidLevels::ReadLineNumberIntL(TLex8& aFileLex)
	{
	TPtrC8 curString8;
	curString8.Set(ExtractToken(aFileLex, KNewLine));
	TLex8 parseString8(curString8);
	TInt value = 0;
	User::LeaveIfError(parseString8.Val(value));
	return value;
	}

TReal TMopoidLevels::ReadLineNumberFloatL(TLex8& aFileLex)
	{
	TPtrC8 curStringPtr8;
	curStringPtr8.Set(ExtractToken(aFileLex, KNewLine));

	RBuf8 curString8;
	CleanupClosePushL(curString8);
	curString8.Create(curStringPtr8);

	TLocale locale;
	TChar decimalSeparator = locale.DecimalSeparator();
	if (decimalSeparator != '.')
		{
		// Change the decimal seperator from the file (.) to the
		// char used by the current locale.
		// Otherwise, TLex won't parse the number correctly.
		TInt charPos = curString8.Locate('.');
		if (charPos != KErrNotFound)
			{
			curString8[charPos] = decimalSeparator;
			}
		}

	TLex8 parseString8(curString8);
	TReal value = 0;
	User::LeaveIfError(parseString8.Val(value));
	CleanupStack::PopAndDestroy(&curString8);
	return value;
	}

void TMopoidLevels::LoadLevelL(RFs& aFs, const TInt aLevelNum,
		CMopoidGrid* aGrid, CMopoidSettings* aSettings)
	{
	RBuf fileName;
	fileName.CleanupClosePushL();
	NCommonFunctions::AddPrivateDirL(aFs, KLevelsDataFile(), fileName);

	HBufC8* sourceFile8 = ReadFileL(aFs, fileName);
	CleanupStack::PushL(sourceFile8);

	// parse file
	TLex8 lex(sourceFile8->Des());
	TInt length = 0;

	TInt curLevel = 0;
	TInt countLevels = 0;
	TPtrC8 curString8;
	TBool reachedEOF = EFalse;
	do
		{
		// Check if another level starts or if we reached
		// the end of the file
		do
			{
			curString8.Set(ExtractToken(lex, KNewLine));
			if (curString8.Length() == 0)
				{
				reachedEOF = ETrue;
				break;
				}
			if (curString8.Compare(KNewLevel8) == 0)
				{
				break;
				}
			}
		while (!reachedEOF);
		if (reachedEOF)
			break;

		// Next level starts
		countLevels ++;

		// Read level number
		curLevel = ReadLineNumberIntL(lex);

		if (curLevel == aLevelNum)
			{
			// Read columns
			aSettings->iGridCols = ReadLineNumberIntL(lex);

			// Read rows
			aSettings->iGridRows = ReadLineNumberIntL(lex);

			// iSettings->iScoreLevelCleared
			aSettings->iScoreLevelCleared = ReadLineNumberIntL(lex);

			// iSettings->iBlockNormalDestroyScore
			aSettings->iBlockNormalDestroyScore = ReadLineNumberIntL(lex);

			// iSettings->iScoreDecreaseTime
			aSettings->iScoreDecreaseTime = ReadLineNumberIntL(lex);

			// iSettings->iScoreDecrement
			aSettings->iScoreDecrement = ReadLineNumberIntL(lex);

			// iSettings: panel width
			aSettings->SetPanelSize(ReadLineNumberIntL(lex));

			// iSettings->iPanelSpeed
			aSettings->iPanelSpeed = ReadLineNumberFloatL(lex);

			// iSettings->iPanelReflection
			aSettings->iPanelReflection = ReadLineNumberFloatL(lex);

			// iSettings->ball width
			aSettings->SetBallSize(ReadLineNumberIntL(lex));

			// iSettings->iBallStartSpeed
			aSettings->iBallStartSpeed = ReadLineNumberFloatL(lex);

			// iSettings->iBallSpeedMaxX
			aSettings->iBallSpeedMaxX = ReadLineNumberFloatL(lex);

			// iSettings->iBallSpeedAccX
			aSettings->iBallSpeedAccX = ReadLineNumberFloatL(lex);

			// iSettings->iBallSpeedMaxY
			aSettings->iBallSpeedMaxY = ReadLineNumberFloatL(lex);

			// iSettings->iBallSpeedAccY
			aSettings->iBallSpeedAccY = ReadLineNumberFloatL(lex);

			aGrid->InitL();

			// Create array to store the level
			TInt curRow, curCol;
			TBrick::TBrickType curBrick;
			for (curRow = 0; curRow < aSettings->iGridRows; curRow++)
				{
				curString8.Set(ExtractToken(lex, KNewLine));
				length = curString8.Length();
				if (length == 0 || length != aSettings->iGridCols)
					{
					User::Leave(KErrCorrupt);
					}

				for (curCol=0; curCol < aSettings->iGridCols; curCol++)
					{
					curBrick = TBrick::EBrickInactive;
					switch (curString8[curCol])
						{
						case '1':
							curBrick = TBrick::EBrickNormal;
							break;
						case '2':
							curBrick = TBrick::EBrickDouble;
							break;
						case '3':
							curBrick = TBrick::EBrickMoving;
							break;
						case '6':
							curBrick = TBrick::EBrickHarderBrick;
							break;
						case '7':
							curBrick = TBrick::EBrickSofterBrick;
							break;
						case '8':
							curBrick = TBrick::EBrickInvisible;
							break;
						case '9':
							curBrick = TBrick::EBrickIndestructible;
							break;
						}
					aGrid->SetBrickAtPos(TPoint(curCol, curRow), curBrick);
					}
				}
			}
		}
	while (!reachedEOF);

	aSettings->iAvailableLevels = countLevels;

	CleanupStack::PopAndDestroy(sourceFile8);
	CleanupStack::PopAndDestroy(&fileName);
	}

HBufC8* TMopoidLevels::ReadFileL(RFs& aFs, const TDesC& inPath)
	{
	// We have to read the file content and put it in <iMadData>
	// Let's open the file
	RFile theFile;
	User::LeaveIfError(theFile.Open(aFs, inPath, EFileShareReadersOnly
			| EFileRead));
	CleanupClosePushL(theFile);

	// Get the file length
	TInt fileLength;
	User::LeaveIfError(theFile.Size(fileLength));

	// Allocates the memory to read the whole file (and pushes it on CleanupStack)
	HBufC8* theData = HBufC8::NewLC(fileLength);

	// Creates a dummy descriptor for the Read method
	TPtr8 theDescriptor(theData->Des());

	// Reads the whole file in our buffer
	User::LeaveIfError(theFile.Read(theDescriptor));

	// We clean our CleanupStack
	CleanupStack::Pop(theData);

	// OK, we can Close and destroy the RFile
	CleanupStack::PopAndDestroy(&theFile);

	// Returns the allocated data
	return theData;
	}

TPtrC8 TMopoidLevels::ExtractToken(TLex8& aLex, const TChar aSeparator)
	{
	aLex.Mark();
	const TInt index = aLex.Remainder().Locate(aSeparator);
	if (index == KErrNotFound)
		{
		aLex.Inc(aLex.Remainder().Length());
		}
	else
		{
		aLex.Inc(index);
		}

	TPtrC8 result = aLex.MarkedToken();
	aLex.Inc();
	return result;
	}

⌨️ 快捷键说明

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