void InitPlayField() { mOdd = false; for (int r = 0; r < ROWS; r++) { List <Egg> eggs = new List <Egg>(); float offset = (mOdd) ? OFFSET_EGG : 0; for (int c = 0; c < COLUMNS; c++) { GameObject obj = Instantiate(mEggPrefab, transform.position, Quaternion.identity); obj.transform.parent = gameObject.transform; Egg egg = obj.GetComponent <Egg>(); egg.SetPosition(c, r, offset); int color = Random.Range(0, Defines.EggCount); egg.SetColor(color); egg.gameObject.SetActive(false); egg.SetHazard(false); eggs.Add(egg); } mGird.Add(eggs); mOdd = !mOdd; } mOdd = true; for (int i = 0; i < NUM_ROWS_SHOW; ++i) { SpawnLine(); } SetState(STATE.PLAYGAME); }
// Handle adding egg to grid when hit, if eggOnGrid is null, it is hit at the top public void AttachEggOnGrid(GameObject eggOnGrid, GameObject refBullet) { float minDist = 99999.0f; Egg newEgg = null; if (eggOnGrid == null) // Hit on top case { for (int i = 0; i < GetMaxCol(); i++) { Egg egg = GetEggInGrid(0, i); float currentDist = Vector3.Distance(egg.gameObject.transform.position, refBullet.transform.position); if (currentDist < minDist) { minDist = currentDist; newEgg = egg; } } } else // normal case { newEgg = GetNewEggPosition(eggOnGrid, refBullet); } int fallenCount = 0; int crackedCount = 0; switch (refBullet.GetComponent <Bullet>().GetColor()) { case (int)Defines.COLOR.SPECIAL_FLASH: if (eggOnGrid != null) { crackedCount = PopEggsFlashEgg(eggOnGrid.GetComponent <Egg>()); mEggAnimation.GetComponent <EggAnimationHandler>().AddFlashBlast(eggOnGrid.transform.position); } else { crackedCount = PopEggsFlashEgg(newEgg); mEggAnimation.GetComponent <EggAnimationHandler>().AddFlashBlast(newEgg.transform.position); } fallenCount = FallEggs(); //Debug.Log(crackedCount + " and " + fallenCount); AddTotalScoreByCount(crackedCount, fallenCount); AudioManager.Instance().PlaySFXFlashEgg(); break; case (int)Defines.COLOR.SPECIAL_DARKGREY: if (eggOnGrid != null) { crackedCount = PopEggsPowerEgg(eggOnGrid.GetComponent <Egg>().GetColor()); mEggAnimation.GetComponent <EggAnimationHandler>().AddPowerBlast(eggOnGrid.transform.position); AudioManager.Instance().PlaySFXPowerEgg(); } else { Egg nearbyEgg = GetNearbyEgg(newEgg); if (nearbyEgg != null) { crackedCount = PopEggsPowerEgg(nearbyEgg.GetColor()); mEggAnimation.GetComponent <EggAnimationHandler>().AddPowerBlast(nearbyEgg.transform.position); AudioManager.Instance().PlaySFXPowerEgg(); } } fallenCount = FallEggs(); //Debug.Log(crackedCount + " and " + fallenCount); AddTotalScoreByCount(crackedCount, fallenCount); break; case (int)Defines.COLOR.SPECIAL_RAINBOW: //Debug.Log("OKKK --- "+bulletEgg.GetComponent<Bullet>().GetColor()); if (eggOnGrid != null) { newEgg.SetColor(eggOnGrid.GetComponent <Egg>().GetColor()); } else { int currentEgg = UnityEngine.Random.Range(0, Defines.EggCount); newEgg.SetColor(currentEgg); } newEgg.gameObject.SetActive(true); List <Egg> crackedEggRainbow = SpreadToAllNearbyEgg(newEgg); crackedCount = crackedEggRainbow.Count; fallenCount = 0; if (crackedCount >= 3) { PopEggs(crackedEggRainbow, true); fallenCount = FallEggs(); AddTotalScoreByCount(crackedCount, fallenCount); currentCombo++; uiAnimationObject.GetComponent <UIAnimationHandler>().ShowComboTextTimed(currentCombo, null, null); } else { if (currentCombo > 0) { uiAnimationObject.GetComponent <UIAnimationHandler>().ShowComboTextTimed(0, null, null); } currentCombo = 0; } AudioManager.Instance().PlaySFXRainbowEgg(); break; default: //Debug.Log("OKKK --- "+bulletEgg.GetComponent<Bullet>().GetColor()); newEgg.SetColor(refBullet.GetComponent <Bullet>().GetColor()); newEgg.gameObject.SetActive(true); List <Egg> crackedEgg = SpreadToAllNearbyEgg(newEgg); crackedCount = crackedEgg.Count; fallenCount = 0; if (crackedCount >= 3) { PopEggs(crackedEgg); fallenCount = FallEggs(); AddTotalScoreByCount(crackedCount, fallenCount); AudioManager.Instance().PlaySFXEggBreak(); currentCombo++; uiAnimationObject.GetComponent <UIAnimationHandler>().ShowComboTextTimed(currentCombo, null, null); } else { AudioManager.Instance().PlaySFXEggHit(); if (currentCombo > 0) { uiAnimationObject.GetComponent <UIAnimationHandler>().ShowComboTextTimed(0, null, null); } currentCombo = 0; } break; } }
void SpawnLine() { for (int r = ROWS - 2; r >= 0; r--) { foreach (Egg e in mGird[r]) { mGird[r + 1][e.GetCol()].SetColor(e.GetColor()); mGird[r + 1][e.GetCol()].SetHazard(e.IsHazard()); mGird[r + 1][e.GetCol()].SetPosition(e.GetCol(), r + 1, e.GetOffset()); mGird[r + 1][e.GetCol()].gameObject.SetActive(e.gameObject.activeSelf); mGird[r + 1][e.GetCol()].UpdatePositionAnim(-Egg.EGG_RATIO, e.GetOffset()); } } float offset = mOdd ? OFFSET_EGG : 0; for (int i = 0; i < COLUMNS; ++i) { Egg e = mGird[0][i]; int color; int left = i == 0 ? 0 : mOdd ? i : i - 1; int right = i == (COLUMNS - 1) ? (COLUMNS - 1) : mOdd ? i + 1 : i; bool haveLeftEgg = mGird[1][left].isActiveAndEnabled; bool haveRightEgg = mGird[1][right].isActiveAndEnabled; if (Random.Range(0, 100) < WEIGHTED_RANDOM_CHANCE && (haveLeftEgg || haveRightEgg)) // 80% { if (haveLeftEgg && haveRightEgg) { if (Random.Range(0, 2) == 1) // 50% { color = mGird[1][left].GetColor(); } else { color = mGird[1][right].GetColor(); } } else if (haveLeftEgg) { color = mGird[1][left].GetColor(); } else { color = mGird[1][right].GetColor(); } } else { color = Random.Range(0, Defines.EggCount); } e.SetColor(color); e.SetPosition(e.GetCol(), 0, offset); e.gameObject.SetActive(true); e.UpdatePositionAnim(-Egg.EGG_RATIO, e.GetOffset()); e.SetHazard(false); if (Random.Range(0, 100) < HAZARD_CHANCE && currentHazardCount < MAX_HAZARD_ON_SCREEN) { switch (color) { case 2: case 4: e.SetHazard(true); currentHazardCount++; break; } } } mOdd = !mOdd; animateEggDown = true; eggDownAnimTime = 0.0f; }