コード例 #1
0
ファイル: CPedSync.cpp プロジェクト: EagleShen/MTA
void CPedSync::Packet_PedSync ( CPedSyncPacket& Packet )
{
    // Grab the player
    CPlayer* pPlayer = Packet.GetSourcePlayer ();
    if ( pPlayer && pPlayer->IsJoined () )
    {
        // Apply the data for each ped in the packet
        vector < CPedSyncPacket::SyncData* > ::const_iterator iter = Packet.IterBegin ();
        for ( ; iter != Packet.IterEnd (); iter++ )
        {
            CPedSyncPacket::SyncData* pData = *iter;

            // Grab the ped this packet is for
            CElement* pPedElement = CElementIDs::GetElement ( pData->Model );
            if ( pPedElement && IS_PED ( pPedElement ) )
            {
                // Convert to a CPed
                CPed* pPed = static_cast < CPed* > ( pPedElement );

                // Is the player syncing this ped?
                // this packet if the time context matches.
                if ( pPed->GetSyncer () == pPlayer &&
                     pPed->CanUpdateSync ( pData->ucSyncTimeContext ) )
                {
                    // Apply the data to the ped
                    if ( pData->ucFlags & 0x01 )
                    {
                        pPed->SetPosition ( pData->vecPosition );
                        g_pGame->GetColManager()->DoHitDetection ( pPed->GetLastPosition (), pPed->GetPosition (), 0.0f, pPed );
                    }
                    if ( pData->ucFlags & 0x02 ) pPed->SetRotation ( pData->fRotation );
                    if ( pData->ucFlags & 0x04 ) pPed->SetVelocity ( pData->vecVelocity );

                    if ( pData->ucFlags & 0x08 )
                    {
                        // Less health than last time?
                        float fPreviousHealth = pPed->GetHealth ();
                        pPed->SetHealth ( pData->fHealth );

                        if ( pData->fHealth < fPreviousHealth )
                        {
                            // Grab the delta health
                            float fDeltaHealth = fPreviousHealth - pData->fHealth;

                            if ( fDeltaHealth > 0.0f )
                            {
                                // Call the onPedDamage event
                                CLuaArguments Arguments;
                                Arguments.PushNumber ( fDeltaHealth );
                                pPed->CallEvent ( "onPedDamage", Arguments );
                            }
                        }
                    }

                    if ( pData->ucFlags & 0x10 ) pPed->SetArmor ( pData->fArmor );

                    // Send this sync
                    pData->bSend = true;
                }
            }
        }

        // Tell everyone
        m_pPlayerManager->BroadcastOnlyJoined ( Packet, pPlayer );
    }
}