IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
TeleportPoint.cs
1 //======= Copyright (c) Valve Corporation, All rights reserved. ===============
2 //
3 // Purpose: Single location that the player can teleport to
4 //
5 //=============================================================================
6 
7 using UnityEngine;
8 using UnityEngine.UI;
9 #if UNITY_EDITOR
10 using UnityEditor;
11 #endif
12 
13 namespace Valve.VR.InteractionSystem
14 {
15  //-------------------------------------------------------------------------
17  {
18  public enum TeleportPointType
19  {
20  MoveToLocation,
21  SwitchToNewScene
22  };
23 
24  //Public variables
25  public TeleportPointType teleportType = TeleportPointType.MoveToLocation;
26  public string title;
27  public string switchToScene;
28  public Color titleVisibleColor;
29  public Color titleHighlightedColor;
30  public Color titleLockedColor;
31  public bool playerSpawnPoint = false;
32 
33  //Private data
34  private bool gotReleventComponents = false;
35  private MeshRenderer markerMesh;
36  private MeshRenderer switchSceneIcon;
37  private MeshRenderer moveLocationIcon;
38  private MeshRenderer lockedIcon;
39  private MeshRenderer pointIcon;
40  private Transform lookAtJointTransform;
41  private new Animation animation;
42  private Text titleText;
43  private Player player;
44  private Vector3 lookAtPosition = Vector3.zero;
45  private int tintColorID = 0;
46  private Color tintColor = Color.clear;
47  private Color titleColor = Color.clear;
48  private float fullTitleAlpha = 0.0f;
49 
50  //Constants
51  private const string switchSceneAnimation = "switch_scenes_idle";
52  private const string moveLocationAnimation = "move_location_idle";
53  private const string lockedAnimation = "locked_idle";
54 
55 
56  //-------------------------------------------------
57  public override bool showReticle
58  {
59  get
60  {
61  return false;
62  }
63  }
64 
65 
66  //-------------------------------------------------
67  void Awake()
68  {
69  GetRelevantComponents();
70 
71  animation = GetComponent<Animation>();
72 
73  tintColorID = Shader.PropertyToID( "_TintColor" );
74 
75  moveLocationIcon.gameObject.SetActive( false );
76  switchSceneIcon.gameObject.SetActive( false );
77  lockedIcon.gameObject.SetActive( false );
78 
79  UpdateVisuals();
80  }
81 
82 
83  //-------------------------------------------------
84  void Start()
85  {
86  player = Player.instance;
87  }
88 
89 
90  //-------------------------------------------------
91  void Update()
92  {
93  if ( Application.isPlaying )
94  {
95  lookAtPosition.x = player.hmdTransform.position.x;
96  lookAtPosition.y = lookAtJointTransform.position.y;
97  lookAtPosition.z = player.hmdTransform.position.z;
98 
99  lookAtJointTransform.LookAt( lookAtPosition );
100  }
101  }
102 
103 
104  //-------------------------------------------------
105  public override bool ShouldActivate( Vector3 playerPosition )
106  {
107  return ( Vector3.Distance( transform.position, playerPosition ) > 1.0f );
108  }
109 
110 
111  //-------------------------------------------------
112  public override bool ShouldMovePlayer()
113  {
114  return true;
115  }
116 
117 
118  //-------------------------------------------------
119  public override void Highlight( bool highlight )
120  {
121  if ( !locked )
122  {
123  if ( highlight )
124  {
125  SetMeshMaterials( Teleport.instance.pointHighlightedMaterial, titleHighlightedColor );
126  }
127  else
128  {
129  SetMeshMaterials( Teleport.instance.pointVisibleMaterial, titleVisibleColor );
130  }
131  }
132 
133  if ( highlight )
134  {
135  pointIcon.gameObject.SetActive( true );
136  animation.Play();
137  }
138  else
139  {
140  pointIcon.gameObject.SetActive( false );
141  animation.Stop();
142  }
143  }
144 
145 
146  //-------------------------------------------------
147  public override void UpdateVisuals()
148  {
149  if ( !gotReleventComponents )
150  {
151  return;
152  }
153 
154  if ( locked )
155  {
156  SetMeshMaterials( Teleport.instance.pointLockedMaterial, titleLockedColor );
157 
158  pointIcon = lockedIcon;
159 
160  animation.clip = animation.GetClip( lockedAnimation );
161  }
162  else
163  {
164  SetMeshMaterials( Teleport.instance.pointVisibleMaterial, titleVisibleColor );
165 
166  switch ( teleportType )
167  {
168  case TeleportPointType.MoveToLocation:
169  {
170  pointIcon = moveLocationIcon;
171 
172  animation.clip = animation.GetClip( moveLocationAnimation );
173  }
174  break;
175  case TeleportPointType.SwitchToNewScene:
176  {
177  pointIcon = switchSceneIcon;
178 
179  animation.clip = animation.GetClip( switchSceneAnimation );
180  }
181  break;
182  }
183  }
184 
185  titleText.text = title;
186  }
187 
188 
189  //-------------------------------------------------
190  public override void SetAlpha( float tintAlpha, float alphaPercent )
191  {
192  tintColor = markerMesh.material.GetColor( tintColorID );
193  tintColor.a = tintAlpha;
194 
195  markerMesh.material.SetColor( tintColorID, tintColor );
196  switchSceneIcon.material.SetColor( tintColorID, tintColor );
197  moveLocationIcon.material.SetColor( tintColorID, tintColor );
198  lockedIcon.material.SetColor( tintColorID, tintColor );
199 
200  titleColor.a = fullTitleAlpha * alphaPercent;
201  titleText.color = titleColor;
202  }
203 
204 
205  //-------------------------------------------------
206  public void SetMeshMaterials( Material material, Color textColor )
207  {
208  markerMesh.material = material;
209  switchSceneIcon.material = material;
210  moveLocationIcon.material = material;
211  lockedIcon.material = material;
212 
213  titleColor = textColor;
214  fullTitleAlpha = textColor.a;
215  titleText.color = titleColor;
216  }
217 
218 
219  //-------------------------------------------------
220  public void TeleportToScene()
221  {
222  if ( !string.IsNullOrEmpty( switchToScene ) )
223  {
224  Debug.Log( "TeleportPoint: Hook up your level loading logic to switch to new scene: " + switchToScene );
225  }
226  else
227  {
228  Debug.LogError( "TeleportPoint: Invalid scene name to switch to: " + switchToScene );
229  }
230  }
231 
232 
233  //-------------------------------------------------
234  public void GetRelevantComponents()
235  {
236  markerMesh = transform.Find( "teleport_marker_mesh" ).GetComponent<MeshRenderer>();
237  switchSceneIcon = transform.Find( "teleport_marker_lookat_joint/teleport_marker_icons/switch_scenes_icon" ).GetComponent<MeshRenderer>();
238  moveLocationIcon = transform.Find( "teleport_marker_lookat_joint/teleport_marker_icons/move_location_icon" ).GetComponent<MeshRenderer>();
239  lockedIcon = transform.Find( "teleport_marker_lookat_joint/teleport_marker_icons/locked_icon" ).GetComponent<MeshRenderer>();
240  lookAtJointTransform = transform.Find( "teleport_marker_lookat_joint" );
241 
242  titleText = transform.Find( "teleport_marker_lookat_joint/teleport_marker_canvas/teleport_marker_canvas_text" ).GetComponent<Text>();
243 
244  gotReleventComponents = true;
245  }
246 
247 
248  //-------------------------------------------------
249  public void ReleaseRelevantComponents()
250  {
251  markerMesh = null;
252  switchSceneIcon = null;
253  moveLocationIcon = null;
254  lockedIcon = null;
255  lookAtJointTransform = null;
256  titleText = null;
257  }
258 
259 
260  //-------------------------------------------------
261  public void UpdateVisualsInEditor()
262  {
263  if ( Application.isPlaying )
264  {
265  return;
266  }
267 
268  GetRelevantComponents();
269 
270  if ( locked )
271  {
272  lockedIcon.gameObject.SetActive( true );
273  moveLocationIcon.gameObject.SetActive( false );
274  switchSceneIcon.gameObject.SetActive( false );
275 
276  markerMesh.sharedMaterial = Teleport.instance.pointLockedMaterial;
277  lockedIcon.sharedMaterial = Teleport.instance.pointLockedMaterial;
278 
279  titleText.color = titleLockedColor;
280  }
281  else
282  {
283  lockedIcon.gameObject.SetActive( false );
284 
285  markerMesh.sharedMaterial = Teleport.instance.pointVisibleMaterial;
286  switchSceneIcon.sharedMaterial = Teleport.instance.pointVisibleMaterial;
287  moveLocationIcon.sharedMaterial = Teleport.instance.pointVisibleMaterial;
288 
289  titleText.color = titleVisibleColor;
290 
291  switch ( teleportType )
292  {
293  case TeleportPointType.MoveToLocation:
294  {
295  moveLocationIcon.gameObject.SetActive( true );
296  switchSceneIcon.gameObject.SetActive( false );
297  }
298  break;
299  case TeleportPointType.SwitchToNewScene:
300  {
301  moveLocationIcon.gameObject.SetActive( false );
302  switchSceneIcon.gameObject.SetActive( true );
303  }
304  break;
305  }
306  }
307 
308  titleText.text = title;
309 
310  ReleaseRelevantComponents();
311  }
312  }
313 
314 
315 #if UNITY_EDITOR
316  //-------------------------------------------------------------------------
317  [CustomEditor( typeof( TeleportPoint ) )]
318  public class TeleportPointEditor : Editor
319  {
320  //-------------------------------------------------
321  void OnEnable()
322  {
323  if ( Selection.activeTransform )
324  {
325  TeleportPoint teleportPoint = Selection.activeTransform.GetComponent<TeleportPoint>();
326  teleportPoint.UpdateVisualsInEditor();
327  }
328  }
329 
330 
331  //-------------------------------------------------
332  public override void OnInspectorGUI()
333  {
334  DrawDefaultInspector();
335 
336  if ( Selection.activeTransform )
337  {
338  TeleportPoint teleportPoint = Selection.activeTransform.GetComponent<TeleportPoint>();
339  if ( GUI.changed )
340  {
341  teleportPoint.UpdateVisualsInEditor();
342  }
343  }
344  }
345  }
346 #endif
347 }