コード例 #1
0
void WeaponItemSubSystem::OnShot( core::ShotEvent const& Evt )
{
    Opt<Actor> actor( mScene.GetActor( Evt.mActorGUID ) );
    if ( !actor.IsValid() )
    {
        return;
    }
    Opt<IInventoryComponent> inventoryC = actor->Get<IInventoryComponent>();
    if ( !inventoryC.IsValid() )
    {
        return;
    }
    Opt<Weapon> weapon = inventoryC->GetSelectedWeapon();
    if ( !weapon.IsValid() )
    {
        return;
    }
    weapon->GetScatter().Shot( Evt.mIsAlt );
    if ( Evt.mIsAlt )
    {
        weapon->SetCooldown( weapon->GetShootAltCooldown() );
        weapon->SetBullets( weapon->GetBullets() - weapon->GetShotCostAlt() );
    }
    else
    {
        weapon->SetCooldown( weapon->GetShootCooldown() );
        weapon->SetBullets( weapon->GetBullets() - weapon->GetShotCost() );
    }
}
コード例 #2
0
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));
                }
            }
        }
    }
}