Ejemplo n.º 1
0
void PlayerPosDetector::processDetector()
{
    if(!_parentNPC) return;

    float d=0.0f;
    for(int i=0; i<_scene->players.size(); i++)
    {
        LVL_Player *p = _scene->players[i];
        float d1=distance(p);
        if((d1<d)||(i==0))
        {
            d=d1;
            pos = p->posRect.center();
            _playersDirection=p->direction();
            if(_parentNPC->posCenterX() < pos.x())
                _directedTo=1;
            else
            if(_parentNPC->posCenterX() > pos.x())
                _directedTo=-1;
            _plrptr=p;
            _plr_id=p->characterID;
            _plr_state=p->stateID;
        }
    }

}
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);
    }
}
Ejemplo n.º 3
0
void LevelScene::addPlayer(PlayerPoint playerData, bool byWarp, int warpType, int warpDirect)
{
    LVL_Player * player;
    if(luaEngine.isValid()){
        player = luaEngine.createLuaPlayer();
        if(player == nullptr)
            player = new LVL_Player();
    }else{
        player = new LVL_Player();
    }

    if(players.size()==0)
        player->camera = &cameras.first();
    else if(players.size()==1)
        player->camera = &cameras.last();

    int sID = findNearestSection(playerData.x, playerData.y);
    LVL_Section *sct = getSection(sID);
    if(!sct)
    {
        delete player;
        return;
    }
    player->setParentSection(sct);
    player->z_index = Z_Player;
    player->setPlayerPointInfo(playerData);
    player->init();
    players.push_back(player);

    if(playerData.id==1)
        player1Controller->registerInControl(player);
    else if(playerData.id==2)
        player2Controller->registerInControl(player);

    if(byWarp)
    {
        player->setPaused(true);
        player->WarpTo(playerData.x, playerData.y, warpType, warpDirect);
        if(warpType==2)
            PGE_Audio::playSoundByRole(obj_sound_role::WarpDoor);
        else if(warpType==0)
            PGE_Audio::playSoundByRole(obj_sound_role::PlayerMagic);
    }
}
void LevelScene::onKeyboardPressedSDL(SDL_Keycode sdl_key, Uint16)
{
    if(m_doExit || isExit())
        return;

    if(m_isPauseMenu && (m_player1Controller->type() != Controller::type_keyboard))
        m_pauseMenu.processKeyEvent(sdl_key);

    switch(sdl_key)
    {
    // Check which
    case SDLK_ESCAPE:
    case SDLK_RETURN:   // Toggle pause mode
    case SDLK_AC_BACK:
    {
        if(m_isPauseMenu && (m_player1Controller->type() == Controller::type_keyboard))
            m_pauseMenu.processKeyEvent(sdl_key);

        if(m_doExit || m_isPauseMenu)
            break;

        m_isPauseMenu = true;
        break;
    }

    case SDLK_BACKQUOTE:
    {
        PGE_Debugger::executeCommand(this);
        break;
    }

    default:

        //Keys allowed only in debug mode!
        if(PGE_Debugger::cheat_debugkeys)
        {
            switch(sdl_key)
            {
            case SDLK_1:
            {
                if(!m_itemsPlayers.empty())
                    launchEffect(1, m_itemsPlayers.front()->posX(), m_itemsPlayers.front()->posY(), 0, 2000, 0, 0, 0);
            }
            break;

            case SDLK_2:
            {
                if(!m_itemsPlayers.empty())
                    launchEffect(1, m_itemsPlayers.front()->posX(), m_itemsPlayers.front()->posY(), 0, 2000, 3, -6, 12);

                break;
            }

            case SDLK_3:
            {
                if(!m_itemsPlayers.empty())
                {
                    Scene_Effect_Phys p;
                    p.decelerate_x = 0.02;
                    p.max_vel_y = 12;
                    LVL_Player *pl = m_itemsPlayers.front();
                    launchEffect(1, pl->posX(), pl->posY(), 0, 5000, -3, -6, 5, 0, p);
                    launchEffect(1, pl->posX(), pl->posY(), 0, 5000, -4, -7, 5, 0, p);
                    launchEffect(1, pl->posX(), pl->posY(), 0, 5000, 3, -6, 5, 0, p);
                    launchEffect(1, pl->posX(), pl->posY(), 0, 5000, 4, -7, 5, 0, p);
                }

                break;
            }

            case SDLK_4:
            {
                if(!m_itemsPlayers.empty())
                {
                    Scene_Effect_Phys p;
                    p.max_vel_y = 12;
                    LVL_Player *pl = m_itemsPlayers.front();
                    launchEffect(11, pl->posX(), pl->posY(), 0, 5000, 0, -3, 12, 0, p);
                }

                break;
            }

            case SDLK_5:
            {
                if(!m_itemsPlayers.empty())
                    launchEffect(10, m_itemsPlayers.front()->posX(), m_itemsPlayers.front()->posY(), 1, 0, 0, 0, 0);
                break;
            }

            case SDLK_7:
            {
                if(!m_itemsPlayers.empty())
                    m_itemsPlayers[0]->setCharacterSafe(m_itemsPlayers[0]->characterID - 1, m_itemsPlayers[0]->stateID);

                break;
            }

            case SDLK_8:
            {
                if(!m_itemsPlayers.empty())
                    m_itemsPlayers[0]->setCharacterSafe(m_itemsPlayers[0]->characterID + 1, m_itemsPlayers[0]->stateID);

                break;
            }

            case SDLK_9:
            {
                if(m_itemsPlayers.size() >= 2)
                    m_itemsPlayers[1]->setCharacterSafe(2, 1);

                if(!m_itemsPlayers.empty())
                    m_itemsPlayers[0]->setCharacterSafe(m_itemsPlayers[0]->characterID, m_itemsPlayers[0]->stateID - 1);

                break;
            }

            case SDLK_0:
            {
                if(m_itemsPlayers.size() >= 2)
                    m_itemsPlayers[1]->setCharacterSafe(2, 2);
                else if(!m_itemsPlayers.empty())
                    m_itemsPlayers[0]->setCharacterSafe(m_itemsPlayers[0]->characterID, m_itemsPlayers[0]->stateID + 1);

                break;
            }

            case SDLK_F5:
            {
                PGE_Audio::playSoundByRole(obj_sound_role::PlayerMagic);
                m_isTimeStopped = !m_isTimeStopped;
                break;
            }

            case SDLK_F6:
            {
                PGE_Audio::playSoundByRole(obj_sound_role::CameraSwitch);
                m_debug_slowTimeMode = !m_debug_slowTimeMode;
                break;
            }

            case SDLK_F7:
            {
                PGE_Audio::playSoundByRole(obj_sound_role::WorldOpenPath);
                m_debug_oneStepMode = !m_debug_oneStepMode;
                break;
            }

            case SDLK_F8:
            {
                if(m_debug_oneStepMode)
                {
                    PGE_Audio::playSoundByRole(obj_sound_role::WorldMove);
                    m_debug_oneStepMode_doStep = true;
                }

                break;
            }

            default:
                break;
            }//switch(sdl_key)
        }//if(PGE_Debugger::cheat_debugkeys)
    }//switch(sdl_key)
}
LevelScene::~LevelScene()
{
    GlRenderer::clearScreen();
    //destroy textures
    size_t i = 0;
    D_pLogDebugNA("clear level textures");

    for(i = 0; i < m_texturesBank.size(); i++)
        GlRenderer::deleteTexture(m_texturesBank[i]);

    m_texturesBank.clear();
    D_pLogDebugNA("Destroy cameras");
    m_cameras.clear();

    D_pLogDebugNA("Destroy players");
    for(i = 0; i < m_itemsPlayers.size(); i++)
    {
        LVL_Player *tmp = m_itemsPlayers[i];
        if(tmp)
        {
            if(m_player1Controller)
                m_player1Controller->removeFromControl(tmp);
            if(m_player2Controller)
                m_player2Controller->removeFromControl(tmp);
            tmp->unregisterFromTree();
            if(!tmp->m_isLuaPlayer)
                delete tmp;
        }
    }

    D_pLogDebugNA("Destroy blocks");
    for(auto tmp : m_itemsBlocks)
    {
        if(tmp)
        {
            m_layers.removeRegItem(tmp->data.layer, tmp);
            tmp->unregisterFromTree();
            delete tmp;
        }
    }
    m_itemsBlocks.clear();

    D_pLogDebugNA("Destroy BGO");
    for(auto tmp : m_itemsBgo)
    {
        if(tmp)
        {
            m_layers.removeRegItem(tmp->data.layer, tmp);
            tmp->unregisterFromTree();
            delete tmp;
        }
    }
    m_itemsBlocks.clear();

    D_pLogDebugNA("Destroy NPC");
    for(auto tmp : m_itemsNpc)
    {
        if(tmp)
        {
            tmp->unregisterFromTree();
            if(!tmp->isLuaNPC)
                delete tmp;
        }
    }
    m_itemsNpc.clear();

    D_pLogDebugNA("Destroy Warps");
    for(auto tmp : m_itemsWarps)
    {
        if(tmp)
        {
            m_layers.removeRegItem(tmp->data.layer, tmp);
            tmp->unregisterFromTree();
            delete tmp;
        }
    }
    m_itemsWarps.clear();

    D_pLogDebugNA("Destroy Physical Environment zones");
    for(auto tmp : m_itemsPhysEnvs)
    {
        if(tmp)
        {
            m_layers.removeRegItem(tmp->data.layer, tmp);
            tmp->unregisterFromTree();
            delete tmp;
        }
    }
    m_itemsPhysEnvs.clear();

    //Delete layers AFTER all objects are been deleted
    m_layers.clear();
    m_switchBlocks.clear();

    D_pLogDebugNA("Destroy sections");
    m_sections.clear();
    m_luaEngine.shutdown();
    destroyLoaderTexture();
    delete m_player1Controller;
    delete m_player2Controller;
    m_texturesBank.clear();
}
Ejemplo n.º 6
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);
}