/// <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); }
/// <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); } } }