Exemplo n.º 1
0
 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);
     });
 }
Exemplo n.º 2
0
 /// <summary>
 /// Loads the game state and starts the game.
 /// </summary>
 /// <param name="state">The object stroing the state of the game.</param>
 public void Load(GameState state)
 {
     _movementChecker  = GameMovementCheckerFactory.Instance.CreateUpdateChecker(_gameBoard);
     Points            = state?.Points ?? 0;
     Difficulty        = state?.Difficulty ?? 1;
     Lifes             = state?.Lifes ?? 3;
     Timer             = _builder.BuildTimer(state);
     _player           = _gameBoard.Children.OfType <Player>().Single();
     _player.Moved    += OnPlayerMoved;
     _player.Direction = state?.PlayerDirection ?? Direction.Left;
     LoadCoinsPositions();
     ConfigureEnemies();
     SetCoinsColleted();
     ConfigureLifesGeneratior();
 }
Exemplo n.º 3
0
        /// <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)));
        }
Exemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of TimeEnemyMovementManager.
 /// </summary>
 /// <param name="enemies">The collection of the enemies.</param>
 /// <param name="movementChecker">The IGameMovementChecker for checking collision between elements.</param>
 /// <param name="provider">An object that provide some game setting values.</param>
 public TimeEnemyMovementManager(IEnumerable <Enemy> enemies, IGameMovementChecker movementChecker, ISettingsProvider provider)
 {
     if (movementChecker == null)
     {
         throw new ArgumentNullException(nameof(movementChecker));
     }
     _movementChecker = movementChecker;
     if (provider == null)
     {
         throw new ArgumentNullException(nameof(provider));
     }
     _provider = provider;
     if (enemies == null)
     {
         enemies = new List <Enemy>();
     }
     Enemies          = enemies;
     MovementInterval = TimeSpan.FromMilliseconds(_provider.EnemyMovementInterval);
     _timer           = new DispatcherTimer();
     _timer.Interval  = MovementInterval;
     _timer.Tick     += (x, e) => ((IEnemyMovementManager)this).MoveEnemies();
 }
Exemplo n.º 5
0
 /// <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));
 }