public void GeneratePhrase(object data) { TemplateCluster cluster = data as TemplateCluster; generatedPhrase = cluster.Populate(_seed, true); IsDone = true; }
/// <summary> /// Generates a phrase from a template synchroniously. Use of this method is discouraged as it will cause the game to wait for it to finish and, depending on the length of the template, /// this can cause a noticable pause. Instead, use GeneratePhraseAsync if possible. /// </summary> /// <param name="template">The string value of the phrase template the new phrase will be generated from.</param> /// <param name="seed">(Optional) A seed to use in generation. If no seed is passed, a new one will be generated based on system time.</param> /// <returns>The newly generated phrase</returns> public string GeneratePhraseFromTemplateString(string template, int?seed = null) { TemplateCluster cluster = TemplateParser.ParseTemplate(template); int _seed = seed.HasValue ? seed.Value : System.Environment.TickCount; string phrase = cluster.Populate(_seed, true); return(phrase); }
/// <summary> /// Generates a phrase on a separate thread and sends the generated phrase and the seed used to generate it to a callback once complete. /// </summary> /// <param name="text">A TextAsset containing the template phrase for the newly generated phrase.</param> /// <param name="callback">A callback to which the string phrase and int seed will be passed.</param> /// <param name="seed">(Optional) The seed that will be used to generate the phrase. /// <para>Use this if you are wanting to recieve a phrase generated previously.</para> /// <para>If no seed is passed, a new one will be created and that int is what will be passed to the callback. Otherwise, the seed passed in is the one that will go to the callback.</para></param> /// <returns>The Id of the PhraseGenerationJob (thread) generating the phrase, can be passed to CancelGeneration to abort the thread later.</returns> public int GeneratePhraseAsyncFromTemplateString(string template, System.Action <string, int> callback, int?seed = null) { PhraseGenerationJob generationJob = new PhraseGenerationJob(); TemplateCluster cluster = TemplateParser.ParseTemplate(template); jobs.Add(generationJob); int _seed = seed.HasValue ? seed.Value : System.Environment.TickCount; generationJob.Start(_seed, cluster, callback); return(generationJob.Id); }
public void Start(int seed, TemplateCluster cluster, System.Action <string, int> callback) { _seed = seed; _isDone = false; _handle = new object(); _thread = null; _isCanceled = false; this.callback = callback; _thread = new System.Threading.Thread(GeneratePhrase); _thread.Start(cluster); }
public static TemplateCluster ParseTemplate(string template) { TemplateCluster cluster = new TemplateCluster(template); TemplatePhrase currentPhrase = new TemplatePhrase(); int startIndex = 0; bool indexOnSeparator = true; for (int i = 0; i <= template.Length; i++) { if (i == template.Length || separators.Contains(template[i]) || phraseSeparators.Contains(template[i])) { if (!indexOnSeparator) { currentPhrase.AddWord(new TemplateWord(template, startIndex, i - startIndex)); if (i == template.Length || (phraseSeparators.Contains(template[i]) && currentPhrase.WordCount() > 0)) { cluster.AddPhrase(currentPhrase); currentPhrase = new TemplatePhrase(); } } startIndex = i; indexOnSeparator = true; } else { if (indexOnSeparator) { indexOnSeparator = false; startIndex = i; } } } return(cluster); }