void LevelScene::collectGarbagePlayers()
{
    QVector<LVL_Player *> stillVizible;//Avoid camera crash (which uses a cached render list)
    while(!dead_players.isEmpty())
    {
        LVL_Player *corpse = dead_players.last();
        dead_players.pop_back();
        if(corpse->isInRenderList())
        {
            stillVizible.push_back(corpse);//Avoid camera crash (which uses a cached render list)
            continue;
        }
        LVL_Player::deathReason reason = corpse->kill_reason;

        #if (QT_VERSION >= 0x050400)
        players.removeAll(corpse);
        #else
        //He-he, it's a great workaround for a Qt less than 5.4 which has QVector without removeAll() function
        while(1)
        {
            const QVector<LVL_Player *>::const_iterator ce = players.cend(), cit = std::find(players.cbegin(), ce, corpse);
            if (cit == ce)
                break;
            const QVector<LVL_Player *>::iterator e = players.end(), it = std::remove(players.begin() + (cit - players.cbegin()), e, corpse);
            players.erase(it, e);
            break;
        }
        #endif
        luaEngine.destoryLuaPlayer(corpse);

        switch(reason)
        {
        case LVL_Player::deathReason::DEAD_burn:
        case LVL_Player::deathReason::DEAD_fall:
        case LVL_Player::deathReason::DEAD_killed:
            if(players.size() > 0)
                PGE_Audio::playSoundByRole(obj_sound_role::PlayerDied);
            else
            {
                Mix_HaltChannel(-1);
                PGE_Audio::playSoundByRole(obj_sound_role::LevelFailed);
            }
            break;
        }
        if(reason==LVL_Player::deathReason::DEAD_burn)
            PGE_Audio::playSoundByRole(obj_sound_role::NpcLavaBurn);
    }
    dead_players.append(stillVizible);

    if(players.isEmpty())
    {
        PGE_MusPlayer::MUS_stopMusic();
        setExiting(4000, LvlExit::EXIT_PlayerDeath);
    }
}
示例#2
0
// Set our start values and load our menu textures if it's our first time at the screen.
MainMenu::MainMenu()
{
	setCurrentSelection(0);
	setSplashState(true);
	setChangedSelection(false);
	setExiting(false);

	hScoreString = "High score: ";

	setSelectedTexture("Select");
	setSplashTexture("Splash");
	setFontMap("Font");

	if (!loaded)
	{
		loadTexture(selectedTexture, "../textures/select.png", 1, 1);
		loadTexture(splashTexture, "../textures/splash.bmp", 1, 1);
		loadTexture(fontMap, "../textures/fontmap.png", 16, 16);
	}
}
void LevelScene::update()
{
    if(m_luaEngine.shouldShutdown())
    {
        m_fader.setFade(10, 1.0, 1.0);
        setExiting(0, LvlExit::EXIT_Error);
    }

    Scene::update();

    if(!m_isPauseMenu)
        tickAnimations(uTickf);

    if(!m_isLevelContinues)
    {
        //Level exit timeout
        m_exitLevelDelay -= uTickf;

        if(m_exitLevelDelay <= 0.0)
        {
            m_doExit = true;

            if(m_fader.isNull())
            {
                if(PGE_MusPlayer::isPlaying())
                    PGE_MusPlayer::fadeOut(500);

                m_fader.setFade(10, 1.0, 0.01);
            }
        }
    }

    if(m_doExit)
    {
        if(m_exitLevelCode == LvlExit::EXIT_Closed)
        {
            m_fader.setFull();
            m_isRunning = false;
        }
        else
        {
            if(m_fader.isFull())
                m_isRunning = false;
        }
    }
    else if(m_isPauseMenu)
        processPauseMenu();
    else
    {
        //Update physics is not pause menu
        updateLua();//Process LUA code
        m_systemEvents.processEvents(uTickf);
        m_events.processTimers(uTickf);

        //update cameras
        for(PGE_LevelCamera &cam : m_cameras)
            cam.updatePre(uTickf);

        processEffects(uTickf);

        if(!m_isTimeStopped) //if activated Time stop bonus or time disabled by special event
        {
            //Make world step
            processPhysics(uTickf);
        }

        while(!m_blockTransforms.empty())
        {
            transformTask_block x = m_blockTransforms.front();
            x.block->transformTo_x(x.id);
            m_blockTransforms.pop_front();
        }

        // Send controller states to controllable objects
        m_player1Controller->sendControls();
        m_player2Controller->sendControls();

        //update players
        for(LVL_Player *plr : m_itemsPlayers)
        {
            plr->update(uTickf);

            if(PGE_Window::showDebugInfo)
            {
                m_debug_player_jumping    = plr->m_jumpPressed;
                m_debug_player_onground   = plr->onGround();
                m_debug_player_foots      = (int)plr->l_contactB.size();
            }
        }

        for(size_t i = 0; i < m_blocksInFade.size(); i++)
        {
            if(m_blocksInFade[i]->tickFader(uTickf))
            {
                m_blocksInFade.erase(m_blocksInFade.begin() + (int)i);
                i--;
            }
        }

        //Process activated NPCs
        //for(size_t i = 0; i < m_npcActive.size(); i++)
        for(auto i = m_npcActive.begin(); i != m_npcActive.end();)
        {
            LVL_Npc *n = *i;
            n->update(uTickf);
            if(n->isKilled())
            {
                i = m_npcActive.erase(i);
                continue;
            }
            else if(n->activationTimeout <= 0)
            {
                if(!n->warpSpawing)
                    n->deActivate();
                if(n->wasDeactivated)
                {
                    if(!isVizibleOnScreen(n->m_momentum) || !n->isVisible() || !n->is_activity)
                    {
                        n->wasDeactivated = false;
                        i = m_npcActive.erase(i);
                        continue;
                    }
                }
            }
            ++i;
        }

        if(!m_isTimeStopped) //if activated Time stop bonus or time disabled by special event
        {
            //Process and resolve collisions
            processAllCollisions();
        }

        /***************Collect garbage****************/
        if(!m_npcDead.empty())
            collectGarbageNPCs();

        if(!m_playersDead.empty())
            collectGarbagePlayers();

        if(!m_blocksToDelete.empty())
            collectGarbageBlocks();

        /**********************************************/

        //update cameras
        for(PGE_LevelCamera &cam : m_cameras)
        {
            cam.updatePost(uTickf);
            //! --------------DRAW HUD--------------------------------------
            // TODO: Implement separated render queue for elements of HUD and provide render functions
            // are will draw HUD elements after world has drawn.
            LuaEngine *sceneLuaEngine = getLuaEngine();
            if(sceneLuaEngine)
            {
                if(sceneLuaEngine->isValid() && !sceneLuaEngine->shouldShutdown())
                {
                    LuaEvent drawHUDEvent = BindingCore_Events_Engine::createDrawLevelHUDEvent(sceneLuaEngine,
                                            &cam,
                                            &m_playerStates[(size_t)(cam.playerID - 1)]);
                    sceneLuaEngine->dispatchEvent(drawHUDEvent);
                }
            }
            //! ------------------------------------------------------------
        }

        //Add effects into the render table
        for(Scene_Effect &item : WorkingEffects)
        {
            renderArrayAddFunction([&item](double camPosX, double camPosY)
            {
                item.render(camPosX, camPosY);
            },  item.m_zIndex);
        }

        //Clear garbage (be careful!)
        //luaEngine.runGarbageCollector();
    }

    //Process interprocessing commands cache
    process_InterprocessCommands();
    //Process Z-sort of the render functions
    renderArrayPrepare();
    //Process message boxes
    m_messages.process();
}
示例#4
0
void LevelScene::addPlayer(PlayerPoint playerData, bool byWarp, int warpType, int warpDirect, bool cannon, double cannon_speed)
{
    LVL_Player *player = nullptr;

    if(m_luaEngine.isValid())
    {
        player = m_luaEngine.createLuaPlayer();

        if(player == nullptr)
            player = new LVL_Player(this);
    }
    else
        player = new LVL_Player(this);

    if(!player)
        throw(std::runtime_error("Out of memory [new LVL_Player] addPlayer"));

    player->m_scene = this;

    if(m_itemsPlayers.size() == 0)
        player->camera = &m_cameras.front();
    else if(m_itemsPlayers.size() == 1)
        player->camera = &m_cameras.back();

    int sID = findNearestSection(playerData.x, playerData.y);
    LVL_Section *sct = getSection(sID);

    if(!sct)
    {
        delete player;
        return;
    }

    player->setParentSection(sct);
    player->z_index = zOrder.player;
    player->m_global_state = ((static_cast<unsigned>(m_playerStates.size()) > (playerData.id - 1)) ?
                            &m_playerStates[static_cast<int>(playerData.id - 1)] : nullptr);
    player->setPlayerPointInfo(playerData);
    if(!player->init())
    {
        delete player;
        m_errorMsg = "Failed to initialize playable character!\nSee log file for more information!";
        m_fader.setFade(10, 1.0, 1.0);
        setExiting(0, LvlExit::EXIT_Error);
        return;
    }
    m_itemsPlayers.push_back(player);

    if(playerData.id == 1)
        m_player1Controller->registerInControl(player);
    else if(playerData.id == 2)
        m_player2Controller->registerInControl(player);

    if(byWarp)
    {
        player->setPaused(true);
        player->WarpTo(playerData.x, playerData.y, warpType, warpDirect, cannon, cannon_speed);

        if(warpType == 2)
            PGE_Audio::playSoundByRole(obj_sound_role::WarpDoor);
        else if(warpType == 0)
            PGE_Audio::playSoundByRole(obj_sound_role::PlayerMagic);
    }
    else
        player->camera->changeSection(sct, true);
}
void LevelScene::processPauseMenu()
{
    if(!m_pauseMenu_opened)
    {
        m_pauseMenu.restart();
        m_pauseMenu_opened = true;
        PGE_Audio::playSoundByRole(obj_sound_role::MenuPause);
    }
    else
    {
        m_pauseMenu.update(uTickf);
        if(!m_pauseMenu.isRunning())
        {
            switch(m_pauseMenuID)
            {
            case 1:
                switch(m_pauseMenu.answer())
                {
                case PAUSE_Continue:
                    //do nothing!!
                break;
                case PAUSE_SaveCont:
                    //Save game state!
                    m_gameState->save();
                break;
                case PAUSE_SaveQuit:
                    //Save game state! and exit from episode
                    m_gameState->save();
                    setExiting(0, LvlExit::EXIT_MenuExit);
                    break;
                case PAUSE_Exit:
                    //Save game state! and exit from episode
                    setExiting(0, LvlExit::EXIT_MenuExit);
                break;
                default: break;
                }
            break;
            case 2:
                switch(m_pauseMenu.answer())
                {
                case PAUSE_2_Continue:
                    //do nothing!!
                break;
                case PAUSE_2_Exit:
                    //Save game state! and exit from episode
                    setExiting(0, LvlExit::EXIT_MenuExit);
                break;
                default: break;
                }
                break;
            case 3:
                switch(m_pauseMenu.answer())
                {
                case PAUSE_3_Continue:
                    //do nothing!!
                break;
                case PAUSE_3_Replay:
                    //Replay same level again
                    setExiting(0, LvlExit::EXIT_ReplayRequest);
                break;
                case PAUSE_3_Exit:
                    //Save game state! and exit from episode
                    setExiting(0, LvlExit::EXIT_MenuExit);
                break;
                default: break;
                }
                break;
            }
            m_pauseMenu_opened=false;
            m_isPauseMenu=false;
        }
    }
}