Example #1
0
        private void MovePlayer(KeyboardEventArgs keyPressed)
        {
            Movement playerMovement = Movement.CreateFromDirection(directionKeys[keyPressed.Code], Player.Speed);

            if (Player.CanMove(playerMovement, ElementMap.GetCloseElements(Player)))
            {
                Player.Move(playerMovement);
            }
        }
Example #2
0
        public void MakeMove(KeyboardEventArgs keyPressed)
        {
            if (GameState != GameState.Playing)
            {
                return;
            }

            //move with enemies
            MoveEnemy();

            // process pressed keys
            if (keyPressed != null)
            {
                if (directionKeys.ContainsKey(keyPressed.Code))
                {
                    MovePlayer(keyPressed);
                }
                if (keyPressed.Code == "Space")
                {
                    AddBomb();
                }
            }

            // check if enemy destroys player
            if (EnemyEatPlayer())
            {
                Player.isDead = true;
                ChangeGameState(GameState.Lose);
            }

            // check if explosion destroys player
            if (ExplosionDestroyPlayer())
            {
                Player.isDead = true;
                ChangeGameState(GameState.Lose);
            }

            // check if explosion destroys enemy
            ExplosionDestroyEnemy();

            // explosion and bombs ticking
            ProcessBombs();
            ProcessExplosions();

            // if all enemies are dead and Finish is not set create Finish
            if (Enemies.Count == 0 && Finish == null)
            {
                (int x, int y) = ElementMap.GetFinishPosition();
                Finish         = new Finish(x, y);
            }

            // if there is finish check if player is on it
            if (Finish != null && Finish.IntersectWith(Player))
            {
                ChangeGameState(GameState.Win);
            }
        }
Example #3
0
 private void MoveEnemy()
 {
     foreach (var enemy in Enemies)
     {
         if (enemy.Movement != null && enemy.CanMove(enemy.Movement, ElementMap.GetCloseElements(enemy)))
         {
             enemy.Move(enemy.Movement);
         }
         else
         {
             enemy.Movement = Movement.CreateRandomMovement(enemy.Speed);
         }
     }
 }
Example #4
0
        private void CreateMap(char[][] charMap)
        {
            ElementMap = new ElementMap(charMap[0].Length, charMap.Length);

            for (int i = 0; i < charMap.Length; i++)
            {
                for (int j = 0; j < charMap[i].Length; j++)
                {
                    char c = charMap[i][j];
                    switch (c)
                    {
                    case 'X':
                        ElementMap.AddElement(new Wall(j, i));
                        break;

                    case 'B':
                        ElementMap.AddElement(new Box(j, i));
                        break;

                    case 'P':
                        ElementMap.AddElement(new Grass(j, i));
                        Player = new Player(j, i);
                        break;

                    case 'E':
                        ElementMap.AddElement(new Grass(j, i));
                        Enemies.Add(new Enemy(j, i));
                        break;

                    case '.':
                        ElementMap.AddElement(new Grass(j, i));
                        break;

                    default:
                        throw new Exception($"Unknow character '{c}' in map");
                    }
                }
            }
        }
Example #5
0
 private void CreateExplosion(Bomb bomb)
 {
     Explosions.AddRange(ElementMap.CreateExplosionForBomb(bomb));
 }