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

📄 playfield.cpp

📁 Source code (C++) of the Icebloxx game for Symbian OS UIQ3.x
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    m_lives--;
    m_state = Dying;
    m_actor[0].m_counter = 0;
    m_actor[0].m_req_dir = Stop;
    m_actor[0].m_act_dir = Stop;
    m_click_x = -1;
    m_click_y = -1;
#ifdef CAN_TELEPORT
    m_teleport = false;
#endif
}

void PlayField::moveOrCrushBlock(int actor, enum Directions dir)
{
    bool moveable = true;
    int j;

    if (actor < 0 || actor >= m_actors
	|| (m_actor[actor].m_type != Block && m_actor[actor].m_type != BlockCoin)) {
    }

    /* determine if the block is moveable */
    switch (dir) {
    case Up:
	if (m_actor[actor].m_y < 30) {
	    moveable = false;
	    break;
	}
	j = detectCollisionBlockOrWall(actor, m_actor[actor].m_x, m_actor[actor].m_y - 1);
	if (j >= 0) {
	    moveable = false;
	}
	break;
    case Down:
	if (m_actor[actor].m_y >= ((max_rows - 1) * 30)) {
	    moveable = false;
	    break;
	}
	j = detectCollisionBlockOrWall(actor, m_actor[actor].m_x, m_actor[actor].m_y + 1);
	if (j >= 0) {
	    moveable = false;
	}
	break;
    case Left:
	if (m_actor[actor].m_x < 30) {
	    moveable = false;
	    break;
	}
	j = detectCollisionBlockOrWall(actor, m_actor[actor].m_x - 1, m_actor[actor].m_y);
	if (j >= 0) {
	    moveable = false;
	}
	break;
    case Right:
	if (m_actor[actor].m_x >= ((max_cols - 1) * 30)) {
	    moveable = false;
	    break;
	}
	j = detectCollisionBlockOrWall(actor, m_actor[actor].m_x + 1, m_actor[actor].m_y);
	if (j >= 0) {
	    moveable = false;
	}
	break;
    default:
	break;
    }

    /* now, move or crush the block */
    if (moveable) {
	m_actor[actor].m_req_dir = dir;
	m_actor[actor].m_act_dir = dir;
	m_soundHandler->PlaySampleL(m_SoundEffects[EMoveBlockFx]);

    } else {
	m_actor[actor].m_counter = m_actor[actor].m_counter
	    + m_strength / 6;
	if (m_actor[actor].m_counter >= 8) {
	    if (m_actor[actor].m_type == BlockCoin) {
		m_coins_collected++;
		m_soundHandler->PlaySampleL(m_SoundEffects[EPickUpCoinFx]);
		m_score = m_score + 100;
		m_coins_in_level--;
		if (m_coins_in_level == 0) {
			m_soundHandler->PlaySampleL(m_SoundEffects[ELevelCompleteFx]);

		    m_levels_completed++;
		    m_score = m_score + 1000;
		    m_state = HappyPenguin;
		    m_actor[0].m_counter = 0;
		    if (m_levels_completed % 10 == 0 && m_lives < max_lives) {
			m_lives++;
		    }
		}
	    } else {
		m_soundHandler->PlaySampleL(m_SoundEffects[ECrushIceFx]);
		m_blocks_broken++;
		m_score = m_score + 5;
	    }
	    m_actor[actor].m_type = Empty;
	}
    }
}

#ifdef CAN_TELEPORT
void PlayField::teleport(void)
{
    int i;

    if (m_score >= 1000) {
	/* try to teleport to the start position, or any other that is far
	 * away from flames.
	 */
	int x = 0;
	int y = 0;
	int nloops = 0;
	while (++nloops < 100) {
	    if (detectCollision(0, x, y) < 0) {
		for (i = 1; i < m_actors; i++) {
		    if (m_actor[i].m_type == Flame) {
			int xdiff = abs(m_actor[i].m_x - x);
			int ydiff = abs(m_actor[i].m_y - y);
			if (xdiff < 90 || ydiff < 90) {
			    break;
			}
		    }
		}
		if (i >= m_actors) {
		    m_score = m_score - 1000;
		    m_actor[0].m_x = x;
		    m_actor[0].m_y = y;
		    m_actor[0].m_counter = 0;
		    m_actor[0].m_req_dir = Stop;
		    m_actor[0].m_act_dir = Stop;
		    return;
		}
	    }
	    x = (rand() % max_cols) * 30;
	    y = (rand() % max_cols) * 30;
	}
    }
}
#endif

int PlayField::detectCollision(int actor, int x, int y)
{
    int i;

    /* if we would hit an outside wall, just return the square of any wall on the map */
    if (x < 0 || y < 0 || x > ((max_cols - 1) * 30) || y > ((max_rows - 1) * 30)) {
	for (i = 0; i < m_actors; i++) {
	    if (m_actor[i].m_type == Wall) {
		return i;
	    }
	}
    }

    /* check for the known objects */
    for (i = 0; i < m_actors; i++) {
	if (i != actor && m_actor[i].m_type != Empty && m_actor[i].m_type != Points) {
	    int xdiff = abs(m_actor[i].m_x - x);
	    int ydiff = abs(m_actor[i].m_y - y);
	    if (xdiff < 30 && ydiff < 30) {
		return i;
	    }
	}
    }

    return -1;
}

int PlayField::detectCollisionMovingBlock(int actor, int x, int y)
{
    int i;

    for (i = 0; i < m_actors; i++) {
	if (i != actor && (m_actor[i].m_type == Block || m_actor[i].m_type == BlockCoin)) {
	    int xdiff = abs(m_actor[i].m_x - x);
	    int ydiff = abs(m_actor[i].m_y - y);
	    if (xdiff < 30 && ydiff < 30 && m_actor[i].m_act_dir != Stop) {
		return i;
	    }
	}
    }
    return -1;
}

int PlayField::detectCollisionBlockOrWall(int actor, int x, int y)
{
    int i;

    /* if we would hit an outside wall, just return the square of any wall on the map */
    if (x < 0 || y < 0 || x > ((max_cols - 1) * 30) || y > ((max_rows - 1) * 30)) {
	for (i = 0; i < m_actors; i++) {
	    if (m_actor[i].m_type == Wall) {
		return i;
	    }
	}
    }

    /* check for the known objects */
    for (i = 0; i < m_actors; i++) {
	if (i != actor
	    && (m_actor[i].m_type == Block || m_actor[i].m_type == BlockCoin
		|| m_actor[i].m_type == Wall)) {
	    int xdiff = abs(m_actor[i].m_x - x);
	    int ydiff = abs(m_actor[i].m_y - y);
	    if (xdiff < 30 && ydiff < 30) {
		return i;
	    }
	}
    }

    return -1;
}


int PlayField::detectCollisionFlame(int actor, int x, int y)
{
    int i;

    for (i = 0; i < m_actors; i++) {
	if (i != actor && m_actor[i].m_type == Flame) {
	    int xdiff = abs(m_actor[i].m_x - x);
	    int ydiff = abs(m_actor[i].m_y - y);
	    if (xdiff < 30 && ydiff < 30) {
		return i;
	    }
	}
    }
    return -1;
}

int PlayField::detectCollisionPlayer(int x, int y)
{
    int xdiff = abs(m_actor[0].m_x - x);
    int ydiff = abs(m_actor[0].m_y - y);
    if (xdiff < 30 && ydiff < 30) {
	return 0;
    } else {
	return -1;
    }
}

int PlayField::detectCollisionWall(int actor, int x, int y)
{
    int i;

    /* if we would hit an outside wall, just return the square of any wall on the map */
    if (x < 0 || y < 0 || x > ((max_cols - 1) * 30) || y > ((max_rows - 1) * 30)) {
	for (i = 0; i < m_actors; i++) {
	    if (m_actor[i].m_type == Wall) {
		return i;
	    }
	}
    }

    /* check for the known objects */
    for (i = 0; i < m_actors; i++) {
	if (i != actor && m_actor[i].m_type == Wall) {
	    int xdiff = abs(m_actor[i].m_x - x);
	    int ydiff = abs(m_actor[i].m_y - y);
	    if (xdiff < 30 && ydiff < 30) {
		return i;
	    }
	}
    }

    return -1;
}

bool PlayField::hasReachedClickPosition(int x, int y)
{
    if (m_click_x != -1 && m_click_y != -1) {
	x = (x / 30) * 30;
	y = (y / 30) * 30;
	if (x == m_click_x && y == m_click_y) {
	    return true;
	}
    }
    return false;
}

bool PlayField::lineOfSight(int actor1, int actor2, enum Directions dir)
{
    int x1 = m_actor[actor1].m_x;
    int y1 = m_actor[actor1].m_y;
    int x2 = m_actor[actor2].m_x;
    int y2 = m_actor[actor2].m_y;
    int i;

    /* if we look from actor1 in direction dir to actor2, is there any
     * obstacle between actor1 and actor2?
     */
    for (i = 0; i < m_actors; i++) {
	if (i != actor1 && i != actor2) {
	    switch (dir) {
	    case Up:
		if (m_actor[i].m_y < y1 && m_actor[i].m_y > y2) {
		    int xdiff1 = abs(m_actor[i].m_x - x1);
		    int xdiff2 = abs(m_actor[i].m_x - x2);
		    if (xdiff1 < 15 || xdiff2 < 15) {
			return false;
		    }
		}
		break;
	    case Down:
		if (m_actor[i].m_y > y1 && m_actor[i].m_y < y2) {
		    int xdiff1 = abs(m_actor[i].m_x - x1);
		    int xdiff2 = abs(m_actor[i].m_x - x2);
		    if (xdiff1 < 15 || xdiff2 < 15) {
			return false;
		    }
		}
		break;
	    case Left:
		if (m_actor[i].m_x < x1 && m_actor[i].m_x > x2) {
		    int ydiff1 = abs(m_actor[i].m_y - y1);
		    int ydiff2 = abs(m_actor[i].m_y - y2);
		    if (ydiff1 < 15 || ydiff2 < 15) {
			return false;
		    }
		}
		break;
	    case Right:
		if (m_actor[i].m_x > x1 && m_actor[i].m_x < x2) {
		    int ydiff1 = abs(m_actor[i].m_y - y1);
		    int ydiff2 = abs(m_actor[i].m_y - y2);
		    if (ydiff1 < 15 || ydiff2 < 15) {
			return false;
		    }
		}
		break;
	    default:
		break;
	    }
	}
    }
    return true;
}

void PlayField::dump(const char *file, const char *function, int line)
{
    int i;

    printf("Dump in %s/%s, line %d:\n", file, function, line);
    printf("\n");
    printf("speed:%d, strength:%d, coins:%d\n", m_speed, m_strength, m_coins);
    printf("state:%d, counter:%d\n", m_state, m_counter);
    printf("score:%d, lives:%d, levels:%d, blocks:%d, coins:%d, flames:%d\n",
	m_score, m_lives, m_levels_completed, m_blocks_broken, m_coins_collected,
	m_flames_killed);
#ifdef CAN_TELEPORT
    printf("actors:%d, coinsleft:%d, click_x:%d, clicky:%d, teleport:%d\n",
	m_actors, m_coins_in_level, m_click_x, m_click_y, m_teleport);
#else
    printf("actors:%d, coinsleft:%d, click_x:%d, clicky:%d\n",
	m_actors, m_coins_in_level, m_click_x, m_click_y);
#endif
    printf("\n");
    for (i = 0; i < m_actors; i++) {
	printf("actor[%d] = {x:%d, y:%d, act_dir:%d, req_dir:%d, speed:%d, counter:%d, type:%d}\n",
	    i, m_actor[i].m_x, m_actor[i].m_y, m_actor[i].m_act_dir, m_actor[i].m_req_dir,
	    m_actor[i].m_speed, m_actor[i].m_counter, m_actor[i].m_type);
    }
    printf("\n");
}

void PlayField::SetMainView(CIceBloxxView* aView)
{
	iView = aView;
}

TBool PlayField::CheckForHiscoreL()
{
	TBool foundHi = EFalse;
	for(TInt loop=0;loop<10 && !foundHi;loop++)
	{
		if(m_score>m_Hiscores[loop])
		{
			for(TInt inner=9;inner>loop;inner--)
			{
				m_Hiscores[inner]= m_Hiscores[inner-1];
				m_HiscoreNames[inner]=m_HiscoreNames[inner-1];
			}
			m_Hiscores[loop] = m_score;
			TBuf<16> inputBuf = _L("");
			hideEvent();
			CIceBloxxHiscoreDlg* dialog = new (ELeave) CIceBloxxHiscoreDlg(inputBuf);
			dialog->ExecuteLD(R_GAME_NEW_HISCORE);
			m_HiscoreNames[loop] = inputBuf;
			foundHi = ETrue;
			showEvent();
		}
	}
	return foundHi;
}

void PlayField::ExternalizeL(RDictionaryWriteStream& aWriteStream) const
{
	TInt loop=0;
	for(loop=0;loop<10;loop++)
	{
		aWriteStream.WriteInt32L(m_Hiscores[loop]);
	}

	for(loop=0;loop<10;loop++)
	{
		aWriteStream.WriteInt32L(m_HiscoreNames[loop].Length());

		aWriteStream.WriteL(m_HiscoreNames[loop]);
	}
}

void PlayField::InternalizeL(RDictionaryReadStream& aReadStream)
{
	TInt loop = 0;
	for( loop=0;loop<10;loop++)
	{
		m_Hiscores[loop] = aReadStream.ReadInt32L();
	}
	TInt length = 0;
	for(loop=0;loop<10;loop++)
	{
		length= aReadStream.ReadInt32L();

		aReadStream.ReadL(m_HiscoreNames[loop],length);
	}
}

void  PlayField::LoadSoundsL()
{
	TFileName fileName = CEikonEnv::Static()->EikAppUi()->Application()->BitmapStoreName();
	TParsePtr appPath(fileName);
	TFileName wavName = appPath.DriveAndPath();
	wavName.Append(_L("IceBloxx\\clank.WAV"));
	m_SoundEffects[EPickUpCoinFx]= m_soundHandler->AddSoundSampleL(wavName);

	wavName = appPath.DriveAndPath();
	wavName.Append(_L("IceBloxx\\crush.WAV"));
	m_SoundEffects[ECrushIceFx] = m_soundHandler->AddSoundSampleL(wavName);;

	wavName = appPath.DriveAndPath();
	wavName.Append(_L("IceBloxx\\hurrah.WAV"));
	m_SoundEffects[ELevelCompleteFx] = m_soundHandler->AddSoundSampleL(wavName);;

	wavName = appPath.DriveAndPath();
	wavName.Append(_L("IceBloxx\\cry.WAV"));
	m_SoundEffects[EGameOverFx] = m_soundHandler->AddSoundSampleL(wavName);;

	wavName = appPath.DriveAndPath();
	wavName.Append(_L("IceBloxx\\sizzle.WAV"));
	m_SoundEffects[EKillFireFx] = m_soundHandler->AddSoundSampleL(wavName);

	wavName = appPath.DriveAndPath();
	wavName.Append(_L("IceBloxx\\block.WAV"));
	m_SoundEffects[EMoveBlockFx] = m_soundHandler->AddSoundSampleL(wavName);

}

⌨️ 快捷键说明

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