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

📄 view.cpp

📁 symbain7.0环境应用程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	else // no ship
		{
		gc.SetPenStyle(CGraphicsContext::ENullPen);
		gc.DrawRect(rect);
		}
	}

// sound

void CFleetView::ExplSound()
	{
	iData.iSoundEffects.PlaySound(CSoundEffects::EExplosion);
	}

void CFleetView::MissSound()
	{
	iData.iSoundEffects.PlaySound(CSoundEffects::EMiss);
	}

void CFleetView::SunkSound()
	{
	iData.iSoundEffects.PlaySound(CSoundEffects::ESunk);
	}
	
// cursor

void CFleetView::SetCursorOff()
	{
	iCursorOn=EFalse;
	}

void CFleetView::SetCursor(TInt aX, TInt aY)
	{
	iCursorOn=ETrue;
	iCursorX=aX;
	iCursorY=aY;
	}

TBool CFleetView::CursorOn() const
	{
	return iCursorOn;
	}

void CFleetView::GetCursor(TInt& aX, TInt& aY) const
	{
	aX=iCursorX;
	aY=iCursorY;
	}

void CFleetView::MoveCursor(TInt aDx, TInt aDy)
	{
	TInt x=iCursorX+aDx;
	TInt y=iCursorY+aDy;
	if (x<0 || x>=8 || y<0 || y>=8)
		return;
	iCursorX=x;
	iCursorY=y;
	}

// view information, from MCoeView

TVwsViewId CFleetView::ViewId() const
	{
	return TVwsViewId(KUidBattleShips, iViewUid);
	}

//#define _DEBUGVIEWS_	
#ifdef _DEBUGVIEWS_
void CFleetView::ViewActivatedL(const TVwsViewId& aPrevViewId, TUid aCustomMessageId,
	const TDesC8& aCustomMessage)
	{
    Window().SetOrdinalPosition(0);
    (static_cast<CGameAppUi*>(iEikonEnv->EikAppUi()))->SetActiveView(*this);
	// display the view switch information
	TBuf<200> buf,buf2;
	buf.Format(_L("View ID: %x\nPrevious view ID: %x\nMessage ID: %x\nMessage: "),
		iViewUid.iUid, aPrevViewId.iViewUid.iUid, aCustomMessageId.iUid);
	buf2.Copy(aCustomMessage);
	buf.Append(buf2);
	iEikonEnv->InfoWinL(_L("View info"), buf);
    }
#else

void CFleetView::ViewActivatedL(const TVwsViewId& /*aPrevViewId*/, TUid /*aCustomMessageId*/,const TDesC8& /*aCustomMessage*/)
	{
    Window().SetOrdinalPosition(0);
    (static_cast<CGameAppUi*>(iEikonEnv->EikAppUi()))->SetActiveView(*this);
    }
#endif
	
void CFleetView::ViewDeactivated()
	{	
	}

//
// COppFleetView: specialises CFleetView for viewing opposition fleet
//

COppFleetView* COppFleetView::NewL(CFleetViewData& aFleetViewData, 
	TFleet& aFleet)//, TBool aP1)
	{
	COppFleetView* self = new (ELeave) COppFleetView(aFleetViewData, aFleet);//, aP1);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop(self);
	return self;
	}

COppFleetView::COppFleetView(CFleetViewData& aFleetViewData, 
	TFleet& aFleet)//, 
	//TBool aP1)
	:CFleetView(aFleetViewData, aFleet)
	{
//	if (aP1)
		iViewUid = KP1OppViewUID;
//	else
//		iViewUid = KP2OppViewUID;
	}
	
TKeyResponse COppFleetView::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
	{
	if (aType!=EEventKey)
		return EKeyWasNotConsumed;
	if (aKeyEvent.iCode==EQuartzKeyFourWayLeft ||
		aKeyEvent.iCode==EQuartzKeyFourWayRight ||
		aKeyEvent.iCode==EQuartzKeyFourWayUp ||
		aKeyEvent.iCode==EQuartzKeyFourWayDown)
		{
		// move cursor
		if (aKeyEvent.iCode==EQuartzKeyFourWayLeft)
			MoveCursor(-1,0);
		else if (aKeyEvent.iCode==EQuartzKeyFourWayRight)
			MoveCursor(1,0);
		else if (aKeyEvent.iCode==EQuartzKeyFourWayUp)
			MoveCursor(0,-1);
		else if (aKeyEvent.iCode==EQuartzKeyFourWayDown)
			MoveCursor(0,1);
		// redraw board
		DrawTilesNow();
		return EKeyWasConsumed;
		}
	else if (aKeyEvent.iCode==EQuartzKeyConfirm)
		{
		if (iFleet.IsKnown(iCursorX, iCursorY))
			iEikonEnv->InfoMsg(R_GAME_ALREADY_KNOWN);
		else
			iData.iCmdHandler.ViewCmdHitFleet(iCursorX, iCursorY);
		return EKeyWasConsumed;
		}
	return EKeyWasNotConsumed;
	}

void COppFleetView::HandlePointerEventL(const TPointerEvent& aPointerEvent)
	{
	// check whether we're interested
	if (
		aPointerEvent.iType==TPointerEvent::EButton1Down &&
		iData.iSeaArea.Contains(aPointerEvent.iPosition)
		)
		{
		// identify the tile that was hit
		TInt x=(aPointerEvent.iPosition.iX-iData.iSeaArea.iTl.iX)/iData.iTileSize;
		TInt y=(aPointerEvent.iPosition.iY-iData.iSeaArea.iTl.iY)/iData.iTileSize;
		// move cursor if necessary
		TBool iCursorMoved=(x!=iCursorX || y!=iCursorY);
		SetCursor(x,y);
		// hit square unless it's already known
		if (iFleet.IsKnown(x,y))
			{
			iEikonEnv->InfoMsg(R_GAME_ALREADY_KNOWN);
			if (iCursorMoved)
				DrawTilesNow();
			}
		else
			iData.iCmdHandler.ViewCmdHitFleet(x,y);
		}
	}

//
// CMyFleetView: specialises CFleetView for viewing own fleet
//

CMyFleetView* CMyFleetView::NewL(CFleetViewData& aFleetViewData, 
	TFleet& aFleet)//, TBool aP1)
	{
	CMyFleetView* self = new (ELeave) CMyFleetView(aFleetViewData, aFleet);//, aP1);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop(self);
	return self;
	}

CMyFleetView::CMyFleetView(CFleetViewData& aFleetViewData, 
	TFleet& aFleet)//, 
	//TBool aP1)
	:CFleetView(aFleetViewData, aFleet)
	{
//	if (aP1)
		iViewUid = KP1MyViewUID;
//	else
//		iViewUid = KP2MyViewUID;
	}
	
	
//
// CSoundEffects
// 

_LIT(KExplSoundFile,"expl.wav");
_LIT(KMissSoundFile,"miss.wav");
_LIT(KSunkSoundFile,"sunk.wav");

CSoundEffects::CSoundEffects()
	{
	}

void CSoundEffects::ConstructL()
	{
	// Get path to sound files
	TParsePtrC parse(CEikonEnv::Static()->EikAppUi()->Application()->AppFullName());
	TPtrC appPath = parse.DriveAndPath();
	ConstructSoundPlayerL(appPath, KExplSoundFile, iExplSoundPlayer);
	ConstructSoundPlayerL(appPath, KMissSoundFile, iMissSoundPlayer);
	ConstructSoundPlayerL(appPath, KSunkSoundFile, iSunkSoundPlayer);
	}

void CSoundEffects::ConstructSoundPlayerL(const TDesC& aPath, const TDesC& aName, 
	CMdaAudioPlayerUtility*& aPlayer )
	{
	TFileName name = aPath;
	name.Append(aName);
	aPlayer=CMdaAudioPlayerUtility::NewFilePlayerL(name, iSoundPlayerCallBack);
	}

CSoundEffects::~CSoundEffects()
	{
	CancelSounds();
	delete iExplSoundPlayer;
	delete iMissSoundPlayer;
	delete iSunkSoundPlayer;
	}

void CSoundEffects::CancelSounds()
	{
	if (iSoundPlayerCallBack.iPlaying)
		iCurrentSoundPlayer->Stop();
	iCurrentSoundPlayer = NULL;
	}
	
CSoundEffects::TAudioPlayerCallBackHandler::TAudioPlayerCallBackHandler()
:iOK(EFalse),
iPlaying(EFalse)
	{
	}

void CSoundEffects::TAudioPlayerCallBackHandler::MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& )
	{
	if (aError == KErrNone) 
		iOK = ETrue;
	}

void CSoundEffects::TAudioPlayerCallBackHandler::MapcPlayComplete(TInt /*aError*/)
	{
	iPlaying = EFalse;
	}

// play things
void CSoundEffects::PlaySound(TSound aSound)
	{
	if (!iSoundPlayerCallBack.iOK) return;
	CancelSounds();
	switch (aSound)
		{
		case EExplosion:
			iCurrentSoundPlayer = iExplSoundPlayer;
			break;
		case EMiss:
			iCurrentSoundPlayer = iMissSoundPlayer;
			break;
		case ESunk:
			iCurrentSoundPlayer = iSunkSoundPlayer;
			break;
		};			
	iCurrentSoundPlayer->Play();
	iSoundPlayerCallBack.iPlaying = ETrue;
	}

⌨️ 快捷键说明

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