private void UpdateWhilePlaying() { List <Attractor> attractors; List <Polyline> lines; List <Obstacle> obstacles; attractors = FindObjectsOfType <Attractor>().ToList(); lines = FindObjectsOfType <Polyline>().ToList(); obstacles = FindObjectsOfType <Obstacle>().ToList(); for (int i = 0; i < lines.Count; i++) { lines[i].Deform(attractors); } // calculate blocked lines var blockedLines = PolylineService.CalculatedBlocked(lines, obstacles); // update blocked and unblocked lines foreach (var l in lines.Except(blockedLines)) { if (l.Blocked) { OnObstacleFreed(); } l.Blocked = false; } foreach (var bl in blockedLines) { if (!bl.Blocked) { OnObstacleHit(); } bl.Blocked = true; } // calculate connections, exclude blocked lines var start = lines.FirstOrDefault(l => l.Type == PolylineType.Start); var end = lines.FirstOrDefault(l => l.Type == PolylineType.End); if (start == null) { Debug.LogWarning("No starting PolyLine defined!"); } if (end == null) { Debug.LogWarning("No end PolyLine defined!"); } IEnumerable <Polyline> connected = new List <Polyline>(); if (!blockedLines.Contains(start)) // consider special case when start line is already blocked as well { connected = PolylineService.CalculateConnected(start, lines.Except(blockedLines)); // TODO: do not calculate each frame } var newNumConnectedLines = connected.Count(); if (numConnectedLines != newNumConnectedLines) { OnNumConnectedChanged(newNumConnectedLines); numConnectedLines = newNumConnectedLines; } // update connected and unconnected lines, send events foreach (var l in lines.Except(connected)) { if (l.Connected) { OnConnectionRemoved(); } l.Connected = false; } foreach (var c in connected) { if (!c.Connected) { OnConnectionAdded(); } c.Connected = true; } // check win condition and spawn level complete overlay if (end != null && end.Connected) { Shader.SetGlobalFloat("_LevelComplete", 1); endConnectedCounter += Time.deltaTime; if (endConnectedCounter > 1) { gameState = GameState.Won; OnLevelComplete(); // force disable grab input var im = transform.Find("/GameManager").GetComponent <InputManager>(); im.Disable(); // load and display level complete overlay var p = Resources.Load <GameObject>("Prefabs/LevelCompleteOverlay"); var levelCompleteOverlay = GameObject.Instantiate(p, GameObject.Find("IngameUI").transform); var continueButton = levelCompleteOverlay.transform.FindDeepComponent <Button>("ContinueButton"); continueButton.onClick.AddListener(() => { LoadNextLevel(); }); } } else { endConnectedCounter = 0; Shader.SetGlobalFloat("_LevelComplete", 0); } }
public PolylineController() { this.polylineService = new PolylineService(); }