protected virtual void OnPlayerMoved(object sender, MovementEventArgs e) { CheckCollisionWithCoins(); CheckCollisionWithEnemies(); var portal = _gameBoard.Elements.OfType <Portal>().FirstOrDefault(p => _movementChecker.CheckCollision(p, _player)); _player.MoveViaPortal(portal); if (!_gameBoard.Elements.OfType <Coin>().Any()) { Difficulty++; FillBoardWithCoins(); DifficultyUp(); } }
private async Task CheckEnemiesMovement(bool result, double[] positions) { await GameBoardHelper.StartStaTask(() => { _gameBoard = GameBoardHelper.GenerateEmptyGameBoard(_width, _height); IGameMovementChecker checker = GameBoardHelper.GetGameMovementChecker(_gameBoard); ISettingsProvider provider = new TestSettingsProvider(); IGraph graph = new Graph(_gameBoard); var player = new Mock <Player>(); player.Object.X = positions[0]; player.Object.Y = positions[1]; var alg = new AStarEnemyMovementAlgorithm(graph, player.Object, (int)_width, (int)_height); var e1 = new Enemy(); e1.X = positions[2]; e1.Y = positions[3]; e1.MovementAlgorithm = alg; var e2 = new Enemy(); e2.X = positions[4]; e2.Y = positions[5]; e2.MovementAlgorithm = alg; _gameBoard.Children.Add(e1); _gameBoard.Children.Add(e2); IEnemyMovementManager manager = new TimeEnemyMovementManager(new [] { e1, e2 }, checker, provider); manager.MoveEnemies(); bool res = checker.CheckCollision(e1, e2); Assert.Equal(result, res); }); }
/// <summary> /// Looks for collisions between the given element and the set of other elements. /// </summary> /// <param name="checker">The IGameMovementChecker instance.</param> /// <param name="element">The element for testing collisions.</param> /// <param name="obstacles">Another elements to test collision with the given element.</param> /// <param name="checkElementInObstacles">Specifies whether the testing element is in the given collection of obstacles. /// If true it wouldn't be testet for collision.</param> /// <returns>True if the element has collision with at least one from the given obstacles, otherwise false.</returns> public static bool CheckCollision(this IGameMovementChecker checker, IGameElement element, IEnumerable <IGameElement> obstacles, bool checkElementInObstacles) { if (checker == null) { throw new NullReferenceException(nameof(checker)); } if (element == null || obstacles == null) { return(false); } IList <IGameElement> obst = obstacles.ToList(); if (obst.Contains(element)) { obst.Remove(element); } return(obst.Aggregate(true, (current, gameElement) => current && checker.CheckCollision(gameElement, element))); }
/// <summary> /// Looks for collisions between the given element and the set of other elements. /// </summary> /// <param name="checker">The IGameMovementChecker instance.</param> /// <param name="element">The element for testing collisions.</param> /// <param name="obstacles">Another elements to test collision with the given element.</param> /// <returns>True if the element has collision with at least one from the given obstacles, otherwise false.</returns> public static bool CheckCollision(this IGameMovementChecker checker, IGameElement element, IEnumerable <IGameElement> obstacles) { return(checker.CheckCollision(element, obstacles, false)); }