Subversion Repository Public Repository

Nextrek

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
/*
 *
 *	Adventure Creator
 *	by Chris Burton, 2013-2014
 *	
 *	"ActionSpeech.cs"
 * 
 *	This action handles the displaying of messages, and talking of characters.
 * 
 */

using UnityEngine;
using System.Collections;
using AC;

#if UNITY_EDITOR
using UnityEditor;
#endif

[System.Serializable]
public class ActionSpeech : Action
{

	public int constantID = 0;
	
	public bool isPlayer;
	public Char speaker;
	public string messageText;
	public int lineID;
	public bool isBackground = false;
	public AnimationClip headClip;
	public AnimationClip mouthClip;

	public bool play2DHeadAnim = false;
	public string headClip2D = "";
	public int headLayer;
	public bool play2DMouthAnim = false;
	public string mouthClip2D = "";
	public int mouthLayer;

	private int splitNumber = 0;
	private bool splitDelay = false;

	private Dialog dialog;
	private StateHandler stateHandler;
	private SpeechManager speechManager;

	
	public ActionSpeech ()
	{
		this.isDisplayed = true;
		title = "Dialogue: Play speech";
		lineID = -1;
	}


	override public float Run ()
	{
		if (speechManager == null)
		{
			speechManager = AdvGame.GetReferences ().speechManager;
		}
		if (speechManager == null)
		{
			Debug.Log ("No Speech Manager present");
			return 0f;
		}

		dialog = GameObject.FindWithTag(Tags.gameEngine).GetComponent <Dialog>();
		stateHandler = GameObject.FindWithTag (Tags.persistentEngine).GetComponent <StateHandler>();

		if (dialog && stateHandler)
		{
			if (!isRunning)
			{
				isRunning = true;
				splitDelay = false;
				splitNumber = 0;
				
				return StartSpeech ();
			}
			else
			{
				if (!dialog.isMessageAlive)
				{
					if (speechManager.separateLines)
					{
						if (!splitDelay)
						{
							// Begin pause if more lines are present
							splitNumber ++;
							string[] textArray = messageText.Split ('\n');

							if (textArray.Length > splitNumber)
							{
								// Still got more to go, so pause for a moment
								splitDelay = true;
								return speechManager.separateLinePause;
							}
							else
							{
								// Finished
								isRunning = false;
								stateHandler.gameState = GameState.Cutscene;
								return 0f;
							}
						}
						else
						{
							// Show next line
							splitDelay = false;
							return StartSpeech ();
						}
					}
					else
					{
						isRunning = false;
						stateHandler.gameState = GameState.Cutscene;
						return 0f;
					}
				}
				else
				{
					return defaultPauseTime;
				}
			}
		}
		
		return 0f;
	}
	
	#if UNITY_EDITOR

	override public void ShowGUI ()
	{
		if (lineID > -1)
		{
			EditorGUILayout.LabelField ("Speech Manager ID:", lineID.ToString ());
		}
		
		isPlayer = EditorGUILayout.Toggle ("Player line?",isPlayer);
		if (isPlayer)
		{
			if (Application.isPlaying)
			{
				speaker = GameObject.FindWithTag (Tags.player).GetComponent <AC.Char>();
			}
			else
			{
				speaker = AdvGame.GetReferences ().settingsManager.GetDefaultPlayer ();
			}
		}
		else
		{
			speaker = (Char) EditorGUILayout.ObjectField ("Speaker:", speaker, typeof(Char), true);

			if (speaker && speaker.GetComponent <ConstantID>())
			{
				constantID = speaker.GetComponent <ConstantID>().constantID;
			}
		}

		EditorGUILayout.LabelField ("Line text:", GUILayout.Width (145f));
		EditorStyles.textField.wordWrap = true;
		messageText = EditorGUILayout.TextArea (messageText);

		if (speaker)
		{
			if (speaker.animEngine == null)
			{
				speaker.ResetAnimationEngine ();
			}
			if (speaker.animEngine)
			{
				speaker.animEngine.ActionSpeechGUI (this);
			}
		}
		else
		{
			EditorGUILayout.HelpBox ("This Action requires a Character before more options will show.", MessageType.Info);
		}
		
		isBackground = EditorGUILayout.Toggle ("Play in background?", isBackground);

		AfterRunningOption ();
	}


	override public string SetLabel ()
	{
		string labelAdd = "";
		
		if (messageText != "")
		{
			string shortMessage = messageText;
			if (shortMessage != null)
			{
				if (shortMessage.Contains ("\n"))
				{
					shortMessage = shortMessage.Replace ("\n", "");
				}
				if (shortMessage.Length > 30)
				{
					shortMessage = shortMessage.Substring (0, 28) + "..";
				}
			}

			labelAdd = " (" + shortMessage + ")";
		}
		
		return labelAdd;
	}

	#endif

	private string ConvertTokens (string _text)
	{
		if (_text.Contains ("[var:"))
		{
			RuntimeVariables runtimeVariables = GameObject.FindWithTag (Tags.persistentEngine).GetComponent <RuntimeVariables>();
			foreach (GVar _var in runtimeVariables.localVars)
			{
				string tokenText = "[var:" + _var.id + "]";
				if (_text.Contains (tokenText))
				{
					_text = _text.Replace (tokenText, runtimeVariables.GetVarValue (_var.id));
				}
			}
		}

		return _text;
	}


	private float StartSpeech ()
	{
		string _text = messageText;
		string _language = "";
		
		if (Options.GetLanguage () > 0)
		{
			// Not in original language, so pull translation in from Speech Manager
			_text = SpeechManager.GetTranslation (lineID, Options.GetLanguage ());
			_language = speechManager.languages [Options.GetLanguage ()];
		}
		
		if (speechManager.separateLines)
		{
			// Split line into an array, and pull the correct one
			string[] textArray = _text.Split ('\n');
			_text = textArray [splitNumber];
		}
		
		_text = ConvertTokens (_text);
		
		if (_text != "")
		{
			dialog.KillDialog ();
			
			if (isPlayer)
			{
				speaker = GameObject.FindWithTag(Tags.player).GetComponent <Player>();
			}
			else if (isAssetFile && constantID != 0)
			{
				// Attempt to find the correct scene object
				ConstantID idObject = Serializer.returnComponent <ConstantID> (constantID);
				if (idObject != null && idObject.GetComponent <Char>())
				{
					speaker = idObject.GetComponent <Char>();
				}
			}
			
			if (speaker)
			{
				dialog.StartDialog (speaker, _text, lineID, _language);
				
				if (speaker.animEngine == null)
				{
					speaker.ResetAnimationEngine ();
				}
				
				if (speaker.animEngine != null)
				{
					speaker.animEngine.ActionSpeechRun (this);
				}
			}
			else
			{
				dialog.StartDialog (_text);
			}
			
			if (!isBackground || speechManager.separateLines)
			{
				return defaultPauseTime;
			}
		}
		
		return 0f;
	}

}

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/AdventureCreator/Scripts/Actions/ActionSpeech.cs

Diff revisions: vs.
Revision Author Commited Message
83 FMMortaroli picture FMMortaroli Tue 13 May, 2014 11:32:51 +0000