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
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
/*
 *
 *	Adventure Creator
 *	by Chris Burton, 2013-2014
 *	
 *	"AdvGame.cs"
 * 
 *	This script provides a number of static functions used by various game scripts.
 * 
 * 	The "DrawOutline" function is based on BĂ©renger's code: http://wiki.unity3d.com/index.php/ShadowAndOutline
 * 
 */

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using AC;

#if UNITY_EDITOR
using UnityEditor;
#endif


public class AdvGame : ScriptableObject
{

	public static List<AC.Action> copiedActions = new List<AC.Action>();


	public static References GetReferences ()
	{
		References references = (References) Resources.Load (Resource.references);
		
		if (references)
		{
			return (references);
		}
		
		return (null);
	}


	#if UNITY_EDITOR

	public static void DrawNodeCurve (Rect start, Rect end, Color color, int offset)
	{
		Vector2 endPos = new Vector2 (end.x - 15, end.y + 10);
		DrawNodeCurve (start, endPos, color, offset);
	}
	
	
	public static void DrawNodeCurve (Rect start, Vector2 end, Color color, int offset)
	{
		Vector3 startPos = new Vector3(start.x + start.width + 16, start.y + start.height - offset - 5, 0);
		Vector3 endPos = new Vector3(end.x, end.y - 1, 0);
		
		float dist = Mathf.Abs (startPos.x - endPos.x);
		
		Vector3 startTan = startPos + Vector3.right * dist / 2;
		Vector3 endTan = endPos + Vector3.left * dist / 2;
		Handles.DrawBezier(startPos, endPos, startTan, endTan, color, null, 3);
	}

	#endif


	public static void DuplicateActionsBuffer ()
	{
		List<AC.Action> tempList = new List<AC.Action>();
		foreach (AC.Action action in copiedActions)
		{
			AC.Action copyAction = Object.Instantiate (action) as AC.Action;
			copyAction.nodeRect = new Rect (0,0,300,60);
			tempList.Add (copyAction);
		}

		copiedActions.Clear ();
		copiedActions = tempList;
	}


	public static Vector3 GetScreenDirection (Vector3 originWorldPosition, Vector3 targetWorldPosition)
	{
		Vector3 originScreenPosition = Camera.main.WorldToScreenPoint (originWorldPosition);
		Vector3 targetScreenPosition = Camera.main.WorldToScreenPoint (targetWorldPosition);

		Vector3 lookVector = targetScreenPosition - originScreenPosition;
		lookVector.z = lookVector.y;
		lookVector.y = 0;

		return (lookVector);
	}


	public static Vector3 GetScreenNavMesh (Vector3 targetWorldPosition)
	{
		SettingsManager settingsManager = AdvGame.GetReferences ().settingsManager;

		Vector3 targetScreenPosition = Camera.main.WorldToScreenPoint (targetWorldPosition);
		Ray ray = Camera.main.ScreenPointToRay (targetScreenPosition);
		RaycastHit hit = new RaycastHit();
		
		if (settingsManager && Physics.Raycast (ray, out hit, settingsManager.navMeshRaycastLength, 1 << LayerMask.NameToLayer (settingsManager.navMeshLayer)))
		{
			return hit.point;
		}

		return targetWorldPosition;
	}


	public static Vector2 GetMainGameViewSize ()
	{
		if (Application.isPlaying)
		{
			return new Vector2 (Screen.width, Screen.height);
		}
		
		System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
		System.Reflection.MethodInfo GetSizeOfMainGameView = T.GetMethod ("GetSizeOfMainGameView", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
		System.Object Res = GetSizeOfMainGameView.Invoke (null,null);
		return (Vector2) Res;
	}
	
	
	public static Matrix4x4 SetVanishingPoint (Camera _camera, Vector2 perspectiveOffset)
	{
		Matrix4x4 m = _camera.projectionMatrix;
		float w = 2f * _camera.nearClipPlane / m.m00;
		float h = 2f * _camera.nearClipPlane / m.m11;
	 
		float left = -(w / 2) + perspectiveOffset.x;
		float right = left + w;
		float bottom = -(h / 2) + perspectiveOffset.y;
		float top = bottom + h;
	 
		return (PerspectiveOffCenter (left, right, bottom, top, _camera.nearClipPlane, _camera.farClipPlane));
	}


	private static Matrix4x4 PerspectiveOffCenter (float left, float right, float bottom, float top, float near, float far)
	{
		float x =  (2f * near) / (right - left);
		float y =  (2f * near) / (top - bottom);
		float a =  (right + left) / (right - left);
		float b =  (top + bottom) / (top - bottom);
		float c = -(far + near) / (far - near);
		float d = -(2f * far * near) / (far - near);
		float e = -1f;
	 
		Matrix4x4 m = new Matrix4x4();
		m[0,0] = x;		m[0,1] = 0f;	m[0,2] = a;		m[0,3] = 0f;
		m[1,0] = 0f;	m[1,1] = y;		m[1,2] = b;		m[1,3] = 0f;
		m[2,0] = 0f;	m[2,1] = 0f;	m[2,2] = c;		m[2,3] =   d;
		m[3,0] = 0f;	m[3,1] = 0f;	m[3,2] = e;		m[3,3] = 0f;
		return m;
	}
	
	
	public static string UniqueName (string name)
	{
		if (GameObject.Find (name))
		{
			string newName = name;
			
			for (int i=2; i<20; i++)
			{
				newName = name + i.ToString ();
				
				if (!GameObject.Find (newName))
				{
					break;
				}
			}
			
			return newName;
		}
		else
		{
			return name;
		}
	}
	
	
	public static string GetName (string resourceName)
	{
		int slash = resourceName.IndexOf ("/");
		string newName;
		
		if (slash > 0)
		{
			newName = resourceName.Remove (0, slash+1);
		}
		else
		{
			newName = resourceName;
		}
		
		return newName;
	}
	
	
	public static Rect GUIBox (float centre_x, float centre_y, float size)
	{
		Rect newRect;
		newRect = GUIRect (centre_x, centre_y, size, size);
		return (newRect);
	}
	
	
	public static Rect GUIRect (float centre_x, float centre_y, float width, float height)
	{
		Rect newRect;
		newRect = new Rect (Screen.width * centre_x - (Screen.width * width)/2, Screen.height * centre_y - (Screen.width * height)/2, Screen.width * width, Screen.width * height);
		return (newRect);
	}
	
	
	public static Rect GUIBox (Vector2 posVector, float size)
	{
		Rect newRect;
		newRect = GUIRect (posVector.x / Screen.width, (Screen.height - posVector.y) / Screen.height, size, size);
		return (newRect);
	}
	
	
	public static void AddAnimClip (Animation _animation, int layer, AnimationClip clip, AnimationBlendMode blendMode, WrapMode wrapMode, Transform mixingBone)
	{
		if (clip != null)
		{
			// Initialises a clip
			_animation.AddClip (clip, clip.name);
			
			if (mixingBone != null)
			{
				_animation [clip.name].AddMixingTransform (mixingBone);
			}
			
			// Set up the state
			_animation [clip.name].layer = layer;
			_animation [clip.name].normalizedTime = 0f;
			_animation [clip.name].blendMode = blendMode;
			_animation [clip.name].wrapMode = wrapMode;
			_animation [clip.name].enabled = true;
		}
	}
	
	
	public static void PlayAnimClip (Animation _animation, int layer, AnimationClip clip, AnimationBlendMode blendMode, WrapMode wrapMode, float fadeTime, Transform mixingBone, bool reverse)
	{
		// Initialises and crossfades a clip

		if (clip != null)
		{
			AddAnimClip (_animation, layer, clip, blendMode, wrapMode, mixingBone);
			if (reverse)
			{
				_animation[clip.name].speed *= -1f;
			}
			_animation.CrossFade (clip.name, fadeTime);
			CleanUnusedClips (_animation);
		}
	}


	public static AudioClip FindAudioClipResource (string clipName)
	{
		if (clipName == "")
		{
			return null;
		}
		
		Object[] objects = Resources.FindObjectsOfTypeAll (typeof (AudioClip));
		foreach (Object _object in objects)
		{
			if (_object.name == clipName)
			{
				return (AudioClip) _object;
			}
		}
		
		return null;
	}


	public static AnimationClip FindAnimClipResource (string clipName)
	{
		if (clipName == "")
		{
			return null;
		}

		Object[] objects = Resources.FindObjectsOfTypeAll (typeof (AnimationClip));
		foreach (Object _object in objects)
		{
			if (_object.name == clipName)
			{
				return (AnimationClip) _object;
			}
		}

		return null;
	}

	
	public static void CleanUnusedClips (Animation _animation)
	{
		// Remove any non-playing animations
		
		List <string> removeClips = new List <string>();
		
		foreach (AnimationState state in _animation)
		{
			if (!_animation [state.name].enabled)
			{
				// Queued animations get " - Queued Clone" appended to it, so remove
				
				int queueIndex = state.name.IndexOf (" - Queued Clone");

				if (queueIndex > 0)
				{
					removeClips.Add (state.name.Substring (0, queueIndex));
				}
				else
				{
					removeClips.Add (state.name);
				}
			}
		}
		
		foreach (string _clip in removeClips)
		{
			_animation.RemoveClip (_clip);
		}
		
	}
	
	
	public static float SmoothTimeFactor (float startT, float deltaT)
	{
		// Return a smooth time scale
		
		float t01 = (Time.time - startT) / deltaT;
		float F = 0.5f - 0.515f * Mathf.Sin (3f * t01 + 1.5f);
		return F;
	}
	
	
	public static float LinearTimeFactor (float startT, float deltaT)
	{
		// Return a linear time scale

		float t01 = (Time.time - startT) / deltaT;
		return t01;
	}
	
	
	public static Rect Rescale (Rect _rect)
	{
		float ScaleFactor;
		ScaleFactor = Screen.width / 884.0f;
		int ScaleFactorInt = Mathf.RoundToInt(ScaleFactor);
		Rect newRect = new Rect (_rect.x * ScaleFactorInt, _rect.y * ScaleFactorInt, _rect.width * ScaleFactorInt, _rect.height * ScaleFactorInt);
		
		return (newRect);
	}
	
	
	public static int Rescale (int _int)
	{
		float ScaleFactor;
		ScaleFactor = Screen.width / 884.0f;
		int ScaleFactorInt = Mathf.RoundToInt(ScaleFactor);
		int returnValue;
		returnValue = _int * ScaleFactorInt;
		
		return (returnValue);
	}
	
	
	public static void DrawTextOutline (Rect rect, string text, GUIStyle style, Color outColor, Color inColor, float size)
	{
		float halfSize = size * 0.5F;
		GUIStyle backupStyle = new GUIStyle(style);
		Color backupColor = GUI.color;
		
		outColor.a = GUI.color.a;
		style.normal.textColor = outColor;
		GUI.color = outColor;

		rect.x -= halfSize;
		GUI.Label(rect, text, style);

		rect.x += size;
		GUI.Label(rect, text, style);

		rect.x -= halfSize;
		rect.y -= halfSize;
		GUI.Label(rect, text, style);

		rect.y += size;
		GUI.Label(rect, text, style);

		rect.y -= halfSize;
		style.normal.textColor = inColor;
		GUI.color = backupColor;
		GUI.Label(rect, text, style);

		style = backupStyle;
	}
	
}	

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/AdventureCreator/Scripts/Static/AdvGame.cs

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