private void OnCollisionEnter(Collision collision) { if (collision.gameObject.layer == LayerMask.NameToLayer("Interactable")) { FloatingObjectScript floatingObjectScript = collision.gameObject.GetComponent <FloatingObjectScript>(); if (floatingObjectScript != null) { floatingObjectScript.DisableAndMarkForReuse(); if (StatManager.timeInLevel <= 3.5f) { return; } if (floatingObjectScript.damage > 0 && StatManager.timeInLevel <= 6 && LevelManager.CurrentLevel == 0) { return; } if (floatingObjectScript.hitSound != null) { collisionAudio.clip = floatingObjectScript.hitSound; collisionAudio.Play(); } StatManager.obstaclesHitInLevel++; trash += floatingObjectScript.score; StatManager.trashCollectedInLevel += floatingObjectScript.score; if (shieldActive) { return; } if (floatingObjectScript.damage > 0) { health -= floatingObjectScript.damage; StatManager.healthLost += floatingObjectScript.damage; StatManager.healthLostInLevel += floatingObjectScript.damage; } if (trash >= trashCapacity) { SceneManager.LoadScene("Assets/Scenes/UI/ScoreScreen.unity"); } trashScoreBoard.text = trash + "/" + trashCapacity; } } }
private List <FloatingObjectScript> SpawnObjects(float simulatedDeltaTime = -1f, bool spawnHazards = true) { if (spawnables == null) { UpdateLevelType(); } if (spawnables == null) { return(null); } float deltaTime; if (simulatedDeltaTime <= 0) { deltaTime = Time.fixedDeltaTime; } else { deltaTime = simulatedDeltaTime; } List <FloatingObjectScript> spawnedObjects = new List <FloatingObjectScript>(); for (int i = 0; i < spawnables.Length; i++) { SpawnInfo spawnInfo = spawnables[i]; if (spawnInfo.interactableType == null) { continue; } if (spawnInfo.interactableType != null && spawnInfo.interactableType.damage > 0) { if (!spawnHazards) { continue; } else { spawnables[i].spawnRate *= 1f + (0.01f * hazardSpawnRateIncrease); spawnables[i].spawnTime = 1f / spawnables[i].spawnRate; } } float spawnChance = deltaTime / spawnInfo.spawnTime; if (UnityEngine.Random.value <= spawnChance) { FloatingObjectScript newFloatingObject = null; int interactableID = spawnInfo.interactableType.ID; if (reUsableFloatingObjects.ContainsKey(interactableID) && reUsableFloatingObjects[interactableID].Count > 0) { newFloatingObject = reUsableFloatingObjects[interactableID].Dequeue(); newFloatingObject.gameObject.SetActive(true); newFloatingObject.GetComponent <Collider>().isTrigger = false; } else { GameObject newObject = Instantiate(spawnInfo.interactableType.model, transform); newFloatingObject = newObject.GetComponent <FloatingObjectScript>(); if (newFloatingObject == null) { newFloatingObject = newObject.AddComponent <FloatingObjectScript>(); } newFloatingObject.master = this; newFloatingObject.damage = spawnInfo.interactableType.damage; newFloatingObject.score = spawnInfo.interactableType.score; newFloatingObject.buoyancy = spawnInfo.interactableType.buoyancy; newFloatingObject.hitSound = spawnInfo.interactableType.hitSound; newFloatingObject.boat = boat; newFloatingObject.ocean = ocean; newFloatingObject.gameObject.layer = LayerMask.NameToLayer("Interactable"); } newFloatingObject.transform.position = transform.position + (UnityEngine.Random.Range(-10f, 10f) * Vector3.right); spawnedObjects.Add(newFloatingObject); } } return(spawnedObjects); }