/// <summary> /// Instantiate a new procedural object /// </summary> /// <param name="po"></param> private void _Instantiate(ProceduralObjectController po) { var spawnPosition = _GetSpawnPosition(po); var rotate = UnityEngine.Random.Range(0, 2); var rotation = Quaternion.identity; // if (rotate == 1) // { // Debug.Log("inverso"); // rotation = Quaternion.Inverse(rotation); // } else { // Debug.Log("idenbtity"); // } var newPo = Instantiate <ProceduralObjectController>(po, spawnPosition, rotation); newPo.gameObject.name = po.gameObject.name; newPo.Depth = Depth; _objectsPool.Add(newPo); _built.Enqueue(newPo); // instantiate proceduralObject colliders if (newPo.colliders.Length > 0) { foreach (var colliderInfo in newPo.colliders) { var colliderPosition = new Vector3(spawnPosition.x, spawnPosition.y + colliderInfo.ySpawnOffset, spawnPosition.z + colliderInfo.zSpawnOffset); var colliderInstance = Instantiate <Collider>(colliderInfo.collider, colliderPosition, Quaternion.identity); colliderInstance.transform.parent = newPo.transform; } } _UpdateSpawnPosition(newPo); }
/// <summary> /// Lazy adds 3 roads for the early game /// </summary> /// <returns></returns> private IEnumerator _Start() { ProceduralObjectController insertedRoad = null; for (int i = 1; i <= 3; i++) { Depth++; // lazy inserts a road string roadName = null; if (insertedRoad != null) { roadName = insertedRoad.gameObject.name; } insertedRoad = InsertNewObject(roadName); yield return(null); // lazy builds level elements over the road just created if (i == 1) { foreach (var obj in _levelController.BuildStartLevelElementsForDepth(Depth)) { yield return(null); } } else { foreach (var obj in _levelController.BuildLevelForDepth(Depth)) { yield return(null); } } } }
/// <summary> /// Show an inactivated instance /// </summary> /// <param name="po"></param> private void _ShowInstance(ProceduralObjectController po) { po.gameObject.transform.position = _GetSpawnPosition(po); po.Depth = Depth; po.gameObject.SetActive(true); _built.Enqueue(po); _UpdateSpawnPosition(po); }
/// <summary> /// inserts a new object right after the last one /// </summary> /// <param name="objectName">name of the object desired. Leave null if wants to get a random one</param> /// <returns></returns> public ProceduralObjectController InsertNewObject(string objectName = null) { var poIdx = 0; ProceduralObjectController po = null; if (string.IsNullOrEmpty(objectName)) { poIdx = Random.Range(0, proceduralObjects.Length); po = proceduralObjects[poIdx]; } else { po = proceduralObjects .Where(x => x.gameObject.name.Equals(objectName)) .SingleOrDefault(); if (po == null) { throw new System.Exception("parametro objectName com valor inválido"); } } var availableInPool = _objectsPool .Where(x => x.gameObject.name.Equals(po.gameObject.name) && !x.gameObject.activeSelf); var poAvailable = availableInPool.FirstOrDefault(); if (poAvailable != null) { _ShowInstance(poAvailable); } else { _Instantiate(po); } return(po); }
/// <summary> /// Spawn position considering yOffset /// </summary> /// <param name="po"></param> /// <returns></returns> private Vector3 _GetSpawnPosition(ProceduralObjectController po) { return(new Vector3(_CurrentSpawnPosition.x, _CurrentSpawnPosition.y + po.ySpawnOffset, _CurrentSpawnPosition.z)); }
/// <summary> /// Update the position where next object has to be spawned /// </summary> /// <param name="lastObjectShown"></param> private void _UpdateSpawnPosition(ProceduralObjectController lastObjectShown) { _CurrentSpawnPosition = new Vector3(_CurrentSpawnPosition.x, _CurrentSpawnPosition.y, _CurrentSpawnPosition.z + lastObjectShown.dimension.z); }