InspectorScript.cs 3.49 KB
//Log Dir         http://docs.unity3d.com/Manual/LogFiles.html
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using System.Threading;

public class InspectorScript : MonoBehaviour {
	
	public Boolean isCaptionsActive = true;
	string alfabeto = "0123456789,ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	string aniName = "";
	AnimationClip aniClip;
	AnimationClip defaultClip;
	GameObject ICARO;
	Animation COMPONENT_ANIMATION;
	Server server;
	Semaphore serverSemaphore;
	Thread t, tPlayer;
	Queue<String> glosaQueue;
	Queue<SubtitleInfo> subtitleQueue;
	bool finalizeFlag;
	Time time;
	float playerSpeed;
	string[] strArg;

	void Start(){
		strArg = Environment.GetCommandLineArgs();
		if(strArg.Length == 4)
			playerSpeed = 1+((float.Parse(strArg[3]))/100);
		else
			playerSpeed = 1.5f;
		serverSemaphore = new Semaphore (0, 1);
		glosaQueue = new Queue<String>();
		subtitleQueue = new Queue<SubtitleInfo>();
		finalizeFlag = false;
		server = new Server (serverSemaphore, this); 
		t = new Thread(new ThreadStart(server.startServer));
		t.Start ();
		tPlayer = new Thread(new ThreadStart(playerRun));
		serverSemaphore.WaitOne (); // Waits until connection with client is established in server.startServer() method
		ICARO = GameObject.FindGameObjectWithTag("avatar");
		COMPONENT_ANIMATION = ICARO.GetComponent<Animation>();
		addAlpha ();
		sendToPlayer ("");
		tPlayer.Start();
	} // Start
	
	void addAlpha( ){
		foreach( char letter in alfabeto ){
			aniClip = Resources.Load<AnimationClip> ("ANIMS/alpha/"+letter);
			if( aniClip ) 
				COMPONENT_ANIMATION.AddClip(aniClip, ""+letter);
			else 
				Debug.Log("Anim "+aniName+" not found");
		}
	}//addAlpha

	public void setFinalizeFlag(bool b){
		finalizeFlag = b;
	}
	public void addGlosaQueue(String str){
		glosaQueue.Enqueue (str);
	}
	public void addSubtitleQueue(SubtitleInfo si){
		subtitleQueue.Enqueue (si);
	}

	// Gets glosa from queue to animate them
	void playerRun(){
		do {
			if(subtitleQueue.Count > 0){
				long pts = subtitleQueue.Peek().getPts();
				if (Time.time > (pts/1000)){ // em segundos
					String glosa = subtitleQueue.Dequeue().getGlosa ();
					sendToPlayer (glosa);
				}
			}
			try{
				Thread.Sleep(100); // waits a little while
			}
			catch(ThreadInterruptedException e){
				Console.WriteLine(e.Source);
			}
			if(finalizeFlag) // Core told to finalize
				if(subtitleQueue.Count == 0) // if there is no glosa to animate
					break;
		} while(true);
		while (COMPONENT_ANIMATION.isPlaying) {
			try{
				Thread.Sleep(500);
			}
			catch(ThreadInterruptedException e){
				Console.WriteLine(e.Source);
			}
		}
		server.sendFinalizeToCore (); // all frames saved
		server.closeConnections ();
		Application.Quit ();
	} // playerRun

	void sendToPlayer(String glosa){
		String[] stringPos;

		glosa +=" _default";
		glosa = glosa.ToUpper ();
		stringPos = glosa.Split(' ');
				
		// Looks for animation 
		foreach( string aniName in stringPos ){
			aniClip = Resources.Load<AnimationClip>("ANIMS/anims/"+aniName);
			if( aniClip ){
				COMPONENT_ANIMATION.AddClip(aniClip, aniName);
				COMPONENT_ANIMATION.CrossFadeQueued( aniName, 0.6F, QueueMode.CompleteOthers );//0.4
			}
			else{ // Spells, if not found
				foreach(char letter in aniName)
					COMPONENT_ANIMATION.CrossFadeQueued(""+letter, 0.6F, QueueMode.CompleteOthers);
			}
		}
		foreach(AnimationState animState in COMPONENT_ANIMATION){
			animState.speed = playerSpeed;
		}
	} // sendToPlayer
} // InspectorIscript