GenericPlayerManager.cs 14.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
//Log Dir         http://docs.unity3d.com/Manual/LogFiles.html
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using UnityEngine.UI;

public abstract class GenericPlayerManager : MonoBehaviour {

	private class AnimationReference
	{
		public string name;
		public string subtitle;
		public AnimationState state;
		public short type;
		public bool playing;

		public AnimationReference(string name, string subtitle, AnimationState state, short type)
		{
			this.name = name;
			this.subtitle = subtitle;
			this.state = state;
			this.type = type;
			this.playing = false;
		}
	}

	protected class DefaultSignSpeed
	{
		public static float DEFAULT = 1.1F;
		public static float DEFAULT_MAX = 2F;

		private float speed;
		private float max;
		// Relação entre a velocidade do tipo representado e a velocidade DEFAULT.
		private float unit;

		public DefaultSignSpeed()
		{
			this.speed = DEFAULT;
			this.max = DEFAULT_MAX;
			this.unit = 1F;
		}

		public DefaultSignSpeed(float defaultSpeed, float defaultMaxSpeed)
		{
			this.speed = defaultSpeed;
			this.max = defaultMaxSpeed;
			this.unit = (this.max - this.speed) / (DEFAULT_MAX - DEFAULT);
		}

		public float Speed {
			get { return this.speed; }
			set {
				this.speed = value;
				this.unit = calculateUnit();
			}
		}
		public float Max {
			get { return this.max; }
			set {
				this.speed = value;
				this.unit = calculateUnit();
			}
		}
		public float Unit {
			get { return this.unit; }
		}

		private float calculateUnit()
		{
			return (this.max - this.speed) / (DEFAULT_MAX - DEFAULT);
		}

		/*
		 *	Retorna velocidade em relação ao estado do slider.
		 *	@param slider - estado do slider (valor entre DefaultSignSpeed.DEFAULT e DefaultSignSpeed.DEFAULT_MAX)
		 */
		public float getProporcionalSpeed(float slider)
		{
			return this.speed + (slider - DEFAULT) * this.unit;
		}
	}

	private const string DEFAULT_ANIMATION = "_default";
	private const string NONE_ANIMATION = "_defaultWORD";

	protected float fadeLength = 0.6F;

	protected DefaultSignSpeed defaultWordSpeed = new DefaultSignSpeed();
	protected DefaultSignSpeed defaultFirstLetterSpeed = new DefaultSignSpeed(2.1F, 2.8F);
	protected DefaultSignSpeed defaultLetterSpeed = new DefaultSignSpeed(3F, 4.3F);
	protected DefaultSignSpeed defaultNumberSpeed = new DefaultSignSpeed(1.5F, 2.9F);

	private const short TYPE_NONE = -1;
	private const short TYPE_WORD = 0;
	private const short TYPE_LETTER = 1;
	private const short TYPE_NUMBER = 2;

	private float hSlidersecond = DefaultSignSpeed.DEFAULT;
	private float wordSpeed = DefaultSignSpeed.DEFAULT;
	private float letterSpeed = DefaultSignSpeed.DEFAULT;
	private float numberSpeed = DefaultSignSpeed.DEFAULT;

	protected string glosa = "";
	private static String[] stringPos = { DEFAULT_ANIMATION };//vetor que sera usado para quebrar a glosa

	private GameObject AVATAR;
	private Animation COMPONENT_ANIMATION;
	private BoxCollider AVATAR_COLLIDER;
	public Text SUBTITLES;

	// Guarda os nomes das palavras ja carregadas.
	private HashSet<string> loadedAssetBundles = new HashSet<string>();
	// Guarda os nomes das palavras que nao tem assetbundle.
	private HashSet<string> nonexistentAssetBundles = new HashSet<string>();

	// Lista de animações sendo reproduzidas.
	// Utilizada para alterar velocidade e apresentar a legenda.
	private volatile Queue<AnimationReference> animQueue = new Queue<AnimationReference>();

	private volatile bool loadingSingleAnimation = false;
	private volatile bool loading = false;
	private volatile bool playing = false;
	private volatile bool paused = false;

	public virtual void Start()
	{
		wordSpeed = defaultWordSpeed.Speed;
		letterSpeed = defaultLetterSpeed.Speed;
		numberSpeed = defaultNumberSpeed.Speed;

		AVATAR = GameObject.FindGameObjectWithTag("avatar");//referencia para o avatar
		COMPONENT_ANIMATION = AVATAR.GetComponent<Animation>();//referencia para o componente animador do avatar
		AVATAR_COLLIDER = GameObject.FindGameObjectWithTag("avatar").GetComponent<BoxCollider>();
	}

	public bool isLoadingSingleAnimation() { return loadingSingleAnimation; }
	public bool isLoading() { return loading; }
	public bool isPlaying() { return playing; }
	public bool isPaused() { return paused; }

	public void SetAvatarCollider(bool isActive)
	{
		AVATAR_COLLIDER.enabled = isActive;
	}

	protected virtual void setSubtitle(string text)
	{
		SUBTITLES.text = text;
	}

	// Define a velocidade das animacões com base no slider da GUI
	public void setSlider(float x)
	{
		hSlidersecond = x;

		wordSpeed = defaultWordSpeed.getProporcionalSpeed(x);
		letterSpeed = defaultLetterSpeed.getProporcionalSpeed(x);
		numberSpeed = defaultNumberSpeed.getProporcionalSpeed(x);

		if ( ! paused)
			foreach (AnimationReference reference in animQueue)
				if (reference.type != TYPE_NONE && reference.state != null)
					reference.state.speed = getSpeedByType(reference.type);
	}

	private float getSpeedByType(short type)
	{
		switch (type)
		{
			case TYPE_WORD: 	return wordSpeed;
			case TYPE_LETTER:	return letterSpeed;
			case TYPE_NUMBER:	return numberSpeed;
		}
		
		return 2F;
	}


	public void stop_animations()
	{
		StopCoroutine("loadAndPlay");
		loading = false;

		playing = false;
		paused = false;
		onPlayingStateChange();

		stopAnimations();
	}

	public void stopAnimations()
	{
		try {
			StopCoroutine("handleStates");
		} catch (NullReferenceException nre) { Debug.Log("StopCoroutine handlestates nullreff::"+nre.ToString()); }

		setSubtitle("");

		try {
			animQueue.Clear();
		} catch (NullReferenceException nre) { Debug.Log("SetQueueList null reff::"+nre.ToString()); }

		COMPONENT_ANIMATION.Stop();
		COMPONENT_ANIMATION.CrossFade(DEFAULT_ANIMATION, fadeLength, PlayMode.StopAll);
	}

	/*
	 *	Manda reproduzir animação e adiciona a file de animações a serem reproduzidas.
	 *
	 *	Caso não haja SUBTITLE, name será utilizado como SUBTITLE.
	 *	Caso não haja fadeLength, será atribuido fadeLength.
	 *	Caso não haja velocidade, hSlidersecond será atribuída.
	 */
	private AnimationState playAnimation(short type, string name, string subtitle, float speed)
	{
		try
		{
			AnimationState state = COMPONENT_ANIMATION.CrossFadeQueued(name, fadeLength, QueueMode.CompleteOthers);
			state.speed = speed;
			animQueue.Enqueue(new AnimationReference(name, subtitle, state, type));

			return state;
		}
		catch (NullReferenceException nre)
		{
			Debug.Log("'" + name + "' não foi encontrado!\n" + nre.ToString());
		}

		return null;
	}
	private AnimationState playAnimation(short type, string name, string subtitle) {
		return playAnimation(type, name, subtitle, getSpeedByType(type));
	}
	private AnimationState playAnimation(short type, string name) {
		return playAnimation(type, name, name);
	}


	/**
	 * 	Returns the asset bundle named aniName.
	 *
	 *	@return AssetBundle - se for encontrado.
	 *			null - se ocorrer num erro.
	 */
	protected abstract WWW loadAssetBundle(string aniName);


	/**
	 *	Listen to changes in the playing status.
	 */
	protected abstract void onPlayingStateChange();


	public void switchPauseState(bool paused)
	{
		if (this.paused != paused)
		{
			this.paused = paused;

			foreach (AnimationReference reference in animQueue)
				if (reference.state != null)
					reference.state.speed = paused ? 0F : getSpeedByType(reference.type);

			onPlayingStateChange();
		}
	}
	public void switchPauseState()
	{
		switchPauseState( ! paused);
	}

	public bool play()
	{
		if (playing)
			switchPauseState();
		else
			play(true, true, true);

		return true;
	}

	public bool play(bool stopLoading, bool stopPlaying, bool forceLoading)
	{
		try {
			if (loading)
			{
				if (stopLoading)
					stop_animations();
				else
					return false;
			}
			else if (playing)
			{
				if (stopPlaying)
					stopAnimations();

				else if ( ! forceLoading)
					return false;
			}
		} catch (NullReferenceException nre) { nre.ToString(); }

		StartCoroutine("loadAndPlay");
		return true;
	}


	/*
	 *	Destaca caractere de uma string.
	 */
	private string highlight(string word, int index)
	{
		string subtitle = "";
		int last = 0;

		if (index == 0)
			subtitle += "<b><color=white>" + word[0] + "</color></b>";
		else
			subtitle += word[0];

		for (int i = 1; i < word.Length; i++)
		{
			if ((word[i] >= 65 && word[i] <= 90) || (word[i] >= 48 && word[i] <= 57))
				subtitle += "-";

			if (i == index || (last == index && word[i] == word[last]))
			{
				subtitle += "<b><color=white>" + word[i] + "</color></b>";
				if (i == index) last = i;
			}
			else
			{
				subtitle += word[i];
				last = i;
			}
		}

		return subtitle;
	}

	/**
	 *	Spells word.
	 *
	 *	@return last animation's subtitle.
	 */
	private string spellWord(string word)
	{
		string lastAnimationSubtitle = "";
		bool defaultPlayed = false;

		// A reprodução da primeira letra deve ser longa para não ser cortada no fade
		letterSpeed = defaultLetterSpeed.getProporcionalSpeed(hSlidersecond);

		for (int i = 0; i < word.Length; i++)
		{
			char second = word[i];
			lastAnimationSubtitle = highlight(word, i);

			// Se for uma letra
			if (second >= 65 && second <= 90)
				playAnimation(TYPE_LETTER, second.ToString(), lastAnimationSubtitle, letterSpeed);

			// Se for um número
			else if (second >= 48 && second <= 57)
				playAnimation(TYPE_NUMBER, second.ToString(), lastAnimationSubtitle, numberSpeed);
			
			// Se for uma vírgula
			else if (second == 44)
				playAnimation(TYPE_WORD, second.ToString(), lastAnimationSubtitle);
			
			// Não há animação
			else
			{
				// Reproduz animação default apenas uma vez
				if ( ! defaultPlayed)
				{
					defaultPlayed = true;
					playAnimation(TYPE_NONE, DEFAULT_ANIMATION, lastAnimationSubtitle);

					// A reprodução da próxima letra deve ser longa para não ser cortada no fade
					letterSpeed = defaultLetterSpeed.getProporcionalSpeed(hSlidersecond);
				}

				Debug.Log("Animação \"" + second + "\" inexistente.");
				continue;
			}

			defaultPlayed = false;
			letterSpeed = defaultLetterSpeed.getProporcionalSpeed(hSlidersecond);
		}

		return lastAnimationSubtitle;
	}


	protected IEnumerator loadAnimation(string name)
	{
		loadingSingleAnimation = true;

		// Função loadAssetBundle é definida pela classe filha
		WWW www = loadAssetBundle(name);

		if (www != null)
		{
			yield return www;

			AssetBundle bundle = null;

			if (www.error == null)
				bundle = www.assetBundle;

			if (bundle != null && ! String.IsNullOrEmpty(bundle.mainAsset.name))
			{
				AnimationClip aniClip = bundle.mainAsset as AnimationClip;
				bundle.Unload(false);

				if (aniClip)
				{
					COMPONENT_ANIMATION.AddClip(aniClip, name);

					// Reproduz palavra
					loadedAssetBundles.Add(name);
					yield break;
				}
				else Debug.Log ("Sinal \"" + name + "\" não carregado corretamente.");
			}
		}

		// Soletra palavra
		nonexistentAssetBundles.Add(name);

		loadingSingleAnimation = false;
	}


	private IEnumerator loadAndPlay()
	{
		loading = true;
		onPlayingStateChange();

		string lastAnimationSubtitle = "";
		bool spelled = false;

		// Default
		playAnimation(TYPE_NONE, DEFAULT_ANIMATION, "", 2F);

		if ( ! playing)
		{
			playing = true;
			StartCoroutine("handleStates");
		}

		stringPos = glosa.Split(' ');

		foreach (string aniName in stringPos)
		{
			try {
				if (String.IsNullOrEmpty(aniName)) continue;
			} catch (Exception e) {
				Debug.Log(e + " :: NotNullNotEmpty");
			}

			bool nonexistent = nonexistentAssetBundles.Contains(aniName);
			bool loaded = loadedAssetBundles.Contains(aniName);

			if ( ! nonexistent && ! loaded)
			{
				// Função loadAssetBundle é definida pela classe filha
				WWW www = loadAssetBundle(aniName);

				if (www != null)
				{
					yield return www;

					AssetBundle bundle = null;

					if (www.error == null)
						bundle = www.assetBundle;

					if (bundle != null && ! String.IsNullOrEmpty(bundle.mainAsset.name))
					{
						AnimationClip aniClip = bundle.mainAsset as AnimationClip;
						bundle.Unload(false);

						if (aniClip)
						{
							COMPONENT_ANIMATION.AddClip(aniClip, aniName);

							loadedAssetBundles.Add(aniName);
							loaded = true;
						}
						else Debug.Log ("Sinal \"" + aniName + "\" não carregado corretamente.");
					}
				}
			}

			// Reproduz palavra
			if (loaded)
			{
				if (spelled)
				{
					// Default
					playAnimation(TYPE_NONE, DEFAULT_ANIMATION, lastAnimationSubtitle);
					spelled = false;
				}

				bool isPunctuation = false;

				if (aniName[0] == '[')
				{
					if (aniName.Equals("[PONTO]"))
					{
						isPunctuation = true;
						lastAnimationSubtitle = ".";
					}

					else if (aniName.Equals("[INTERROGACAO]"))
					{
						isPunctuation = true;
						lastAnimationSubtitle = "?";
					}

					else if (aniName.Equals("[EXCLAMACAO]"))
					{
						isPunctuation = true;
						lastAnimationSubtitle = "!";
					}
					else
					{
						lastAnimationSubtitle = aniName;
					}
				}
				
				if (isPunctuation)
					playAnimation(TYPE_WORD, aniName, lastAnimationSubtitle);
				else
					playAnimation(TYPE_WORD, aniName);
			}
			// Soletra palavra
			else
			{
				// Se a animação não foi carregada e nem está marcada como não existente,
				// adiciona ao set de animações não existentes
				if ( ! nonexistent)
					nonexistentAssetBundles.Add(aniName);

				Debug.Log("~~ To spell: " + aniName);

				if (aniName.Equals("[PONTO]") || aniName.Equals("[INTERROGACAO]") || aniName.Equals("[EXCLAMACAO]"))
				{
					playAnimation(TYPE_NONE, DEFAULT_ANIMATION, "", 1.6F);
					continue;
				}

				// Se já houve o soletramento de alguma palavra, reproduz animação default
				if (spelled)
					playAnimation(TYPE_NONE, DEFAULT_ANIMATION, lastAnimationSubtitle, 1.6F);
				else
					spelled = true;

				lastAnimationSubtitle = spellWord(aniName);
			}
		}

		// Default
		playAnimation(TYPE_NONE, DEFAULT_ANIMATION, "");

		loading = false;
		onPlayingStateChange();
	}

	//int _id = 0;

	/*
	 *	Sincroniza as legendas com as animações.
	 */
	IEnumerator handleStates()
	{
		// Enquanto estiver executando a rotina "loadAndPlay"
		// ou existir animações na fila de reprodução
		while (loading || animQueue.Count > 0)
		{
			if (animQueue.Count > 0)
			{
				AnimationReference reference = animQueue.Peek();

				setSubtitle(reference.subtitle);

				while (COMPONENT_ANIMATION.IsPlaying(reference.name))
				{
					reference.playing = true;
					yield return null;
				}

				if (reference.state == null)
					animQueue.Dequeue();
				else
					yield return null;
			}
			else yield return null;

			setSubtitle("");
		}

		playing = false;
		paused = false;
		onPlayingStateChange();
	}

}