/// <summary> /// Converts a string from the 'pretty' spaced version to a tight no-whitespaced version (and vice versa) /// </summary> /// <param name="dir">Add or remove the whitespace</param> /// <param name="ipToConvert">The string you want to convert</param> /// <returns>Converted string</returns> private string convert(ConvertDirection dir, string ipToConvert) { if (ipToConvert == null) { return(""); } string[] ip = ipToConvert.Split(_octetSplitter); string finalIP = ""; try { if (dir == ConvertDirection.AddWhitespace) // Add whitespace when you're setting pushing an IP against the mask { for (int i = 0; i < _spaceForEachOctet - 2; i++) { finalIP += ip[i].PadLeft(_spaceForEachOctet - 2, ' '); } } else { for (int i = 0; i < ip.Length; i++) // Remove whitespace when you're exposing the IP to external classes { finalIP += ip[i].Trim() + "."; } finalIP = finalIP.Length > 0 ? finalIP.Substring(0, finalIP.Length - 1) : finalIP; // Get rid of the last period (when applicable) } } catch { } return(finalIP); }
/// <summary> /// Aktualizuje stan gry. /// </summary> /// <param name="checkInput">Określa, czy metoda ma obłsużyć wejście.</param> public void Update(bool checkInput) { bool updateDirection = false; Directions newDirection = Directions.Up; if (checkInput) { // Obsługa wejścia. switch (currentKey) { case ConsoleKey.UpArrow: updateDirection = true; newDirection = Directions.Up; break; case ConsoleKey.DownArrow: updateDirection = true; newDirection = Directions.Down; break; case ConsoleKey.LeftArrow: updateDirection = true; newDirection = Directions.Left; break; case ConsoleKey.RightArrow: updateDirection = true; newDirection = Directions.Right; break; default: break; } } // Aktualizacja obiektów gry. foreach (GameObject gameObject in gameObjects) { // Aktualizacja węży. if (gameObject is Snake) { Snake snake = gameObject as Snake; // Obsługa zmiany kierunku. if (updateDirection) { Vector2Int zero = new Vector2Int(0, 0); if (ConvertDirection.ToVector2Int(newDirection) + ConvertDirection.ToVector2Int(snake.Direction) != zero) { snake.UpdatedDirection = newDirection; snake.DirectionUpdateWaiting = true; } } // Obsługa zmiany kierunku, ruchu, zebrania bonusu i kolizji. double secondsPerMove = (1D / snake.Speed); int timesToMove = (int)(snake.speedTimer.TimeElapsed.TotalSeconds / secondsPerMove); if (timesToMove > 0) { snake.UpdateDirection(); snake.speedTimer.SubtractTimeElapsed(TimeSpan.FromSeconds(timesToMove * secondsPerMove)); for (int i = 0; i < timesToMove; i++) { Vector2Int futureHead = snake.Points[0] + ConvertDirection.ToVector2Int(snake.Direction); if (futureHead.X >= 0 && futureHead.Y >= 0 && futureHead.X < level.Size.X && futureHead.Y < level.Size.Y) { PointTypes pointType = level.GetPointType(futureHead); if (pointType == PointTypes.Empty) { // Zwykły ruch. snake.Move(); level.UpdateLevel(gameObjects); } else if (pointType == PointTypes.Bonus) { // Ruch z zebraniem bonusu i urośnięciem węża. Bonuses bonuses = level.WhatObjectIsHere(futureHead, gameObjects) as Bonuses; snake.Move(true); bonuses.DeleteBonus(futureHead); level.UpdateLevel(gameObjects); bonuses.SpawnBonus(level.PickAnEmptyPoint()); level.UpdateLevel(gameObjects); Score++; scoreBar.UpdateScores(Score); } else { // Kolizja z wężem. InGame = false; } } else { // Kolizja z granicą levelu. InGame = false; } } } } } }