public Actor Spawn(Type type, object outer, SpawnParameters spawnArgs) { if (!typeof(Actor).IsAssignableFrom(type)) { throw new Exception("Can only spawn actors!"); } int classID = SerializableObject.StaticClassIDSlow(type); return(InternalSpawn(null, type, classID, outer, spawnArgs)); }
SpawnParameters CalculateSpawnParameters(SpawnerData data) { var parameters = new SpawnParameters(); parameters.position = new Vector3(transform.position.x + Random.Range(-volumeSize.x / 2, volumeSize.x / 2), transform.position.y + Random.Range(-volumeSize.y / 2, volumeSize.y / 2), transform.position.z + Random.Range(-volumeSize.z / 2, volumeSize.z / 2)); parameters.rotation = Quaternion.Euler(Random.Range(0, 360), Random.Range(0, 360), Random.Range(0, 360)); parameters.scale = new Vector3(data.CalculateScale(), data.CalculateScale(), data.CalculateScale()); return(parameters); }
public Actor Spawn(ActorSpawnTag tag, object outer, SpawnParameters spawnArgs) { if (tag.type == null) { throw new Exception("ActorSpawnTag cannot load type: " + tag.typeName); } else { int classID = SerializableObject.StaticClassIDSlow(tag.type); return(InternalSpawn(tag, tag.type, classID, outer, spawnArgs)); } }
public void Suspend(GameObject species, GameObject creature, bool fadeOut = false) { SpawnParameters sp = species.GetComponent <SpawnParameters>(); if (sp.isSuspending) { return; } else { sp.isSuspending = true; StartCoroutine(SuspendCoRoutine(species, creature, fadeOut)); } }
System.Collections.IEnumerator Spawn() { float delay = 1.0f / (float)spawnRate; WorldGenerator wg = FindObjectOfType <WorldGenerator>(); int nextSpecies = 0; while (true) { // Remove too far creatures for (int i = alive.Count - 1; i >= 0; i--) { GameObject creature = alive[i]; SpawnParameters sp = creature.GetComponent <SpawnParameters>(); GameObject species = sp.Species; Vector3 boidPos = GetCreaturePosition(creature); Vector3 camPos = player.transform.position; float dist = Vector3.Distance(boidPos, camPos); bool behind; //bool behind = (Vector3.Dot(boidPos - camPos, player.transform.forward) < 0) && (dist > 500); behind = Vector3.Angle(boidPos - camPos, player.transform.forward) > 80 && (dist > 2000); if (sp.Species == PlayerController.Instance.species) { continue; } //Debug.Log(i + "\t" + dist); if (behind) { Suspend(species, creature); } } if (alive.Count < maxcreatures) { // Find a spawn point // Calculate the position GetSpecies(nextSpecies, prefabs[nextSpecies].GetComponent <SpawnParameters>().singleton); nextSpecies = (nextSpecies + 1) % prefabs.Length; } yield return(new WaitForSeconds(delay)); } }
Actor InternalSpawn(ActorSpawnTag tag, Type type, int classID, object outer, SpawnParameters spawnArgs) { var actor = _objectFactory.NewObject(classID) as Actor; if ((actor == null) || !type.IsAssignableFrom(actor.GetType())) { throw new ObjectStaticClassMismatchException(type.FullName + " has a bad static class id!"); } if (tag != null) { actor.SetReplicates(tag.replicates); actor.SetSpawnTagID((ushort)tag.spawnID); } actor.SetWorld(this); if (actor.replicates && (this is Server.ServerWorld)) { actor.SetNetID(GetNextNetID()); actor.SetRemoteRole(ERemoteRole.Authority); _replicatedActors.Add(actor.netIDHashCode, actor); _replicatedObjects.Add(actor.netIDHashCode, actor); } _actors.Add(actor); if (spawnArgs.preConstruct != null) { spawnArgs.preConstruct(actor); } actor.PreConstruct(outer); actor.Construct(); actor.PostConstruct(); if (_didLevelStart) { actor.OnLevelStart(); } return(actor); }
/// <summary> /// Spawns targets if enough time has elapsed and spawns only a certain percentage /// of the spawners. /// </summary> /// <param name="timeRange">The range in seconds to have the spawner fire.</param> /// <param name="pctRange">The percentage range you want indices chosen. .1f would be 10% of the spawners.</param> private void SpawnTargets(SpawnParameters parameters) { //Increase difficulty independent of spawn rate of targets. float stepTime = (Time.time - difficultyTimer); if (stepTime > parameters.difficultyTimeStep) { parameters.timeRangeBtmLmt *= parameters.difficultyPctModifier; parameters.timeRangeTopLmt *= parameters.difficultyPctModifier; difficultyTimer = Time.time; } float powTime = (Time.time - powerupTime); if (powTime > UnityEngine.Random.Range(parameters.powerupBtmTimeRng, parameters.powerupTopTimeRng)) { powerupTime = Time.time; int spawner = UnityEngine.Random.Range(0, spawners.Length); spawners[spawner].Spawn(powerup.gameObject); return;//short circuit for now to handle only spawning the target. } float antipowTime = (Time.time - antiPowerupTime); if (antipowTime > UnityEngine.Random.Range(parameters.antiPowerupBtmTimeRng, parameters.antiPowerupTopTimeRng)) { antiPowerupTime = Time.time; int spawner = UnityEngine.Random.Range(0, spawners.Length); spawners[spawner].Spawn(antiPowerup.gameObject); return;//short circuit for now to handle only spawning the target. } //Spawn targets within the range specified for the given difficulty. float time = (Time.time - startTime); if (time > UnityEngine.Random.Range(parameters.timeRangeBtmLmt, parameters.timeRangeTopLmt)) { startTime = Time.time; int numberOfSpawnersToFire = Mathf.RoundToInt(spawners.Length * UnityEngine.Random.Range(parameters.spawnPctBtmRange, parameters.spawnPctTopRange));// Take total amount of spawners and multiply them by a percentage. int[] selectedIndices = new int[numberOfSpawnersToFire]; initializeArray(ref selectedIndices); for (int i = 0; i < numberOfSpawnersToFire; i++) { int indice = UnityEngine.Random.Range(0, spawners.Length); while (selectedIndices.Contains(indice)) { indice = UnityEngine.Random.Range(0, spawners.Length); } selectedIndices[i] = indice; } for (int i = 0; i < selectedIndices.Length; i++) { spawners[selectedIndices[i]].Spawn(true, spawnVariation); } } }
bool FindPlace(GameObject creature, out Vector3 newPos) { bool found = false; int count = 0; newPos = Vector3.zero; while (!found) { SpawnParameters sp = creature.GetComponent <SpawnParameters>(); if (sp == null) { Debug.Log("Creature : " + creature + " doesnt have spawn parameters!!!"); found = false; break; } float start = Mathf.Min(sp.start, genesisSpawnDistance); //Vector3 r = Vector3.forward; // Random.insideUnitSphere; //r.z = spawnInFront ? Mathf.Abs(r.z) : r.z; //r.y = 0; Vector3 r = Vector3.forward; r *= Random.Range(start, sp.end); r += (r.normalized * start); r = Quaternion.AngleAxis(Random.Range(-fov, fov), Vector3.up) * r; newPos = player.transform.TransformPoint(r); float sampleY = WorldGenerator.Instance.SamplePos(newPos.x, newPos.z); float worldMax = WorldGenerator.Instance.surfaceHeight - sp.minDistanceFromSurface; float minHeight = sampleY + sp.minHeight; int segments = 3; if (sp.radiusRequired != 0) { float[] heights = new float[segments + 1]; heights[0] = sampleY; float sum = sampleY; float thetaInc = (Mathf.PI * 2.0f) / segments; for (int i = 0; i < segments; i++) { float theta = i * thetaInc; Vector3 p = new Vector3 (Mathf.Sin(theta) * sp.radiusRequired , 0 , Mathf.Cos(theta) * sp.radiusRequired ); // Translate by newPos p += newPos; heights[i + 1] = WorldGenerator.Instance.SamplePos(p.x, p.z); } float stdDev = Utilities.StdDev(heights); if (stdDev > 2) { count++; continue; } } if (minHeight > worldMax) { count++; continue; } if (count == 10) { found = false; break; } float maxHeight = Mathf.Min(sampleY + sp.maxHeight, worldMax); newPos.y = Mathf.Min(Random.Range(minHeight, maxHeight), worldMax); found = true; } return(found); }
public GameObject GetSpecies(int speciesIndex, bool useExisting) { Vector3 newPos = Vector3.zero; GameObject newCreature = null; SpawnParameters sp = prefabs[speciesIndex].GetComponent <SpawnParameters>(); if (useExisting && aliveMap.ContainsKey(prefabs[speciesIndex])) { GameObject creature = aliveMap[prefabs[speciesIndex]]; if (!creature.GetComponent <SpawnParameters>().isSuspending) { return(prefabs[speciesIndex]); } } if (suspended.ContainsKey(prefabs[speciesIndex])) { newCreature = suspended.Get(prefabs[speciesIndex]); if (FindPlace(newCreature, out newPos)) { suspended.Remove(prefabs[speciesIndex], newCreature); Teleport(newCreature, newPos); newCreature.SetActive(true); if (newCreature.GetComponent <LifeColours>()) { newCreature.GetComponent <LifeColours>().FadeIn(); } if (newCreature.GetComponentInChildren <CreatureController>()) { newCreature.GetComponentInChildren <CreatureController>().Restart(); } // Change the school size every time we teleport a school SchoolGenerator sg = newCreature.GetComponentInChildren <SchoolGenerator>(); if (sg != null) { sg.transform.position = newPos; sg.targetCreatureCount = Random.Range(sg.minBoidCount, sg.maxBoidCount); } alive.Add(newCreature); aliveMap[prefabs[speciesIndex]] = newCreature; /*GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); * cube.transform.position = newPos; * cube.transform.localScale = Vector3.one * 5; */ } } else { //Debug.Log("Instiantiating a new: " + prefabs[nextCreature]); if (FindPlace(prefabs[speciesIndex], out newPos)) { newCreature = GameObject.Instantiate <GameObject>(prefabs[speciesIndex], newPos , prefabs[speciesIndex].transform.rotation * Quaternion.AngleAxis(Random.Range(0, 360), Vector3.up) ); newCreature.GetComponent <SpawnParameters>().Species = prefabs[speciesIndex]; if (school != null) { Boid b = newCreature.GetComponent <Boid>(); b.school = school; school.boids.Add(b); } if (newCreature.GetComponentInChildren <CreatureController>()) { newCreature.GetComponentInChildren <CreatureController>().mother = this; } newCreature.transform.parent = this.transform; newCreature.transform.position = newPos; newCreature.SetActive(true); alive.Add(newCreature); aliveMap[prefabs[speciesIndex]] = newCreature; } else { Debug.Log("Couldnt find a place for the new creature"); } } return(prefabs[speciesIndex]); }
public T Spawn <T>(object outer, SpawnParameters spawnArgs) where T : Actor, new() { int classID = SerializableObject.StaticClassID <T>(); return((T)InternalSpawn(null, typeof(T), classID, outer, spawnArgs)); }