Beispiel #1
0
    /// <summary>
    /// re-caches all of the standard UFPS local player components
    /// </summary>
    private static void RefreshPlayerComponents()
    {
        m_FPCamera        = null;
        m_FPController    = null;
        m_FPInput         = null;
        m_FPDamageHandler = null;
        m_FPWeaponHandler = null;
        m_Inventory       = null;
        m_Respawner       = null;
        m_FPBodyAnimator  = null;
        m_RagdollHandler  = null;
        m_FootFXHandler   = null;

        if (m_FPEventHandler == null)
        {
            return;
        }

        m_FPCamera        = m_FPEventHandler.GetComponentInChildren <vp_FPCamera>();
        m_FPController    = m_FPEventHandler.GetComponentInChildren <vp_FPController>();
        m_FPInput         = m_FPEventHandler.GetComponentInChildren <vp_FPInput>();
        m_FPDamageHandler = m_FPEventHandler.GetComponentInChildren <vp_FPPlayerDamageHandler>();
        m_FPWeaponHandler = m_FPEventHandler.GetComponentInChildren <vp_FPWeaponHandler>();
        m_Inventory       = m_FPEventHandler.GetComponentInChildren <vp_PlayerInventory>();
        m_Respawner       = m_FPEventHandler.GetComponentInChildren <vp_PlayerRespawner>();
        m_FPBodyAnimator  = m_FPEventHandler.GetComponentInChildren <vp_FPBodyAnimator>();
        m_RagdollHandler  = m_FPEventHandler.GetComponentInChildren <vp_RagdollHandler>();
        m_FootFXHandler   = m_FPEventHandler.GetComponentInChildren <vp_PlayerFootFXHandler>();
    }
    /// <summary>
    ///
    /// </summary>
    public static int GenerateRespawnersForAllDamageHandlers()
    {
        // --- update old vp_PlayerDamageHandlers to the new vp_FPPlayerDamageHandler ---

        vp_PlayerDamageHandler[] oldPlayerDamageHandlers = FindObjectsOfType(typeof(vp_PlayerDamageHandler)) as vp_PlayerDamageHandler[];
        if (oldPlayerDamageHandlers != null && oldPlayerDamageHandlers.Length > 0)
        {
            foreach (vp_PlayerDamageHandler p in oldPlayerDamageHandlers)
            {
                // if this vp_PlayerDamageHandler is on the same transform as a
                // vp_FPPlayerEventHandler we will boldly assume that it's an
                // object from UFPS 1.4.6b or older which needs to be updated.
                // if not, it might be a new and valid vp_PlayerDamageHandler
                // (added to something like a remote player) and we'll leave it

                if (p.transform.GetComponent <vp_FPPlayerEventHandler>() == null)
                {
                    continue;
                }

                vp_FPPlayerDamageHandler n = p.gameObject.AddComponent <vp_FPPlayerDamageHandler>();

                n.AllowFallDamage        = p.AllowFallDamage;
                n.DeathEffect            = p.DeathEffect;
                n.DeathSound             = p.DeathSound;
                n.DeathSpawnObjects      = p.DeathSpawnObjects;
                n.FallDamageThreshold    = p.FallDamageThreshold;
                n.ImpactDamageMultiplier = p.ImpactDamageMultiplier;
                n.ImpactDamageThreshold  = p.ImpactDamageThreshold;
                n.m_Audio            = p.m_Audio;
                n.CurrentHealth      = p.CurrentHealth;
                n.m_StartPosition    = p.m_StartPosition;
                n.m_StartRotation    = p.m_StartRotation;
                n.MaxDeathDelay      = p.MaxDeathDelay;
                n.MaxHealth          = p.MaxHealth;
                n.MaxRespawnTime     = p.MaxRespawnTime;
                n.MinDeathDelay      = p.MinDeathDelay;
                n.MinRespawnTime     = p.MinRespawnTime;
                n.RespawnCheckRadius = p.RespawnCheckRadius;
                n.Respawns           = p.Respawns;
                n.RespawnSound       = p.RespawnSound;

                DestroyImmediate(p);
            }
        }

        // --- move respawn variables of all damagehandlers to new respawner components ---

        vp_DamageHandler[] damageHandlers         = FindObjectsOfType(typeof(vp_DamageHandler)) as vp_DamageHandler[];
        vp_DamageHandler[] FPPlayerDamageHandlers = FindObjectsOfType(typeof(vp_FPPlayerDamageHandler)) as vp_DamageHandler[];

        int amountOfObjectsUpdated = 0;

        foreach (vp_DamageHandler d in damageHandlers)
        {
            if (d.CreateTempRespawner())
            {
                amountOfObjectsUpdated++;
            }
        }

        foreach (vp_DamageHandler d in FPPlayerDamageHandlers)
        {
            if (d.CreateTempRespawner())
            {
                amountOfObjectsUpdated++;
            }
        }

        return(amountOfObjectsUpdated);
    }
    /// <summary>
    /// creates a copy of the currently selected UFPS player gameobject
    /// - stripped of all its 1st person functionality. the new object
    /// can be used for AI or multiplayer remote players
    /// </summary>
    public static void Generate()
    {
        GameObject target = Selection.activeObject as GameObject;

        if ((target == null) || (!vp_Utility.IsActive(target)) || (target.GetComponentInChildren <vp_FPController>() == null) && (target.GetComponentInChildren <vp_FPCamera>() == null))
        {
            EditorUtility.DisplayDialog("Failed to run wizard", "Please select the main gameobject of a 1st person player in the Hierarchy view (make sure it's active) and try again.", "OK");
            return;
        }

        if (!EditorUtility.DisplayDialog("Generate Remote Player?", "This wizard will create a copy of the selected player object - stripped of all its 1st person functionality. This new object can be used for AI or multiplayer remote players.\n\nNOTE: Only default UFPS classes will be processed.", "OK", "Cancel"))
        {
            return;
        }

        DecideCopyStates(target);

        string name = target.name + "(Remote)";

        // generate - and operate upon - a copy of the target
        target      = (GameObject)GameObject.Instantiate(target);
        target.name = target.name.Replace("(Clone)", "");

        try
        {
            // layer likely should no longer be 'LocalPlayer', so default to 'RemotePlayer'
            target.gameObject.layer = vp_Layer.RemotePlayer;

            // convert weapons
            ConvertWeaponsTo3rdPerson(target);

            // find any charactercontroller and convert it into a capsulecollider with a rigidbody
            CharacterController ch = target.GetComponentInChildren <CharacterController>();
            if (ch != null)
            {
                if (ch.transform.GetComponent <Rigidbody>() == null)
                {
                    CapsuleCollider ca = ch.gameObject.AddComponent <CapsuleCollider>();
                    ca.radius = ch.radius;
                    ca.height = ch.height;
                    ca.center = ca.center;
                    Rigidbody r = ch.gameObject.AddComponent <Rigidbody>();
                    r.useGravity             = false;
                    r.isKinematic            = true;
                    r.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
                }
            }

            // convert 1st person controller
            vp_FPController fpController = target.GetComponent <vp_FPController>();
            if (fpController != null)
            {
                vp_CapsuleController cController = target.AddComponent <vp_CapsuleController>();
                vp_EditorUtility.CopyValuesFromDerivedComponent(fpController, cController, true, false, null);                  // TEST: fpcontroller is not derived from capsulecontroller! see note in 'GenerateStatesAndPresetsFromDerivedComponent'
                if (m_CopyStates && !string.IsNullOrEmpty(m_StatePath))
                {
                    vp_EditorUtility.GenerateStatesAndPresetsFromDerivedComponent(fpController, cController, m_StatePath);
                }
            }

            // convert weapon handler
            vp_FPWeaponHandler fpWHandler = target.GetComponent <vp_FPWeaponHandler>();
            if (fpWHandler != null)
            {
                vp_WeaponHandler wHandler = target.AddComponent <vp_WeaponHandler>();
                vp_EditorUtility.CopyValuesFromDerivedComponent(fpWHandler, wHandler, true, false, null);
            }

            // convert damage handler
            vp_FPPlayerDamageHandler fpDHandler = target.GetComponent <vp_FPPlayerDamageHandler>();
            if (fpDHandler != null)
            {
                vp_PlayerDamageHandler dHandler = target.AddComponent <vp_PlayerDamageHandler>();
                vp_EditorUtility.CopyValuesFromDerivedComponent(fpDHandler, dHandler, true, true, null);
            }

            // convert event handler
            vp_FPPlayerEventHandler fpEHandler = target.GetComponent <vp_FPPlayerEventHandler>();
            if (fpEHandler != null)
            {
                vp_PlayerEventHandler eHandler = target.AddComponent <vp_PlayerEventHandler>();
                vp_EditorUtility.CopyValuesFromDerivedComponent(fpEHandler, eHandler, true, false, null);
            }

            // convert body animator
            vp_FPBodyAnimator fpBAnimator = target.GetComponentInChildren <vp_FPBodyAnimator>();
            if (fpBAnimator != null)
            {
                vp_BodyAnimator bAnimator = fpBAnimator.gameObject.AddComponent <vp_BodyAnimator>();
                vp_EditorUtility.CopyValuesFromDerivedComponent(fpBAnimator, bAnimator, true, true, null);
            }

            // delete 'remoteplayer-illegal' components
            DeleteComponentsOfType(target.transform, typeof(vp_FPCamera));                      // these first due to dependencies
            DeleteComponentsOfType(target.transform, typeof(vp_FPController));                  // these first due to dependencies
            DeleteComponentsOfTypes(target.transform, new System.Type[]
            {
                typeof(Camera),
                typeof(AudioListener),
                typeof(vp_FPInput),
                typeof(vp_SimpleCrosshair),
                typeof(vp_FootstepManager),
                typeof(vp_FPInteractManager),
                typeof(vp_SimpleHUD),
                typeof(vp_PainHUD),
                typeof(vp_FPEarthquake),
                typeof(CharacterController),
                typeof(vp_FPWeaponHandler),
                typeof(vp_FPPlayerDamageHandler),
                typeof(vp_FPPlayerEventHandler),
                typeof(vp_FPBodyAnimator)
            });

            Transform weaponCamera = vp_Utility.GetTransformByNameInChildren(target.transform, "WeaponCamera", true);
            if (weaponCamera != null)
            {
                GameObject.DestroyImmediate(weaponCamera.gameObject);
            }

            target.name = name;
        }
        catch (System.Exception e)
        {
            Debug.Log(e);
            target.name = target.name + " (CONVERSION FAILED - see error log)";
        }
    }