/// <summary> /// Given a list of individual item weights and a target weight value, return the index that corresponds /// to teh accumulated item weights that meet target weight. /// </summary> /// <param name="itemWeights"></param> /// <param name="targetWeight"></param> /// <returns></returns> public int GetIndexFromWeights(List <int> itemWeights, int targetWeight) { var weightCounter = 0; for (int i = 0; i < itemWeights.Count; i++) { var itemWeight = itemWeights[i]; if (itemWeight != 0) { weightCounter += itemWeight; //Debug.Log(weightCounter + ", " + weight); if (weightCounter >= targetWeight) { { return(i); } } } } // should never happen as we should always get a match! We warn about case where all are 0 earlier, warn on others here. if (weightCounter != 0) { MyDebug.LogWarningF("No match for weight {0}. Please report this error as this should never happen!", targetWeight); MyDebug.LogWarningF(ToString()); } return(0); }
/// <summary> /// Gets a text translation from a LocalisationEntry /// </summary> /// <param name="key"></param> public string GetText(string key, string language) { var languageIndex = GetLanguageIndex(language); if (languageIndex == -1) { MyDebug.LogWarningF("Localisation key {0} not found for language {1}.", key, language); return(null); } return(GetText(key, languageIndex)); }
/// <summary> /// Gets a text translation from a LocalisationEntry /// </summary> /// <param name="key"></param> public string GetText(string key, int languageIndex) { Assert.IsTrue(languageIndex >= 0 && languageIndex < Languages.Count, string.Format("language index {0} is out of bounds when getting key '{1}'", languageIndex, key)); var entry = GetEntry(key); if (entry == null) { MyDebug.LogWarningF("Localisation key {0} not found for language {1} (index {2}).", key, Languages[languageIndex].Name, languageIndex); return(null); } return(entry.Languages[languageIndex]); }
/// <summary> /// Localise the specified value based on the currently set language. /// </summary> /// If language is specific then this method will try and get the key for that particular value, returning null if not found. public static string Get(string key, string language = null, bool missingReturnsNull = false) { // Ensure we have a Language to work with LoadDictionary(); if (!IsLocalisationLoaded) { return(null); } // try and get the key string[] vals; if (_localisations.TryGetValue(key, out vals)) { if (language == null) { if (_languageIndex < vals.Length) { return(vals[_languageIndex]); } } else { // get value for a specific language var index = Array.IndexOf(Languages, language); if (index == -1) { return(null); } if (index < vals.Length) { return(vals[index]); } else { return(null); } } } if (missingReturnsNull) { return(null); } MyDebug.LogWarningF("Localisation key not found: '{0}' for Language {1}", key, Language); return(key); }
public void PlayEffect(AudioClip clip, float pitchLow = 1, float pitchHigh = 1) { Assert.IsNotNull(EffectAudioSources, "Ensure that you have added AudioSources if you are playying effects."); Assert.AreNotEqual(0, EffectAudioSources.Length, "Ensure that you have added AudioSources if you are playying effects."); var newPitch = UnityEngine.Random.Range(pitchLow, pitchHigh); // try and find a free or similar audio source //AudioSource similarAudioSource = null; foreach (var audioSource in EffectAudioSources) { if (audioSource.isPlaying == false) { audioSource.clip = clip; audioSource.pitch = newPitch; audioSource.Play(); return; } //if (Mathf.Approximately(audioSource.pitch, pitchHigh)) //{ // similarAudioSource = audioSource; //} } // no free so play one shot if we have a similar match. //if (similarAudioSource != null) //{ // MyDebug.LogWarningF("Not enough free effect AudioSources for playing {0}, ({1}). Using a similar one - consider adding more AudioSources to the GameManager gameobject for performance.", clip.name, newPitch); // similarAudioSource.PlayOneShot(clip); // return; //} // otherwise we create and add a new one MyDebug.LogWarningF("Not enough free effect AudioSources for playing {0}, ({1}). Adding a new one - consider adding more AudioSources to the GameManager gameobject for performance.", clip.name, newPitch); var newAudioSource = gameObject.AddComponent <AudioSource>(); newAudioSource.playOnAwake = false; newAudioSource.volume = EffectAudioVolume; newAudioSource.pitch = newPitch; newAudioSource.clip = clip; newAudioSource.Play(); EffectAudioSources = EffectAudioSources.Concat(Enumerable.Repeat(newAudioSource, 1)).ToArray(); }
/// <summary> /// Sync all items and distances so for each distance every item has a corresponding weight. We need this /// to be able to easily look up items for a given distance. /// </summary> public void PrepareForUse() { // sort distances _distancesWithWeights = _distancesWithWeights.OrderBy(t => t.Distance).ToList(); // fill distances for all items for (var i = 0; i < _items.Count; i++) { var item = _items[i]; // process for all distances. int lastWeight = 0; for (var j = 0; j < _distancesWithWeights.Count; j++) { var distanceWithWeights = _distancesWithWeights[j]; // it this item has a matching distance setting then get matching weight from item, if not then use last weight. int itemWeight; var distanceWeightValue = item.DistanceWeights.FirstOrDefault(t => t.Distance == distanceWithWeights.Distance); itemWeight = distanceWeightValue != null ? distanceWeightValue.Weight : lastWeight; // add to end of item weights list (this will maintain the same ordering as in _items for later) distanceWithWeights.ItemWeights.Add(itemWeight); distanceWithWeights.Total += itemWeight; lastWeight = itemWeight; } } #if UNITY_EDITOR // sanity check for (var j = 0; j < _distancesWithWeights.Count; j++) { var distanceWithWeights = _distancesWithWeights[j]; if (distanceWithWeights.Total == 0) { MyDebug.LogWarningF("All Totals are 0 for distance {0}. This might give unexpected results.", distanceWithWeights.Distance); } } #endif isPreparedForUse = true; }
/// <summary> /// Start the level automatically is set to do so. /// </summary> void Start() { #if UNITY_EDITOR // some sanity checking and warnings if (GameOverWhenTargetTimeReached && Mathf.Approximately(Level.TimeTarget, 0)) { MyDebug.LogWarningF("You have enabled the option 'GameOverWhenTargetTimeReached' in LevelManager however the current level has a configured TimeTarget of 0 and so will end immediately. Consider using Level resource configuration files and setting this value."); } if (GameWonWhenTargetScoreReached && Mathf.Approximately(Level.ScoreTarget, 0)) { MyDebug.LogWarningF("You have enabled the option 'GameWonWhenTargetScoreReached' in LevelManager however the current level has a configured ScoreTarget of 0 and so will end immediately. Consider using Level resource configuration files and setting this value."); } if (GameWonWhenTargetCoinsReached && Mathf.Approximately(Level.CoinTarget, 0)) { MyDebug.LogWarningF("You have enabled the option 'GameWonWhenTargetCoinsReached' in LevelManager however the current level has a configured CoinTarget of 0 and so will end immediately. Consider using Level resource configuration files and setting this value."); } #endif if (AutoStart) { StartLevel(); } }