// Add more hearts when we get 'em private void OnHealthAdded(float amount) { int numOfHearts = (int)(amount / 100.0f); int startIndex = heartList.Count; int endIndex = startIndex + numOfHearts; for (int x = startIndex; x < endIndex; x++) { // Create a new heart and added it to our row GameObject newHeartObj = Instantiate(heartPrefab.gameObject, heartsRow.transform); HeartUI newHeartUI = newHeartObj.GetComponent <HeartUI>(); // Cache this new heart in our list heartList.Add(newHeartUI); } currentHeartIndex = heartList.Count - 1; // Update our crystal states HeartUI currentHeart = heartList[currentHeartIndex]; currentHeart.FullGem.SetActive(true); currentHeart.ChippedGem.SetActive(false); currentHeart.ShatteredGem.SetActive(false); currentHeart.GhostGem.SetActive(false); }
// Update our current heart's current state private void OnHealthChanged(float amountChange, bool dmg = false) { // Don't update health if we have none if (currentHeartIndex < 0) { return; } HeartUI currentHeart = heartList[currentHeartIndex]; // Figure out previous health and see if we crossed over state transitions float currentHealth = playerHealthComp.GetCurrentHealth - (currentHeartIndex * 100.0f); // TODO: Weird conversion? float negateAmount = (dmg) ? 1.0f : -1.0f; // Reversed so we can know the old amount; positive for damage, negative for healing float oldHealth = currentHealth + negateAmount * amountChange; currentHealth = currentHealth / 100.0f; oldHealth = oldHealth / 100.0f; if (negateAmount < 0) { return; } // 0 health transition if (oldHealth > 0.0f && currentHealth <= 0.0f) { currentHeart.FullGem.SetActive(false); currentHeart.ChippedGem.SetActive(false); currentHeart.ShatteredGem.SetActive(false); currentHeart.GhostGem.SetActive(true); // Shatter effect currentHeart.ChipEffect.Play(); currentHeartIndex--; } // 1/3rd Damaged Gem transition else if (oldHealth > 1.0f / 3.0f && currentHealth <= 1.0f / 3.0f) { currentHeart.FullGem.SetActive(false); currentHeart.ChippedGem.SetActive(false); currentHeart.ShatteredGem.SetActive(true); currentHeart.GhostGem.SetActive(false); // Shatter effect currentHeart.ChipEffect.Play(); } // 2/3rd Damaged Gem transition else if (oldHealth > 3.0f / 4.0f && currentHealth <= 3.0f / 4.0f) { currentHeart.FullGem.SetActive(false); currentHeart.ChippedGem.SetActive(true); currentHeart.ShatteredGem.SetActive(false); currentHeart.GhostGem.SetActive(false); // Shatter effect currentHeart.ChipEffect.Play(); } }