void BounceCollisionSubSystem::Collide( Actor& actor, Actor& other )
{
    Opt<BounceCollisionComponent> bounceCC = actor.Get<BounceCollisionComponent>();
    if (mShotCollisionSubSystem.IsValid() && bounceCC->IsUseShotCollision())
    {
        mShotCollisionSubSystem->Collide(actor, other);
    }
    //TODO: for now its wallcc should be a bouncableComponent or this should be a collision class
    Opt<WallCollisionComponent> wallCC = other.Get<WallCollisionComponent>();
    if( !wallCC.IsValid() )
    {
        return;
    }
    Opt<IPositionComponent> positionC = actor.Get<IPositionComponent>();
    Opt<IMoveComponent> moveC = actor.Get<IMoveComponent>();

    Opt<IPositionComponent> otherPositionC = other.Get<IPositionComponent>();
    Opt<ICollisionComponent> otherCC = other.Get<ICollisionComponent>();
    if ( !otherPositionC.IsValid() || !otherCC.IsValid() || !positionC.IsValid() )
    {
        return;
    }
    double const dx = otherPositionC->GetX() - positionC->GetX();
    double const dy = otherPositionC->GetY() - positionC->GetY();

    const double h = moveC->GetHeading();
    double c = cos( h );
    double s = sin( h );
    double at = atan2( s, c );
    double at2 = atan2( c, s );
    if( std::abs( dx ) > std::abs( dy ) )
    {
        c *= -1;
    }
    else if( std::abs( dx ) < std::abs( dy ) )
    {
        s *= -1;
    }
    moveC->SetHeading( atan2( s, c ) );
    moveC->GetSpeed().mBase.Set( moveC->GetSpeed().mBase.Get() * ( 1.0 - bounceCC->GetSpeedLossPercent() ) );
    if (bounceCC->GetHitCountToKill() > 0)
    {
        bounceCC->SetHitCountToKill(bounceCC->GetHitCountToKill() - 1);
    }
    if (bounceCC->IsResetActorsCollidedOnBounce())
    {
        bounceCC->ResetDamagedActorIds();
    }
}
示例#2
0
glm::vec4 Grid::Box( Actor const& Obj, double Dt )const
{
    Opt<IMoveComponent> moveC = Obj.Get<IMoveComponent>();
    float const MvX = moveC.IsValid() ? Dt * moveC->GetSpeedX() : 0.0f;
    float const MvY = moveC.IsValid() ? Dt * moveC->GetSpeedY() : 0.0f;

    double const Radius = Obj.Get<ICollisionComponent>()->GetRadius();
    Opt<IPositionComponent> const objPositionC = Obj.Get<IPositionComponent>();
    double const Ox = objPositionC->GetX() - mMin.x;
    double const Oy = objPositionC->GetY() - mMin.y;
    glm::vec4 Ret( Ox - Radius,
                   Oy - Radius,
                   Ox + Radius,
                   Oy + Radius );
    if( MvX < 0.0 )
    {
        Ret.x += MvX;
    }
    else
    {
        Ret.z += MvX;
    }
    if( MvY < 0.0 )
    {
        Ret.y += MvY;
    }
    else
    {
        Ret.w += MvY;
    }
    return Ret;
}
void MouseAdapterSystem::Update( double DeltaTime )
{
    Opt<Actor> actor( mScene.GetActor( mProgramState.mControlledActorGUID ) );
    if ( !actor.IsValid() )
    {
        return;
    }
    int32_t playerId = 1;
    static input::PlayerControlDevice& pcd( input::PlayerControlDevice::Get() );
    if( pcd.GetControlDevice( playerId ) != input::PlayerControlDevice::KeyboardAndMouse )
    {
        return;
    }
    InputState inputState = mInputSystem->GetInputState( playerId );

    Opt<IPositionComponent> actorPositionC = actor->Get<IPositionComponent>();
    inputState.mOrientation = atan2( mY - actorPositionC->GetY(), mX - actorPositionC->GetX() );

    inputState.mCursorX = mX;
    inputState.mCursorY = mY;

    if ( mMouse->IsButtonPressed( MouseSystem::Button_Left ) )
    {
        inputState.mShoot = true;
    }
    else if ( mMouse->IsButtonPressed( MouseSystem::Button_Right ) )
    {
        inputState.mShootAlt = true;
    }
    mInputSystem->SetInputState( playerId, inputState );
}
示例#4
0
void ParticleSystem::Update( double DeltaTime )
{
    for( auto actor: mScene.GetActorsFromMap( GetType_static() ) )
    {
        Opt<IEmitterComponent> emitterC = actor->Get<IEmitterComponent>();
        if (!emitterC.IsValid())
        {
            continue;
        }
        Opt<IPositionComponent> positionC = actor->Get<IPositionComponent>();
        if( !positionC.IsValid() )
        {
            continue;
        }
        Opt<IMoveComponent> moveC = actor->Get<IMoveComponent>();
        glm::vec2 distance(0);
        if ( moveC.IsValid() && moveC->IsMoving() && !moveC->IsRooted() )
        {
            distance = glm::vec2(moveC->GetSpeedX()*DeltaTime, moveC->GetSpeedY()*DeltaTime);
        }
        emitterC->Update( DeltaTime );
        std::vector<int32_t> const& emitted = emitterC->GetEmitTypes();
        for( std::vector<int32_t>::const_iterator ie = emitted.begin(), ee = emitted.end(); ie != ee; ++ie )
        {
            static ParticleEngine& pe( ParticleEngine::Get() );
            pe.AddParticle( *ie, glm::vec2( positionC->GetX(), positionC->GetY() ), distance, positionC->GetOrientation() );
        }
        emitterC->Emitted( emitted );
    }
}
void WeaponItemSubSystem::SetProjectilePosition(Actor& projectile, Actor& actor)
{
    Opt<IPositionComponent> projPositionC = projectile.Get<IPositionComponent>();
    Opt<IPositionComponent> actorPositionC = actor.Get<IPositionComponent>();
    glm::vec2 rvec(0.0, 0.0);
    Opt<IInventoryComponent> inventoryC = actor.Get<IInventoryComponent>();
    if (inventoryC.IsValid())
    {
        Opt<Weapon> weapon = inventoryC->GetSelectedWeapon();
        if (weapon.IsValid())
        {
            rvec.x += weapon->GetPositionX();
            rvec.y -= weapon->GetPositionY();
        }
    }
    rvec=glm::rotate(rvec, float(actorPositionC->GetOrientation()));
    projPositionC->SetX( projPositionC->GetX() + actorPositionC->GetX() + rvec.x );
    projPositionC->SetY( projPositionC->GetY() + actorPositionC->GetY() + rvec.y );
}
std::auto_ptr<PositionMessage> PositionMessageSenderSystem::GeneratePositionMessage( Actor const& actor )
{
    Opt<IPositionComponent> positionC = actor.Get<IPositionComponent>();
    if ( !positionC.IsValid() )
    {
        return std::auto_ptr<PositionMessage>();
    }
    std::auto_ptr<PositionMessage> positionMsg( new PositionMessage );
    positionMsg->mX = std::floor( positionC->GetX() * PRECISION );
    positionMsg->mY = std::floor( positionC->GetY() * PRECISION );
    //positionMsg->mOrientation=positionC->GetOrientation();
    positionMsg->mActorGUID = actor.GetGUID();
    return positionMsg;
}
int32_t GuardControllerSubSystem::GetDistanceSqr( Opt<IPositionComponent> positionC, Opt<IPositionComponent> otherPositionC )
{
    glm::vec2 const distV( (positionC->GetX() - otherPositionC->GetX()), (positionC->GetY() - otherPositionC->GetY()) );
    return glm::dot( distV, distV );
}
void WeaponItemSubSystem::Update( Actor& actor, double DeltaTime )
{
    Opt<IInventoryComponent> inventoryC = actor.Get<IInventoryComponent>();
    Opt<Weapon> weapon = inventoryC->GetSelectedWeapon();
    if ( !weapon.IsValid() )
    {
        return;
    }
    double cd = weapon->GetCooldown();
    cd -= DeltaTime;
    if( cd < 0 )
    {
        cd = 0;
    }
    weapon->SetCooldown( cd );
    //scatter updated on client
    Opt<IAccuracyComponent> accuracyC = actor.Get<IAccuracyComponent>();
    weapon->GetScatter().Update( DeltaTime, accuracyC.IsValid() ? accuracyC->GetAccuracy().Get() : 0 );

    if ( weapon->CanReload()
         //Not enough bullet for current shot  Need to see if the player wants to shoot alt:
         && ( weapon->GetShoot() && weapon->GetBullets() < weapon->GetShotCost()
              && weapon->GetCooldown() <= 0
              //Not enough bullet for current alt shot. Need to see if the player wants to shoot normal:
              || weapon->GetShootAlt() && weapon->GetBullets() < weapon->GetShotCostAlt()
              && weapon->GetCooldown() <= 0
              //Not enough bullet at all. This time the reload is a sure thing:
              || weapon->GetBullets() < weapon->GetShotCost()
              && weapon->GetBullets() < weapon->GetShotCostAlt() ) )
    {
        weapon->SetBullets( 0.0 );
        weapon->SetReloadTime( weapon->GetReloadTimeMax() );
        EventServer<ItemPropertiesChangedEvent>::Get().SendEvent( ItemPropertiesChangedEvent( *weapon ) );
    }

    weapon->SetReloadTime( weapon->GetReloadTime() - DeltaTime );

    if ( weapon->GetBullets() <= 0.0 )
    {
        if ( weapon->GetReloadTime() <= 0 && weapon->GetStaticReload() == 0.0 )
        {
            //todo: need to sync reloading with the server (-1 could occur, if a shot comes too fast, then it is reset by the end of the reload missing one bullet)
            weapon->SetBullets( weapon->GetBullets() + weapon->GetBulletsMax() );
            weapon->SetReloadTime( 0.0 );
        }
    }

    if ( weapon->GetReloadTime() <= 0 && weapon->GetStaticReload() > 0.0 )
    {
        weapon->SetBullets( std::min(
                                ( weapon->GetBullets() + weapon->GetStaticReload()/**DeltaTime*/ )
                                , weapon->GetBulletsMax() ) );
        weapon->SetReloadTime( weapon->GetReloadTimeMax() );
    }
    BindIds_t::iterator itemssIt = mSubSystems.get<SubSystemHolder::AllByBindId>().find( weapon->GetId() );
    if ( itemssIt != mSubSystems.get<SubSystemHolder::AllByBindId>().end() )
    {
        itemssIt->mSystem->Update( actor, DeltaTime );
    }

    if (mProgramState.mMode != core::ProgramState::Client)
    {
        if (weapon->GetCooldown() <= 0.0) //not synced to client
        {
            Opt<IPositionComponent> actorPositionC = actor.Get<IPositionComponent>();
            if (actorPositionC.IsValid())
            {
                if (weapon->IsShooting() && weapon->GetBullets() >= weapon->GetShotCost())
                {
                    if (weapon->GetMuzzleId() != -1)
                    {
                        std::auto_ptr<Actor> muzzle(mActorFactory(weapon->GetMuzzleId()));
                        SetProjectilePosition(*muzzle, actor);
                        BOOST_ASSERT(muzzle->Get<IPositionComponent>().IsValid());
                        muzzle->Get<IPositionComponent>()->SetOrientation(actorPositionC->GetOrientation());
                        mScene.AddActor(muzzle.release());
                    }
                    EventServer<core::ShotEvent>::Get().SendEvent(core::ShotEvent(actor.GetGUID(), glm::vec2(actorPositionC->GetX(), actorPositionC->GetY()), false));
                }
                else if (weapon->IsShootingAlt() && weapon->GetBullets() >= weapon->GetShotCostAlt())
                {
                    if (weapon->GetMuzzleAltId() != -1)
                    {
                        std::auto_ptr<Actor> muzzle(mActorFactory(weapon->GetMuzzleAltId()));
                        SetProjectilePosition(*muzzle, actor);
                        BOOST_ASSERT(muzzle->Get<IPositionComponent>().IsValid());
                        muzzle->Get<IPositionComponent>()->SetOrientation(actorPositionC->GetOrientation());
                        mScene.AddActor(muzzle.release());
                    }
                    EventServer<core::ShotEvent>::Get().SendEvent(core::ShotEvent(actor.GetGUID(), glm::vec2(actorPositionC->GetX(), actorPositionC->GetY()), true));
                }
            }
        }
    }
}
示例#9
0
void HealthBarRenderer::Draw()
{
    SceneVertices_t Vertices;
    Vertices.reserve( mPrevVertices.size() );
    typedef std::vector<glm::vec2> Positions_t;
    Positions_t Positions;
    Positions_t TexCoords;
    Positions.reserve( mPrevVertices.size() );
    Positions_t TextPosition;
    TextPosition.reserve( mPrevVertices.size() );
    Positions_t Dimensions;
    Dimensions.reserve( mPrevVertices.size() );

    typedef std::vector<glm::vec4> Colors_t;
    Colors_t Colors;
    Colors.reserve( mPrevVertices.size() );
    SceneVertexInserter_t Inserter( Vertices );
    struct ChangedAt
    {
        size_t Start;
        size_t Count;
    };
    int32_t lastVertexIndex = 0;

    for ( core::ProgramState::ClientDatas_t::iterator i = mProgramState.mClientDatas.begin(), e = mProgramState.mClientDatas.end(); i != e; ++i )
    {
        Opt<Actor> player( Scene::Get().GetActor( ( *i ).mClientActorGUID ) );
        if ( !player.IsValid() )
        {
            continue;
        }
        engine::CloakSystem::CloakState cloakState = engine::CloakSystem::GetCloakState( *player.Get() );
        if ( cloakState == engine::CloakSystem::Invisible )
        {
            continue;
        }
        Opt<IPositionComponent> positionC = player->Get<IPositionComponent>();

        Opt<IHealthComponent> healthC( player->Get<IHealthComponent>() );
        Opt<ITeamComponent> teamC( player->Get<ITeamComponent>() );

        Opt<IArmorComponent> armorC = player->Get<IArmorComponent>();
        int32_t armor = 0;
        if ( armorC.IsValid() )
        {
            armor = armorC->GetCurrentArmor();
        }

        double currPercent = healthC->GetHP() / double( healthC->GetMaxHP().Get() );
        double currArmorPercent = armor / double( healthC->GetMaxHP().Get() + armor );

        bool isself = i->mClientActorGUID == mProgramState.mControlledActorGUID;
        {
            glm::vec4 dim( int32_t( positionC->GetX() ) + mBorderPosition.x, int32_t( positionC->GetY() ) + mBorderPosition.y, mBorderSize.x, mBorderSize.y );
            glm::vec4 col = isself ? glm::vec4( 0.0, 0.0, 0.0, 0.7 ) : glm::vec4( 0.0, 0.0, 0.0, 0.7 );
            ColoredBox( dim, col, Inserter );
        }
        {
            glm::vec4 dim( int32_t( positionC->GetX() ) + mPosition.x, int32_t( positionC->GetY() ) + mPosition.y, currPercent * mSize.x, mSize.y );
            glm::vec4 col = isself ? glm::vec4( 0.59, 0.15, 0.7, 1.0 ) : glm::vec4( 0.0, 0.8, 0.0, 0.7 );
            if ( teamC.IsValid() )
            {
                col = mColorRepo( teamC->GetTeam() );
            }
            ColoredBox( dim, col, Inserter );
        }
        {
            double currCombinedPercent = currPercent + currArmorPercent > 1.0 ? 1 - currArmorPercent : currPercent;
            glm::vec4 dim( int32_t( positionC->GetX() ) + mPosition.x + currCombinedPercent * mSize.x
                           , int32_t( positionC->GetY() ) + mPosition.y
                           , currArmorPercent * mSize.x, mSize.y );
            glm::vec4 col = glm::vec4( 1.0, 1.0, 1.0, 0.7 );
            ColoredBox( dim, col, Inserter );
        }

    }
    size_t const CurSize = Vertices.size();
    if ( CurSize == 0 )
    {
        return;
    }
    SceneVertex vertex = Vertices.back();

    // todo: check and track changes
    for( SceneVertices_t::const_iterator i = Vertices.begin(), e = Vertices.end(); i != e; ++i )
    {
        SceneVertex const& Vert = *i;
        Positions.push_back( Vert.Pos );
        Colors.push_back( Vert.Color );
        TextPosition.push_back( Vert.RealPos );
        //Dimensions.push_back(Vert.Dimensions);
    }


    mVAO.Bind();
    if( CurSize != mPrevVertices.size() )
    {
        size_t TotalSize = CurSize * ( sizeof( glm::vec4 ) + 3 * sizeof( glm::vec2 ) );
        glBufferData( GL_ARRAY_BUFFER, TotalSize, NULL, GL_DYNAMIC_DRAW );
    }

    size_t CurrentOffset = 0;
    size_t CurrentSize = CurSize * sizeof( glm::vec2 );
    GLuint CurrentAttribIndex = 0;

    glBufferSubData( GL_ARRAY_BUFFER, CurrentOffset, CurrentSize, &Positions[0] );
    glEnableVertexAttribArray( CurrentAttribIndex );
    glVertexAttribPointer( CurrentAttribIndex, 2, GL_FLOAT, GL_FALSE, 0, ( GLvoid* )CurrentOffset );
    ++CurrentAttribIndex;
    CurrentOffset += CurrentSize;
    CurrentSize = CurSize * sizeof( glm::vec4 );
    glBufferSubData( GL_ARRAY_BUFFER, CurrentOffset, CurrentSize, &Colors[0] );
    glEnableVertexAttribArray( CurrentAttribIndex );
    glVertexAttribPointer( CurrentAttribIndex, 4, GL_FLOAT, GL_FALSE, 0, ( GLvoid* )CurrentOffset );
    ++CurrentAttribIndex;
    CurrentOffset += CurrentSize;
    CurrentSize = CurSize * sizeof( glm::vec2 );
    glBufferSubData( GL_ARRAY_BUFFER, CurrentOffset, CurrentSize, &TextPosition[0] );
    glEnableVertexAttribArray( CurrentAttribIndex );
    glVertexAttribPointer( CurrentAttribIndex, 2, GL_FLOAT, GL_FALSE, 0, ( GLvoid* )CurrentOffset );
    //     ++CurrentAttribIndex;
    //     CurrentOffset += CurrentSize;
    //     CurrentSize = CurSize * sizeof( glm::vec2 );
    //     glBufferSubData( GL_ARRAY_BUFFER, CurrentOffset, CurrentSize, &Dimensions[0] );
    //     glEnableVertexAttribArray( CurrentAttribIndex );
    //     glVertexAttribPointer( CurrentAttribIndex, 2, GL_FLOAT, GL_FALSE, 0, ( GLvoid* )CurrentOffset );



    ShaderManager& ShaderMgr( ShaderManager::Get() );
    ShaderMgr.ActivateShader( "health_bar" );
    int w, h;
    mWindow->GetWindowSize( w, h );
    glDrawArrays( GL_TRIANGLES, 0, CurSize );

    mVAO.Unbind();
    // store current match
    using std::swap;
    swap( mPrevVertices, Vertices );
    mTexts.clear();
}
void ControllerAdapterSystem::Update(double DeltaTime)
{
    Opt<InputSystem> inputsys = InputSystem::Get();
    if( !inputsys.IsValid() )
    {
        return;
    }

    // collect controlled players by controller id
    std::map<int32_t, Opt<Actor> > controlledPlayers;
    static core::ProgramState& ps =  core::ProgramState::Get();
    Opt<Actor> actor( mScene.GetActor( ps.mControlledActorGUID ) );
    Opt<core::ClientData> clientData( ps.FindClientDataByActorGUID( ps.mControlledActorGUID ) );
    if( clientData.IsValid() && actor.IsValid() )
    {
        controlledPlayers[ clientData->mControlledLocalPlayerId ] = actor;
    }

    for( auto joy : getJoysticks() )
    {
        // get keymapping for joy name @ joy pos ( if custom, support per-slot mapping )
        ControllerState cs( joy );
        if( !cs.isValid() )
        {
            continue;
        }

        if( mCalibrate )
        {
            std::cout << "Joy " << joy << "\n";
            std::cout << "\tname: '" << cs.name << "'\n";
            std::cout << "\taxes: \n";
            int cnt = 0;
            for( auto const& a : cs.axes )
            {
                std::cout << "\t\ta" << cnt++ << " " << a << "\n";
            }
            std::cout << "\tbuttons: \n";
            cnt = 0;
            for( auto const& b : cs.buttons )
            {
                std::cout << "\t\tb" << cnt++ << " " << b << "\n";
            }
        }

        // get controlled player for joy
        int32_t controlledLocalPlayerId = 1;
        static input::PlayerControlDevice& pcd( input::PlayerControlDevice::Get() );
        if( pcd.GetControlDevice( controlledLocalPlayerId ) != input::PlayerControlDevice::Controller ||
            pcd.GetControllerIndex( controlledLocalPlayerId ) != joy )
        {
            continue;
        }

        // get InputState for player
        InputState is = inputsys->GetInputState( controlledLocalPlayerId );

        // fill inputstate for player
        is.mShoot = cs.getBool( "shoot" );
        is.mShootAlt = cs.getBool( "shoot_alt" );
        is.mUseNormalItem = cs.getBool( "use_normal" );
        is.mReload = cs.getBool( "reload" );
        is.mShowLeaderboard = cs.getBool( "show_leaderboard" );
        is.mPause = cs.getBool( "pause" );
        double vh = cs.getDouble( "view_horizontal" ), vv = cs.getDouble( "view_vertical" );
        static Settings& settings( Settings::Get() );
        static double const viewNullZone = settings.GetDouble( "controllers.view_null_zone", 0.4 );
        if( std::abs( vv ) >= viewNullZone || std::abs( vh ) >= viewNullZone )
        {
            is.mOrientation = atan2( vv, vh );
        }
        Opt<Actor> actor = controlledPlayers[ controlledLocalPlayerId ];
        if( actor.IsValid() )
        {
            Opt<IPositionComponent> actorPositionC = actor->Get<IPositionComponent>();
            static double const radius = settings.GetDouble( "controllers.cursor_radius", 400 );
            is.mCursorX = actorPositionC->GetX() + radius * cos( is.mOrientation );
            is.mCursorY = actorPositionC->GetY() + radius * sin( is.mOrientation );
        }
        double mh = cs.getDouble( "move_horizontal" ), mv = cs.getDouble( "move_vertical" );
        is.mHeading = atan2( mv, mh );
        static double const moveNullZone = settings.GetDouble( "controllers.move_null_zone", 0.2 );
        is.mMoving = std::abs( mv ) >= moveNullZone || std::abs( mh ) >= moveNullZone;
        inputsys->SetInputState( controlledLocalPlayerId, is );
        // TODO fill generic input state ( ui )
    }
}