void Update() { if (timer > timeBetweenSpawn || timer >= 0.2) { // Spawn GameObject go = new GameObject(); go.transform.position = spawnPoints[Mathf.RoundToInt(Random.Range(0, spawnPoints.Length))].transform.position; FruitBehaviour fruit = go.AddComponent <FruitBehaviour>(); fruit.fruits = fruits; fruit.slicedFruitsSprites = slicedFruitsSprites; fruit.layer = layer; timer = 0; // Play a random sound effect fruit.audioSource = source; fruit.cutFruit = sounds[Mathf.RoundToInt(Random.Range(0, sounds.Length))]; } // Make the span time faster as the game goes on timeBetweenSpawn -= Time.deltaTime / 10; timer += Time.deltaTime; }
private FruitBehaviour getFruit() { FruitBehaviour fb = fruit.Find(x => !x.isActive); if (fb == null) { int randomNmbr = Random.Range(1, 11); if (randomNmbr < 10) // 90% chance to spawn "ordinary fruit" { int fruitIndex = Random.Range(0, 4); fb = Instantiate(fruitPrefab[fruitIndex]).GetComponent <FruitBehaviour>(); fruit.Add(fb); } else { fb = Instantiate(fruitPrefab[4]).GetComponent <FruitBehaviour>(); fruit.Add(fb); } } return(fb); }
// Update is called once per frame private void Update() { if (isPaused) { return; } if (Time.time - lastSpawn > deltaSpawn) { int amountOfFruit = Random.Range(0, 10); if (amountOfFruit > 3) { FruitBehaviour fb = getFruit(); float randomX = Random.Range(-1.65f, 1.65f); fb.LauchFruit(Random.Range(1.85f, 2.75f), randomX, -randomX); lastSpawn = Time.time; } else if (amountOfFruit == 2 || amountOfFruit == 1) { FruitBehaviour fb1 = getFruit(); FruitBehaviour fb2 = getFruit(); float randomX1 = Random.Range(-1.65f, 1.65f); float randomX2 = Random.Range(-1.65f, 1.65f); fb1.LauchFruit(Random.Range(1.85f, 2.75f), randomX1, -randomX1); fb2.LauchFruit(Random.Range(1.85f, 2.75f), randomX2, -randomX2); lastSpawn = Time.time; } else if (amountOfFruit == 0) { FruitBehaviour fb1 = getFruit(); FruitBehaviour fb2 = getFruit(); FruitBehaviour fb3 = getFruit(); float randomX1 = Random.Range(-1.65f, 1.65f); float randomX2 = Random.Range(-1.65f, 1.65f); float randomX3 = Random.Range(-1.65f, 1.65f); fb1.LauchFruit(Random.Range(1.85f, 2.75f), randomX1, -randomX1); fb2.LauchFruit(Random.Range(1.85f, 2.75f), randomX2, -randomX2); fb3.LauchFruit(Random.Range(1.85f, 2.75f), randomX3, -randomX3); lastSpawn = Time.time; } } if (Input.GetMouseButton(0)) { Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition); pos.z = -1; trail.position = pos; //trail.gameObject.GetComponent<TrailRenderer>().time = 0.1f; trail.gameObject.SetActive(true); Collider2D[] thisFruitFrame = Physics2D.OverlapPointAll(new Vector2(pos.x, pos.y), LayerMask.GetMask("Fruit")); if ((Input.mousePosition - lastMousePosition).sqrMagnitude >= REQUIRED_SLICE_FORCE) { foreach (Collider2D c2 in thisFruitFrame) { c2.GetComponent <FruitBehaviour>().Slice(); GameObject victim = c2.GetComponent <FruitBehaviour>().gameObject; if ((victim.name == "TomatoFruit(Clone)") || victim.name == "CarrotFruit(Clone)") { Instantiate(fruitSplashEffectRed, victim.transform); Vector3 invertedPos = new Vector3(pos.y, pos.x, pos.z); GameObject[] pieces = BLINDED_AM_ME.MeshCut.Cut(victim, victim.transform.position, invertedPos, capMaterialRed); if (!pieces[1].GetComponent <Rigidbody>()) { pieces[1].AddComponent <Rigidbody>(); } Destroy(pieces[1], 1); victim.GetComponent <BoxCollider2D>().enabled = false; // IS THIS NECESSARY?? } else if ((victim.name == "BananaFruit(Clone)") || victim.name == "PineappleFruit(Clone)") { Instantiate(fruitSplashEffectYellow, victim.transform); Vector3 invertedPos = new Vector3(pos.y, pos.x, pos.z); GameObject[] pieces = BLINDED_AM_ME.MeshCut.Cut(victim, victim.transform.position, invertedPos, capMaterialYellow); if (!pieces[1].GetComponent <Rigidbody>()) { pieces[1].AddComponent <Rigidbody>(); } Destroy(pieces[1], 1); victim.GetComponent <BoxCollider2D>().enabled = false; // IS THIS NECESSARY?? } else //Fruit sliced is a bomb, make player lose HP { LoseLifePoint(); Camera.main.GetComponent <ShakeEffect>().ShakeCamera(0.7f, 0.15f); Destroy(victim.gameObject); score = score - 5; scoreText.text = score.ToString(); } } } lastMousePosition = Input.mousePosition; fruitCols = thisFruitFrame; } else { Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition); pos.z = -1; trail.position = pos; //trail.gameObject.GetComponent<TrailRenderer>().time = 0; trail.gameObject.SetActive(false); } }