Commit b10df488e0c8c5790463212b88ebd47f28f48275

Authored by Mateus Lustosa
1 parent b175a846
Exists in ui

Remake

Assets/Scripts/Player Manager/AnimationReference.cs
1 1 using UnityEngine;
2 2  
3   -public class AnimationReference
4   -{
  3 +public class AnimationReference {
  4 +
  5 + private GenericPlayerManager context = GenericPlayerManager.context;
  6 +
5 7 public string name;
6 8 public string subtitle;
7   - public AnimationState state;
8 9 public short type;
9   - public bool playing;
  10 + public float speed;
  11 +
  12 + public AnimationState state = null;
10 13  
11   - public AnimationReference(string name, string subtitle, AnimationState state, short type)
  14 + public AnimationReference(string name, string subtitle, short type, float speed)
12 15 {
13 16 this.name = name;
14 17 this.subtitle = subtitle;
15   - this.state = state;
16 18 this.type = type;
17   - this.playing = false;
  19 + this.speed = speed;
  20 + }
  21 +
  22 + public AnimationReference(string name, string subtitle, short type)
  23 + : this(name, subtitle, type, 0F) {
  24 + this.speed = context.getSpeedByType(Subtitle.TYPE_WORD);
18 25 }
  26 +
  27 + public void play()
  28 + {
  29 + this.state = context.COMPONENT_ANIMATION.CrossFadeQueued(this.name, context.fadeLength, QueueMode.CompleteOthers);
  30 + this.state.speed = this.speed;
  31 + }
  32 +
19 33 }
20 34 \ No newline at end of file
... ...
Assets/Scripts/Player Manager/GenericPlayerManager.cs
... ... @@ -10,9 +10,11 @@ using UnityEngine.UI;
10 10  
11 11 public abstract class GenericPlayerManager : MonoBehaviour {
12 12  
  13 + public static GenericPlayerManager context;
  14 +
13 15 private const string DEFAULT_ANIMATION = "_default";
14 16  
15   - protected float fadeLength = 0.6F;
  17 + public float fadeLength = 0.6F;
16 18 public string gloss = "";
17 19  
18 20 // Vetor usado para quebrar a glosa
... ... @@ -21,17 +23,15 @@ public abstract class GenericPlayerManager : MonoBehaviour {
21 23 // Referencia para o avatar
22 24 private GameObject AVATAR;
23 25 // Referencia para o componente animador do avatar
24   - private Animation COMPONENT_ANIMATION;
  26 + public Animation COMPONENT_ANIMATION;
25 27 public Text SUBTITLES;
26 28  
27 29 // Guarda os nomes das palavras já carregadas
28   - private HashSet<string> loadedAssetBundles = new HashSet<string>();
  30 + public HashSet<string> loadedAssetBundles = new HashSet<string>();
29 31 // Guarda os nomes das palavras que não tem assetbundle
30   - private HashSet<string> nonexistentAssetBundles = new HashSet<string>();
  32 + public HashSet<string> nonexistentAssetBundles = new HashSet<string>();
31 33  
32   - // Fila de animações para reprodução
33   - // Utilizada para alterar velocidade e apresentar a legenda
34   - private Queue<AnimationReference> animQueue = new Queue<AnimationReference>();
  34 + private Queue<Gloss> glossQueue = new Queue<Gloss>();
35 35  
36 36 // Sinais de intervalo de animações: não sinaliza reprodução na UI
37 37 private HashSet<string> intervalAnimations = new HashSet<string>();
... ... @@ -56,6 +56,8 @@ public abstract class GenericPlayerManager : MonoBehaviour {
56 56  
57 57 public virtual void Start()
58 58 {
  59 + context = this;
  60 +
59 61 // Configuração de velocidade das animações
60 62 subtitles = new Subtitle(SUBTITLES);
61 63 subtitles.DefaultWordSpeed = new DefaultSignSpeed();
... ... @@ -113,7 +115,7 @@ public abstract class GenericPlayerManager : MonoBehaviour {
113 115 }
114 116 }
115 117  
116   - private float getSpeedByType(short type)
  118 + public float getSpeedByType(short type)
117 119 {
118 120 switch (type)
119 121 {
... ... @@ -128,7 +130,10 @@ public abstract class GenericPlayerManager : MonoBehaviour {
128 130  
129 131 public void stopAll()
130 132 {
131   - StopCoroutine("loadAndPlay");
  133 + foreach (Gloss gloss in this.glossQueue) gloss.destroy();
  134 + gloss.Clear();
  135 +
  136 + //StopCoroutine("loadAndPlay");
132 137 this.randomAnimations.unlockFor("loadAndPlay");
133 138 loading = false;
134 139  
... ... @@ -160,36 +165,6 @@ public abstract class GenericPlayerManager : MonoBehaviour {
160 165 play(this.gloss, true, true, true);
161 166 }
162 167  
163   - /*
164   - * Manda reproduzir animação e adiciona a file de animações a serem reproduzidas.
165   - */
166   - private AnimationState playAnimation(short type, string name, string subtitle, float speed)
167   - {
168   - try
169   - {
170   - AnimationState state = COMPONENT_ANIMATION.CrossFadeQueued(name, fadeLength, QueueMode.CompleteOthers);
171   - state.speed = speed;
172   -
173   - lock (this.animQueue) {
174   - animQueue.Enqueue(new AnimationReference(name, subtitle, state, type));
175   - }
176   -
177   - return state;
178   - }
179   - catch (NullReferenceException nre)
180   - {
181   - UnityEngine.Debug.Log("'" + name + "' não foi encontrado!\n" + nre.ToString());
182   - }
183   -
184   - return null;
185   - }
186   - private AnimationState playAnimation(short type, string name, string subtitle) {
187   - return playAnimation(type, name, subtitle, getSpeedByType(type));
188   - }
189   - private AnimationState playAnimation(short type, string name) {
190   - return playAnimation(type, name, name);
191   - }
192   -
193 168  
194 169 /**
195 170 * Returns the asset bundle named aniName.
... ... @@ -197,7 +172,7 @@ public abstract class GenericPlayerManager : MonoBehaviour {
197 172 * @return AssetBundle - se for encontrado.
198 173 * null - se ocorrer num erro.
199 174 */
200   - protected abstract WWW loadAssetBundle(string aniName);
  175 + public abstract WWW loadAssetBundle(string aniName);
201 176  
202 177  
203 178 /**
... ... @@ -270,70 +245,30 @@ public abstract class GenericPlayerManager : MonoBehaviour {
270 245 }
271 246 }
272 247  
273   - StartCoroutine("loadAndPlay", gloss);
  248 + StartCoroutine("loadAndPlay", gloss)
274 249 return true;
275 250 }
276 251  
277   - /**
278   - * Spells word.
279   - *
280   - * @return last animation's subtitle.
281   - */
282   - private string spellWord(string word)
283   - {
284   - string lastAnimationSubtitle = "";
285   - bool defaultPlayed = false;
286   -
287   - // A reprodução da primeira letra deve ser longa para não ser cortada no fade
288   - this.subtitles.updateLetterSpeed();
  252 + private System.Object loadingLocker = new System.Object();
289 253  
290   - for (int i = 0; i < word.Length; i++)
  254 + private IEnumerator loadAndPlay(string glossText)
  255 + {
  256 + lock (this.loadingLocker)
291 257 {
292   - char second = word[i];
293   - lastAnimationSubtitle = Subtitle.highlight(word, i);
294   -
295   - // Se for uma letra
296   - if (second >= 65 && second <= 90)
297   - playAnimation(Subtitle.TYPE_LETTER, second.ToString(), lastAnimationSubtitle, subtitles.LetterSpeed);
  258 + Gloss gloss = new Gloss(glossText);
  259 + this.glossQueue.Add(gloss);
298 260  
299   - // Se for um número
300   - else if (second >= 48 && second <= 57)
301   - playAnimation(Subtitle.TYPE_NUMBER, second.ToString(), lastAnimationSubtitle, subtitles.NumberSpeed);
302   -
303   - // Se for uma vírgula
304   - else if (second == 44)
305   - playAnimation(Subtitle.TYPE_WORD, second.ToString(), lastAnimationSubtitle);
  261 + if ( ! gloss.loaded)
  262 + yield return gloss.load();
306 263  
307   - // Não há animação
308   - else
309   - {
310   - // Reproduz animação default apenas uma vez
311   - if ( ! defaultPlayed)
312   - {
313   - defaultPlayed = true;
314   - playAnimation(Subtitle.TYPE_NONE, DEFAULT_ANIMATION, lastAnimationSubtitle);
  264 + yield return gloss.handle();
  265 +
315 266  
316   - // A reprodução da próxima letra deve ser longa para não ser cortada no fade
317   - subtitles.updateLetterSpeed();
318   - }
319 267  
320   - UnityEngine.Debug.Log("Animação \"" + second + "\" inexistente.");
321   - continue;
322   - }
323 268  
324   - defaultPlayed = false;
325   - subtitles.updateLetterSpeed();
326   - }
327 269  
328   - return lastAnimationSubtitle;
329   - }
330 270  
331   - private System.Object loadingLocker = new System.Object();
332 271  
333   - private IEnumerator loadAndPlay(string gloss)
334   - {
335   - lock (this.loadingLocker)
336   - {
337 272 this.randomAnimations.lockFor("loadAndPlay");
338 273 this.loading = true;
339 274 // onPlayingStateChange();
... ...
Assets/Scripts/Player Manager/Gloss.cs 0 → 100644
... ... @@ -0,0 +1,181 @@
  1 +using UnityEngine;
  2 +using System.Collections;
  3 +using System.Collections.Generic;
  4 +using System;
  5 +using System.Threading;
  6 +
  7 +public class Gloss {
  8 +
  9 + private GenericPlayerManager context = GenericPlayerManager.context;
  10 +
  11 + private string gloss;
  12 + private string[] tokens;
  13 +
  14 + private Queue<AnimationReference> animations = new Queue<AnimationReference>();
  15 +
  16 + public bool loaded = false;
  17 +
  18 + public Gloss(string gloss)
  19 + {
  20 + this.gloss = gloss;
  21 + this.tokens = gloss.Split(' ');
  22 + }
  23 +
  24 + private void addDefault() {
  25 + this.animations.Add(new AnimationReference(context.DEFAULT_ANIMATION, "", Subtitle.TYPE_NONE, 2F));
  26 + }
  27 +
  28 + public IEnumerator load()
  29 + {
  30 + loaded = true;
  31 + addDefault();
  32 +
  33 + bool spelled = false;
  34 +
  35 + foreach (string token in this.tokens)
  36 + {
  37 + if (String.IsNullOrEmpty(token)
  38 + || context.nonexistentAssetBundles.Contains(token)
  39 + || context.loadedAssetBundles.Contains(token))
  40 + continue;
  41 +
  42 + bool success = false;
  43 + string error = null;
  44 +
  45 + WWW www = context.loadAssetBundle(token);
  46 + if (www != null)
  47 + {
  48 + yield return www;
  49 +
  50 + if (www.error == null)
  51 + {
  52 + AssetBundle bundle = www.assetBundle;
  53 +
  54 + if (bundle != null && ! String.IsNullOrEmpty(bundle.mainAsset.name))
  55 + {
  56 + AnimationClip clip = bundle.mainAsset as AnimationClip;
  57 + bundle.Unload(false);
  58 +
  59 + if (clip)
  60 + {
  61 + success = true;
  62 + context.COMPONENT_ANIMATION.AddClip(clip, token);
  63 + context.loadedAssetBundles.Add(token);
  64 +
  65 + if (spelled)
  66 + {
  67 + addDefault();
  68 + spelled = false;
  69 + }
  70 +
  71 + string subtitle = context.flags.Contains(token) ? token : "";
  72 +
  73 + this.animations.Add(new AnimationReference(token, subtitle, Subtitle.TYPE_WORD));
  74 + }
  75 + else error = "Error at Gloss.load(): clip from AssetBundle of " + token + " wasn't found";
  76 + }
  77 + else error = "Error at Gloss.load(): could not get AssetBundle from WWW";
  78 + }
  79 + else error = "Error at Gloss.load(): WWW: " + www.error;
  80 + }
  81 + else error = "Error at Gloss.load(): WWW is null";
  82 +
  83 + if ( ! success)
  84 + {
  85 + Debug.Log(error);
  86 + context.nonexistentAssetBundles.Add(token);
  87 +
  88 + if (this.flags.Contains(aniName))
  89 + {
  90 + addDefault();
  91 + continue;
  92 + }
  93 +
  94 + // Se já houve o soletramento de alguma palavra, reproduz animação default
  95 + if (spelled)
  96 + addDefault();
  97 + else
  98 + spelled = true;
  99 +
  100 + spellWord(token);
  101 + }
  102 + }
  103 +
  104 + addDefault();
  105 + }
  106 +
  107 + private void spellWord(string word)
  108 + {
  109 + // A reprodução da primeira letra deve ser longa para não ser cortada no fade
  110 + context.subtitles.updateLetterSpeed();
  111 +
  112 + char last = ' ';
  113 +
  114 + for (int i = 0; i < word.Length; i++)
  115 + {
  116 + char letter = word[i];
  117 + string subtitle = Subtitle.highlight(word, i);
  118 +
  119 + string animationName = letter.ToString();
  120 + if (letter == last) animationName += "_2";
  121 +
  122 + last = letter;
  123 +
  124 + // Se for uma letra
  125 + if (letter >= 65 && letter <= 90)
  126 + this.animations.Add(new AnimationReference(
  127 + animationName,
  128 + subtitle,
  129 + Subtitle.TYPE_LETTER
  130 + ));
  131 +
  132 + // Se for um número
  133 + else if (letter >= 48 && letter <= 57)
  134 + this.animations.Add(new AnimationReference(
  135 + animationName,
  136 + subtitle,
  137 + Subtitle.TYPE_NUMBER
  138 + ));
  139 +
  140 + // Se for uma vírgula
  141 + else if (letter == 44)
  142 + this.animations.Add(new AnimationReference(
  143 + animationName,
  144 + subtitle,
  145 + Subtitle.TYPE_WORD
  146 + ));
  147 +
  148 + context.subtitles.updateLetterSpeed();
  149 + }
  150 + }
  151 +
  152 + private IEnumerator play()
  153 + {
  154 + foreach (AnimationReference animation in this.animations)
  155 + animation.play();
  156 +
  157 + while (this.animations.Count > 0)
  158 + {
  159 + AnimationReference reference = this.animations.Peek();
  160 +
  161 + this.subtitles.setText(reference.subtitle);
  162 +
  163 + while (COMPONENT_ANIMATION.IsPlaying(reference.name))
  164 + yield return null;
  165 +
  166 + if (reference.state == null)
  167 + this.animations.Dequeue();
  168 + else
  169 + yield return null;
  170 + }
  171 +
  172 + this.subtitles.setText("");
  173 + }
  174 +
  175 + public void destroy()
  176 + {
  177 + context.StopCoroutine("load");
  178 + context.StopCoroutine("handle");
  179 + }
  180 +
  181 +}
... ...
Assets/Scripts/Player Manager/Gloss.cs.meta 0 → 100644
... ... @@ -0,0 +1,12 @@
  1 +fileFormatVersion: 2
  2 +guid: ff84f8e2340949a419032a7d7f7bc160
  3 +timeCreated: 1456775969
  4 +licenseType: Pro
  5 +MonoImporter:
  6 + serializedVersion: 2
  7 + defaultReferences: []
  8 + executionOrder: 0
  9 + icon: {instanceID: 0}
  10 + userData:
  11 + assetBundleName:
  12 + assetBundleVariant:
... ...
Assets/Scripts/Player Manager/Subtitle.cs
... ... @@ -119,28 +119,16 @@ public class Subtitle {
119 119 public static string highlight(string word, int index)
120 120 {
121 121 string subtitle = "";
122   - int last = 0;
123 122  
124   - if (index == 0)
125   - subtitle += "<b><color=white>" + word[0] + "</color></b>";
126   - else
127   - subtitle += word[0];
128   -
129   - for (int i = 1; i < word.Length; i++)
  123 + for (int i = 0; i < word.Length; i++)
130 124 {
131   - if ((word[i] >= 65 && word[i] <= 90) || (word[i] >= 48 && word[i] <= 57))
  125 + if (i > 0 && ((word[i] >= 65 && word[i] <= 90) || (word[i] >= 48 && word[i] <= 57)))
132 126 subtitle += "-";
133 127  
134   - if (i == index || (last == index && word[i] == word[last]))
135   - {
  128 + if (i == index)
136 129 subtitle += "<b><color=white>" + word[i] + "</color></b>";
137   - if (i == index) last = i;
138   - }
139 130 else
140   - {
141 131 subtitle += word[i];
142   - last = i;
143   - }
144 132 }
145 133  
146 134 return subtitle;
... ...
Assets/Scripts/PlayerManager.cs
... ... @@ -98,7 +98,7 @@ public class PlayerManager : GenericPlayerManager {
98 98  
99 99 public Text debugText;
100 100  
101   - protected override WWW loadAssetBundle(string aniName) {
  101 + public override WWW loadAssetBundle(string aniName) {
102 102 return WWW.LoadFromCacheOrDownload(BASE_URL + aniName, version);
103 103 }
104 104  
... ...