示例#1
0
    /// <summary>
    /// Spawns a spawn object by index
    /// </summary>
    public virtual void Spawn(int index)
    {
        // make sure index in range
        if (index > AISpawnerObjects.Count - 1)
        {
            return;
        }

        // check object exists
        AISpawnerObject spawnerObject = AISpawnerObjects[index];

        if (spawnerObject == null)
        {
            return;
        }

        // check if any AI are cached
        if (spawnerObject.CachedEventHandlers.Count == 0)
        {
            return;
        }

        // set each ai to active if applicable
        foreach (vp_AIEventHandler ai in spawnerObject.CachedEventHandlers)
        {
            if (ai.Dead.Active)
            {
                continue;
            }

            vp_Utility.Activate(ai.gameObject);
            ai.Respawn.Send();
        }
    }
示例#2
0
    /// <summary>
    /// uses Reflection to check if a property is
    /// applicable and should be overriden
    /// before adding it to the overrides dictionary
    /// </summary>
    protected virtual void AddSpawnProperty(AISpawnerObject property, FieldInfo info, Dictionary <string, object> dict)
    {
        if (info.GetValue(property) == null)
        {
            return;
        }

        dict.Add(info.Name, info.GetValue(property));
    }
示例#3
0
    /// <summary>
    /// Spawns a spawn object by name
    /// </summary>
    public virtual void SpawnByName(string n)
    {
        AISpawnerObject spawnerObject = AISpawnerObjects.FirstOrDefault(ai => ai.SpawnObjectName == n);

        if (spawnerObject == null)
        {
            return;
        }

        Spawn(AISpawnerObjects.IndexOf(spawnerObject));
    }
示例#4
0
        /// <summary>
        /// Used to duplicate this instance.
        /// </summary>
        public virtual AISpawnerObject Copy()
        {
            Dictionary <string, object> dict = new Dictionary <string, object>();

            foreach (FieldInfo info in this.GetType().GetFields())
            {
                dict.Add(info.Name, info.GetValue(this));
            }

            AISpawnerObject newProperties = new AISpawnerObject();

            foreach (string k in dict.Keys)
            {
                FieldInfo p = newProperties.GetType().GetField(k);
                if (p != null)
                {
                    if (p.Name.IndexOf("Foldout") != -1 && p.Name != "Foldout")
                    {
                        p.SetValue(newProperties, false);
                    }
                    else if (p.Name == "Foldout")
                    {
                        p.SetValue(newProperties, true);
                    }
                    else if (p.Name == "SpawnObjectName")
                    {
                        p.SetValue(newProperties, dict[k] + " Copy");
                    }
                    else
                    {
                        p.SetValue(newProperties, dict[k]);
                    }
                }
            }

            Foldout = false;

            return(newProperties);
        }
示例#5
0
    /// <summary>
    /// Instantiates an spawn object by it's index
    /// and sets the new AI's properties
    /// </summary>
    protected virtual void InstantiateObject(int index)
    {
        if (index > AISpawnerObjects.Count)
        {
            return;
        }

        AISpawnerObject spawnerObject = AISpawnerObjects[index];

        if (spawnerObject == null)
        {
            return;
        }

        if (spawnerObject.Prefab == null || !spawnerObject.Enabled)
        {
            return;
        }

        for (int i = 0; i < spawnerObject.AmountToSpawn; i++)
        {
            GameObject go = Object.Instantiate(spawnerObject.Prefab) as GameObject;

            vp_AIEventHandler ai = go.GetComponent <vp_AIEventHandler>();
            if (ai != null)
            {
Reroll:

                // get a random position within the spawners bounds
                Vector3 min = m_Collider.bounds.min;
                Vector3 max     = m_Collider.bounds.max;
                Vector3 randPos = new Vector3(Random.Range(min.x, max.x), max.y, Random.Range(min.z, max.z));

                // check to see if this is a good position to spawn, if not, reroll
                if (Physics.CheckSphere(randPos, spawnerObject.DamageHandlerRespawnCheckRadius, vp_Layer.Mask.PhysicsBlockers))
                {
                    goto Reroll;
                }

                // random rotation
                Quaternion randRot = Random.rotation;
                randRot.z = 0;
                randRot.x = 0;

                go.transform.position = randPos;
                go.transform.rotation = randRot;

                // set all the overridden properties to send to the various components
                Dictionary <string, object> dict = new Dictionary <string, object>();
                foreach (FieldInfo info in spawnerObject.GetType().GetFields())
                {
                    AddSpawnProperty(spawnerObject, info, dict);
                }

                // send the spawner bounds with the other overrides
                dict.Add("AreaSpawnerBounds", m_Collider.bounds);

                // send the overridden properties to the various components
                ai.SetProperties.Send(dict);

                spawnerObject.CachedEventHandlers.Add(ai);

                if (!SpawnAtStart || (SpawnAtStart && !spawnerObject.SpawnAtStart))
                {
                    Renderer[] renderers = go.GetComponentsInChildren <Renderer>();
                    foreach (Renderer r in renderers)
                    {
                        r.enabled = false;
                    }

                    // the AI needs to be allowed to setup some stuff before being set to inactive
                    vp_Timer.In(.1f, delegate {
                        vp_Utility.Activate(go, false);
                        foreach (Renderer r in renderers)
                        {
                            r.enabled = true;
                        }
                    });
                }

                ai.Reset.Send(Vector3.zero);
            }
        }
    }