private void OnHasFileChanged(object sender, EventArgs args) { if (Controller.CurrentState == PlayerState.Stopped) { TrackNavigation.JumpFirst(); } }
public void CustomizePrefab(GameObject prefab) { TrackNavigation[] navList = prefab.GetComponentsInChildren <TrackNavigation>(); int i = 0; foreach (TrackNavigation nav in navList) { Debug.Log("Navigation: " + nav.ToString()); nav.gameObject.name = "Navigation_" + i++; BoxCollider2D collider = nav.gameObject.AddComponent <BoxCollider2D>(); collider.isTrigger = true; collider.size = new Vector2(TRIGGER_SIZE, TRIGGER_SIZE); collider.offset = new Vector2(TRIGGER_OFFSET, TRIGGER_OFFSET); nav.centerOffset = collider.offset; } foreach (TrackNavigation nav in navList) { Vector3 position = nav.position; Vector3 dir = nav.gameObject.transform.right; bool origFlag = Physics2D.queriesStartInColliders; Physics2D.queriesStartInColliders = false; RaycastHit2D hit = Physics2D.Raycast(position, dir, TrackNavigation.MAX_RAY_LENGTH, 1 << nav.gameObject.layer); if (hit.collider != null) { TrackNavigation other = hit.collider.gameObject.GetComponent <TrackNavigation>(); if (other != null) { float distance = Vector3.Distance(position, other.position); hit = Physics2D.Raycast(position, dir, distance, 1 << LayerMask.NameToLayer("FinishLine")); if (hit.collider != null) { GameObject go = new GameObject("FinishLine"); go.transform.parent = nav.transform.parent; go.transform.position = hit.point; TrackNavigation finish = go.AddComponent <TrackNavigation>(); finish.nextNavigationLink = other; finish.previousNavigationLink = nav; nav.nextNavigationLink = finish; other.previousNavigationLink = finish; finish.crossesFinishLine = true; } else { nav.nextNavigationLink = other; other.previousNavigationLink = nav; } } } Physics2D.queriesStartInColliders = origFlag; } }
public void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.layer == GameController.FINISH_LINE_LAYER) { TrackNavigation link = _trackController.GetClosestLink(transform.position); if (link != null) { if (link.crossesFinishLine && _trackLink.previousNavigationLink == link) { _trackController.CrossFinishLine(this); } } } }
// Use this for initialization void Start() { _lapsComplete = new Dictionary <string, int>(); _startPoints = new List <StartPoint>(); StartPoint[] points = FindObjectsOfType <StartPoint>(); for (int i = 0; i < points.Length; ++i) { _startPoints.Add(points[i]); } _startPoints.Sort((x, y) => x.startIndex.CompareTo(y.startIndex)); _navPoints = new List <TrackNavigation>(); TrackNavigation[] navPoints = FindObjectsOfType <TrackNavigation>(); for (int i = 0; i < navPoints.Length; ++i) { _navPoints.Add(navPoints[i]); MeshRenderer r = navPoints[i].GetComponentInChildren <MeshRenderer>(); if (r != null) { r.enabled = false; } if (navPoints[i].crossesFinishLine) { _startingNavLine = navPoints[i]; } } if (_startingNavLine == null) { Debug.LogError("Missing start navigation lines!"); return; } TrackNavigation nav = _startingNavLine.nextNavigationLink; int index = 0; _startingNavLine.index = index; while (nav != null && nav != _startingNavLine) { index++; nav.index = index; nav = nav.nextNavigationLink; if (index > 1000) { Debug.LogError("There is no way there are 1000+ nav markers... infinite loop is probable..."); break; } } }
// Update is called once per frame protected void Update() { PreUpdate(); if (_trackController != null) { if (_trackLink == null) { return; } // Get the distance to the registered link float distance = Vector3.Distance(this.transform.position, _trackLink.position); // Find the closest link if there is one TrackNavigation link = _trackController.GetClosestLink(transform.position); // Is it a different link than the one we have registered? // This only matters if its a non-null link, and we are outside the range of the current link if (link != _trackLink && link != null && distance > _trackLink.navigationRadius * _radiusModifier) { // Going backwards? if (link.index < lastNavIndex) { // Show wrong way text? have to work out wrapping indices } else if (link.index > lastNavIndex + 1 && !_trackLink.crossesFinishLine) { //transform.position = _trackLink.previousNavigationLink.position; } } else { if (distance < _trackLink.navigationRadius * _radiusModifier) { _trackLink = _trackLink.nextNavigationLink; lastNavIndex = _trackLink.index; } } } _boosting = Input.GetButton("Boost"); PostUpdate(); }
public TrackNavigation GetClosestLink(Vector3 position) { float minDistance = float.MaxValue; TrackNavigation closest = null; for (int i = 0; i < _navPoints.Count; ++i) { TrackNavigation nav = _navPoints[i]; if (nav != null) { float distance = Vector3.Distance(position, nav.position); // Only considered closest IF we are inside the radius if (distance < minDistance && distance < nav.navigationRadius) { minDistance = distance; closest = nav; } } } return(closest); }
public void SpawnAtStart(int index) { transform.position = _trackController.SpawnPoint(index); _trackLink = _trackController.StartingLink; }
void Awake() { Instance = this; }
private void OnPlayerMediaEnded(object sender, EventArgs e) { Controller.Stop(); TrackNavigation.JumpNext(); Controller.Play(); }
protected override void PreUpdate() { _prevLink = _trackLink; }