/// <summary> /// Checks whether or not the given settings are valid and sanitizes the user's input. /// </summary> /// <param name="newSettings">The settings to sanitize</param> private MortalSettings SanitizeSettings(MortalSettings newSettings) { if (newSettings.randomizeHealth) { if (newSettings.minHealthCap <= 0) { Debug.LogWarning("Minimum Health Cap must be greater than 0"); newSettings.minHealthCap = 1; } if (newSettings.maxHealthCap < newSettings.minHealthCap) { Debug.LogWarning("Maximum Health Cap must be greater than or equal to " + newSettings.minHealthCap); newSettings.maxHealthCap = newSettings.minHealthCap; } if (newSettings.minStartingHealth <= 0 || newSettings.minStartingHealth > newSettings.maxHealthCap) { Debug.LogWarning("Minimum Starting Health must be greater than 0 and less than or equal to " + newSettings.maxHealthCap); newSettings.minStartingHealth = Mathf.Clamp(newSettings.minStartingHealth, 1, newSettings.maxHealthCap); } if (newSettings.maxStartingHealth < newSettings.minStartingHealth || newSettings.maxStartingHealth > newSettings.maxHealthCap) { Debug.LogWarning("Maximum Starting Health must be greater than or equal to " + newSettings.minStartingHealth + " and less than or equal to " + newSettings.maxHealthCap); newSettings.maxStartingHealth = Mathf.Clamp(newSettings.maxStartingHealth, newSettings.minStartingHealth, newSettings.maxHealthCap); } } else { if (newSettings.maxHealth <= 0) { Debug.LogWarning("Maximum health must be greater than 0"); newSettings.maxHealth = 1; } if (newSettings.startingHealth <= 0 || newSettings.startingHealth > newSettings.maxHealth) { Debug.LogWarning("Starting health must be greater than 0 and less than or equal to " + newSettings.maxHealth); newSettings.startingHealth = Mathf.Clamp(newSettings.startingHealth, 1, newSettings.maxHealth); } } return(newSettings); }
/// <summary> /// Applies the given settings to the Mortal /// </summary> /// <param name="newSettings">The settings to apply</param> public void Initialize(MortalSettings newSettings) { // Clean the user's horrible input SanitizeSettings(newSettings); // Compute the maximum and current health from the sanitized settings value if (newSettings.randomizeHealth) { _maxHealth = Random.Range(newSettings.minHealthCap, newSettings.maxHealthCap + 1); _currentHealth = Mathf.Clamp(Random.Range(newSettings.minStartingHealth, newSettings.maxStartingHealth), 1, _maxHealth); } else { _maxHealth = newSettings.maxHealth; _currentHealth = newSettings.startingHealth; } _dead = false; // Let listeners know that the settings have been applied if (OnInitialized != null) { OnInitialized(this); } }
/// <summary> /// Checks whether or not the public settings are valid and sanitizes the user's input. /// </summary> private void SanitizeSettings() { settings = SanitizeSettings(settings); }