示例#1
0
//------------------------------------------------------------------------
void CCarryEntity::Attach( EntityId actorId )
{
    CPlayer *pPlayer = static_cast<CPlayer*>(g_pGame->GetIGameFramework()->GetIActorSystem()->GetActor(actorId));
    if (pPlayer)
    {
        const bool bIsOwnerFP = !pPlayer->IsThirdPerson();
        IEntity *pPlayerEntity = pPlayer->GetEntity();

        IInventory *pInventory = pPlayer->GetInventory();
        m_previousWeaponId = pInventory->GetCurrentItem();
        if (m_previousWeaponId == m_spawnedWeaponId)
        {
            // Current item is the spawned weapon - this won't be happy so get the previous item instead
            m_previousWeaponId = pInventory->GetLastItem();
            if (m_previousWeaponId == m_spawnedWeaponId)
            {
                // Item is still the spawned weapon, can't have this so default to 0
                m_previousWeaponId = 0;
            }
        }

        // Get the secondary weapon id - if the player doesn't have one then use the spawned weapon
        EntityId secondaryWeaponId = pPlayer->SimpleFindItemIdInCategory("secondary");
        bool bIsSpawnedWeapon = false;
        if (secondaryWeaponId == 0)
        {
            secondaryWeaponId = m_spawnedWeaponId;
            bIsSpawnedWeapon = true;
        }

        bool bDoAttach = true;
        // If swimming attach here - DT#17176 Picking up relay/tick while swimming was reverting to main weapon instead of primary when leaving water, and not hiding attached item.
        if (pPlayer->IsSwimming())
        {
            AttachmentUtils::AttachObject( bIsOwnerFP, pPlayerEntity, GetEntity(), CARRYENTITY_ATTACHMENT_NAME );
            bDoAttach = false;
        }

        EntityId holsteredId = pInventory->GetHolsteredItem();
        if (holsteredId)
        {
            if (pPlayer->IsClient())
            {
                pPlayer->HideLeftHandObject(true);
            }
            m_cachedLastItemId = holsteredId;
            pInventory->SetHolsteredItem(secondaryWeaponId);

            if (gEnv->bServer && bIsSpawnedWeapon)
            {
                pPlayer->PickUpItem(secondaryWeaponId, false, false);
            }
        }
        else
        {
            pPlayer->CancelScheduledSwitch();
            if (!bIsSpawnedWeapon)
            {
                pPlayer->SelectItem(secondaryWeaponId, true, true);
            }
            else if (gEnv->bServer)
            {
                pPlayer->PickUpItem(secondaryWeaponId, false, true);
            }
        }


        m_bSpawnedWeaponAttached = bIsSpawnedWeapon;

        pPlayer->EnableSwitchingItems(false);
        pPlayer->EnableIronSights(false);
        pPlayer->EnablePickingUpItems(false);

        pPlayer->SetTagByCRC( m_playerTagCRC, true );

        if(bDoAttach)
        {
            // If not swimming, attach here !B DT10978 Fix for issues with left handed weapons and picking up relays/extraction ticks. We now start the weapon switch prior to attaching the objective so that the left handed weapon doesn't clear it from the left hand
            AttachmentUtils::AttachObject( bIsOwnerFP, pPlayerEntity, GetEntity(), CARRYENTITY_ATTACHMENT_NAME );
        }
    }

    m_attachedActorId = actorId;
}
示例#2
0
//------------------------------------------------------------------------
void CCarryEntity::Detach()
{
    CPlayer *pPlayer = static_cast<CPlayer*>(g_pGame->GetIGameFramework()->GetIActorSystem()->GetActor(m_attachedActorId));
    if (pPlayer)
    {
        IEntity *pPlayerEntity = pPlayer->GetEntity();
        IEntity *pEntity = GetEntity();

        pPlayer->SetTagByCRC( m_playerTagCRC, false );

        AttachmentUtils::DetachObject( pPlayerEntity, GetEntity(), CARRYENTITY_ATTACHMENT_NAME );

        GetEntity()->DetachThis(IEntity::ATTACHMENT_KEEP_TRANSFORMATION, 0);

        pEntity->SetRotation(Quat::CreateRotationXYZ(Ang3(0.f, 0.f, 0.f)));

        EntityId itemIdToUse = m_cachedLastItemId;
        if (!itemIdToUse)
        {
            itemIdToUse = m_previousWeaponId;
        }

        IInventory *pInventory = pPlayer->GetInventory();
        if (pPlayer->IsSwimming())
        {
            pInventory->SetLastItem(itemIdToUse);
        }
        else
        {
            if (itemIdToUse)
            {
                EntityId holsteredId = pInventory->GetHolsteredItem();
                if (holsteredId)
                {
                    pInventory->SetHolsteredItem(itemIdToUse);
                }
                else
                {
                    pPlayer->SelectItem(itemIdToUse, false, true);
                }
            }
        }

        if (m_bSpawnedWeaponAttached)
        {
            pInventory->RemoveItem(m_spawnedWeaponId);
            IEntity *pSpawnedEntity = gEnv->pEntitySystem->GetEntity(m_spawnedWeaponId);
            if (pSpawnedEntity)
            {
                pSpawnedEntity->SetPos(Vec3(0.f, 0.f, 0.f));
            }
        }

        pPlayer->EnableSwitchingItems(true);
        pPlayer->EnableIronSights(true);
        pPlayer->EnablePickingUpItems(true);
    }

    m_attachedActorId = 0;
    m_previousWeaponId = 0;
    m_cachedLastItemId = 0;
    m_bSpawnedWeaponAttached = false;
}