InspectorScript.cs
3.49 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
//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