예제 #1
0
void CollisionSystem::Update( double DeltaTime )
{
    static const auto enableCollision = Settings::Get().GetInt( "collisions.enable", 1 ) != 0;
    if( !enableCollision )
    {
        return;
    }
    mUpdateTimer.Log( "start collision" );
    mPerfTimer.Log( "pre build grid" );
    std::vector<std::pair<Opt<CollisionSubSystem>, Actor*>> collisionAndActors;
    for (auto actor : mScene.GetActorsFromMap( GetType_static() ))
    {
        Opt<ICollisionComponent> collisionC = actor->Get<ICollisionComponent>();
        if ( collisionC.IsValid() )
        {
            Opt<CollisionSubSystem> collisionSS = GetCollisionSubSystem( collisionC->GetId() );
            if ( collisionSS.IsValid() )
            {
                collisionAndActors.push_back( std::make_pair( collisionSS, actor ) );
                collisionSS->ClipScene( *actor );
            }
            mCollisionGrid.AddActor( actor, DeltaTime, collisionC );
        }
    }
    mPerfTimer.Log( "post build grid" );
    PossibleCollisions_t const& PossibleCollisions = mCollisionGrid.GetPossibleCollisions();
    for( PossibleCollisions_t::const_iterator i = PossibleCollisions.begin(), e = PossibleCollisions.end(); i != e; ++i )
    {
        Actor& A = *( i->A1 );
        Actor& B = *( i->A2 );
        Opt<ICollisionComponent> ACollisionC = A.Get<ICollisionComponent>();
        Opt<ICollisionComponent> BCollisionC = B.Get<ICollisionComponent>();
        BOOST_ASSERT( ACollisionC.IsValid() && BCollisionC.IsValid() ); //TODO: here this one should be true

        CollisionModel const& CollModel = mCollisionStore.GetCollisionModel( ACollisionC->GetCollisionClass(), BCollisionC->GetCollisionClass() );
        if( !CollModel.AreActorsColliding( A, B, DeltaTime ) )
        {
            continue;
        }
        //TODO: needs optimization, maybe a template parameter for SubSystemHolder to subsystem would do
        Opt<CollisionSubSystem> ACollisionSS = GetCollisionSubSystem( ACollisionC->GetId() );
        if ( ACollisionSS.IsValid() )
        {
            ACollisionSS->Collide( A, B );
        }
        Opt<CollisionSubSystem> BCollisionSS = GetCollisionSubSystem( BCollisionC->GetId() );
        if ( BCollisionSS.IsValid() )
        {
            BCollisionSS->Collide( B, A );
        }
    }
    mPerfTimer.Log( "post collide" );
    for (auto& collAndActor : collisionAndActors)
    {
        collAndActor.first->Update( *collAndActor.second, DeltaTime );
    }
    mPerfTimer.Log( "post collSS" );
    mUpdateTimer.Log( "end collision" );
}
예제 #2
0
void CollisionSystem::Update( double DeltaTime )
{
    mCollisionGrid.Clear();
    for( ActorList_t::iterator it = mScene.GetActors().begin(), e = mScene.GetActors().end(); it != e; ++it )
    {
        Actor& actor = **it;
        Opt<ICollisionComponent> collisionC = actor.Get<ICollisionComponent>();
        if ( collisionC.IsValid() )
        {
            mCollisionGrid.AddActor( &actor, DeltaTime );
            Opt<CollisionSubSystem> collisionSS = GetCollisionSubSystem( collisionC->GetId() );
            if ( collisionSS.IsValid() )
            {
                collisionSS->ClipScene( actor );
            }
        }
    }
    PossibleCollisions_t const& PossibleCollisions = mCollisionGrid.GetPossibleCollisions();
    for( PossibleCollisions_t::const_iterator i = PossibleCollisions.begin(), e = PossibleCollisions.end(); i != e; ++i )
    {
        Actor& A = *( i->A1 );
        Actor& B = *( i->A2 );
        Opt<ICollisionComponent> ACollisionC = A.Get<ICollisionComponent>();
        Opt<ICollisionComponent> BCollisionC = B.Get<ICollisionComponent>();
        BOOST_ASSERT( ACollisionC.IsValid() && BCollisionC.IsValid() ); //TODO: here this one should be true

        CollisionModel const& CollModel = mCollisionStore.GetCollisionModel( ACollisionC->GetCollisionClass(), BCollisionC->GetCollisionClass() );
        if( !CollModel.AreActorsColliding( A, B, DeltaTime ) )
        {
            continue;
        }
        //TODO: needs optimization, maybe a template parameter for SubSystemHolder to subsystem would do
        Opt<CollisionSubSystem> ACollisionSS = GetCollisionSubSystem( ACollisionC->GetId() );
        if ( ACollisionSS.IsValid() )
        {
            ACollisionSS->Collide( A, B );
        }
        Opt<CollisionSubSystem> BCollisionSS = GetCollisionSubSystem( BCollisionC->GetId() );
        if ( BCollisionSS.IsValid() )
        {
            BCollisionSS->Collide( B, A );
        }
    }
    for( ActorList_t::iterator it = mScene.GetActors().begin(), e = mScene.GetActors().end(); it != e; ++it )
    {
        Actor& actor = **it;
        Opt<ICollisionComponent> collisionC = actor.Get<ICollisionComponent>();
        if ( collisionC.IsValid() )
        {
            Opt<CollisionSubSystem> collisionSS = GetCollisionSubSystem( collisionC->GetId() );
            if ( collisionSS.IsValid() )
            {
                collisionSS->Update( actor, DeltaTime );
            }
        }
    }
}
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));
                }
            }
        }
    }
}