private void CompleteGame() { var score = Snake.Count - 1; // TODO: Affected by Move() logic AchivementsManager.GameCompleted(score); if (Profile.Scores.ContainsKey(Drake)) { if (score > Profile.Scores[Drake]) { Profile.Scores[Drake] = score; } } else { Profile.Scores.Add(Drake, score); } Profile.Save(); State = GameState.Completed; Engine.AudioPlayer.PlayPop(); Engine.PanelManager.HideAchievementUnlockedPanel(); Engine.PanelManager.ShowGameOver(); Engine.TaskScheduler.CreateTask(Engine.AudioPlayer.PlayStageFailed, 0.5f); Engine.TaskScheduler.CreateTask(() => TweenAlpha.Begin(Engine.Interface.FillerSprite, 1, 0), 0.5f); Engine.Field.RePaint(new Color(1, 1, 1, 0), Snake, Apple); }
private void Move() { if (State == GameState.Running || State == GameState.Paused) { FindObjectOfType <TaskScheduler>().CreateTask(Move, DeltaTime); } if (State != GameState.Running) { return; } var direction = DirectionNext.Count > 0 ? DirectionNext[0] : DirectionPrev; if (direction + DirectionPrev == Vector2.zero) { direction = DirectionPrev; } var head = Snake[0] + direction; var tail = Snake[Snake.Count - 1]; if (head.x < 0 || head.x >= (int)Engine.Field.Size.x || head.y < 0 || head.y >= (int)Engine.Field.Size.y || Snake.Contains(head)) { Snake.Add(tail); CompleteGame(); for (var i = 0; i < Snake.Count; i++) { var index = i; var sprite = Engine.Field.Sprites[(int)Snake[index].x, (int)Snake[index].y]; Engine.TaskScheduler.CreateTask(() => { TweenAlpha.Begin(sprite.gameObject, 0.25f, 0.4f); CreateSparks(Snake[index], SnakeColor); }, (i + 1) * 0.05f); } return; } Snake.Reverse(); Snake.Add(head); Snake.Reverse(); if (head != Apple) { Snake.Remove(tail); } TweenColor.Begin(Engine.Field.Sprites[(int)head.x, (int)head.y].gameObject, DeltaTime, SnakeColor); if (head != tail) { TweenColor.Begin(Engine.Field.Sprites[(int)tail.x, (int)tail.y].gameObject, DeltaTime, Engine.Field.Color); } DirectionPrev = direction; if (DirectionNext.Count > 0) { DirectionNext.RemoveAt(0); } if (head == Apple) { CreateApple(); Engine.Interface.UpdateScore(); var up = false; for (var i = 0; i < Targets.Length; i++) { if (Snake.Count != Targets[i]) { continue; } DeltaTime = DeltaTimes[i]; _fillAmount = Mathf.Max(0, 1 - Snake.Count / (float)PassTarget); up = true; } if (up) { Engine.AudioPlayer.PlayUp(); } else { Engine.AudioPlayer.PlayAppleEaten(); } CreateSparks(head, up ? GreenAppleColor : AppleColor); AchivementsManager.AppleEaten(Snake.Count); if (Snake.Count == Targets[Targets.Length - 3]) { Engine.TaskScheduler.CreateTask(Engine.AudioPlayer.PlayStageCompleted, 0.5f); } } }