/// <summary>
        /// Changes the ghost movement to chase.
        /// </summary>
        /// <param name="ghost">Instance of the ghost whose movement
        /// will be changed.</param>
        private void SwitchChaseMode(GameObject ghost)
        {
            MoveComponent moveComponent = ghost.GetComponent <MoveComponent>();

            switch (ghost.Name)
            {
            case "blinky":
                moveComponent.AddMovementBehaviour(
                    new BlinkyChaseBehaviour(
                        ghost,
                        ghost.GetComponent <MapComponent>(),
                        pacman.GetComponent <MapTransformComponent>(),
                        ghost.GetComponent <MapTransformComponent>(),
                        3));
                break;

            case "pinky":
                moveComponent.AddMovementBehaviour(
                    new PinkyChaseBehaviour(
                        pacmanMovementBehaviour,
                        ghost,
                        map,
                        pacman.GetComponent <MapTransformComponent>(),
                        ghost.GetComponent <MapTransformComponent>(),
                        3));
                break;

            case "inky":
                MapTransformComponent blinkyMapTransform =
                    ghosts.FirstOrDefault(g => g.Name == "blinky")?.
                    GetComponent <MapTransformComponent>();

                moveComponent.AddMovementBehaviour(
                    new InkyChaseBehaviour(
                        pacmanMovementBehaviour,
                        map,
                        pacman.GetComponent <MapTransformComponent>(),
                        blinkyMapTransform,
                        ghost,
                        ghost.GetComponent <MapTransformComponent>(),
                        3));
                break;

            case "clyde":
                moveComponent.AddMovementBehaviour(
                    new ClydeChaseBehaviour(
                        pacmanMovementBehaviour,
                        ghost,
                        map,
                        pacman.GetComponent <MapTransformComponent>(),
                        ghost.GetComponent <MapTransformComponent>(),
                        3));
                break;
            }
        }
        /// <summary>
        /// Changes the ghost movement to scatter.
        /// </summary>
        /// <param name="ghost">Instance of the ghost whose movement
        /// will be changed.</param>
        private void SwitchScatterMode(GameObject ghost)
        {
            MoveComponent moveComponent = ghost.GetComponent <MoveComponent>();
            int           xMax          = map.Map.GetLength(0);
            int           yMax          = map.Map.GetLength(1);

            switch (ghost.Name)
            {
            case "blinky":
                moveComponent.AddMovementBehaviour(
                    new ScatterMovementBehaviour(
                        ghost,
                        ghost.GetComponent <MapComponent>(),
                        new MapTransformComponent(0, 0),
                        ghost.GetComponent <MapTransformComponent>(),
                        3));
                break;

            case "pinky":
                moveComponent.AddMovementBehaviour(
                    new ScatterMovementBehaviour(
                        ghost,
                        ghost.GetComponent <MapComponent>(),
                        new MapTransformComponent(xMax, 0),
                        ghost.GetComponent <MapTransformComponent>(),
                        3));
                break;

            case "inky":
                moveComponent.AddMovementBehaviour(
                    new ScatterMovementBehaviour(
                        ghost,
                        ghost.GetComponent <MapComponent>(),
                        new MapTransformComponent(0, yMax),
                        ghost.GetComponent <MapTransformComponent>(),
                        3));
                break;

            case "clyde":
                moveComponent.AddMovementBehaviour(
                    new ScatterMovementBehaviour(
                        ghost,
                        ghost.GetComponent <MapComponent>(),
                        new MapTransformComponent(xMax, yMax),
                        ghost.GetComponent <MapTransformComponent>(),
                        3));
                break;
            }
        }
        /// <summary>
        /// Happens once when there's a collision with a ghost.
        /// </summary>
        private void GhostCollision(GameObject ghost)
        {
            switch (ghost.GetComponent <MoveComponent>().MovementState)
            {
            case MovementState.Chase:
            case MovementState.Scatter:
                OnGhostChaseCollision();
                break;

            case MovementState.Frightened:
                ResetTimer(frightenedModeTime);
                ConsoleSprite consoleSprite =
                    ghost.GetComponent <ConsoleSprite>();

                consoleSprite.ChangeColor(
                    ConsoleColor.Red, ConsoleColor.DarkBlue);

                MoveComponent moveComponent =
                    ghost.GetComponent <MoveComponent>();
                InvertGhostDirection(ghost);
                moveComponent.MovementState = MovementState.Eaten;
                moveComponent.AddMovementBehaviour(
                    new BlinkyChaseBehaviour(
                        ghost,
                        map,
                        new MapTransformComponent(13, 11),
                        ghost.GetComponent <MapTransformComponent>(),
                        3));
                break;
            }
        }
        /// <summary>
        /// Method that defines what happens when a ghost is inside the midle
        /// house.
        /// </summary>
        /// <param name="ghost">Ghost to check.</param>
        private void GhostOnHouse(GameObject ghost)
        {
            MoveComponent moveComponent = ghost.GetComponent <MoveComponent>();

            moveComponent.AddMovementBehaviour(
                new BlinkyChaseBehaviour(
                    ghost,
                    ghost.GetComponent <MapComponent>(),
                    new MapTransformComponent(13, 11),
                    ghost.GetComponent <MapTransformComponent>(),
                    3));
            ChangeSpriteColor(ghost);
            moveComponent.MovementState = MovementState.OutGhostHouse;
        }
        /// <summary>
        /// Changes the ghost movement to frightened.
        /// </summary>
        /// <param name="ghost">Instance of the ghost whose movement
        /// will be changed.</param>
        private void SwitchFrightenedMode(GameObject ghost)
        {
            MoveComponent moveComponent =
                ghost.GetComponent <MoveComponent>();

            MapTransformComponent ghostMapTransform =
                ghost.GetComponent <MapTransformComponent>();

            moveComponent.AddMovementBehaviour(
                new FrightenedMovementBehaviour(
                    ghost,
                    map,
                    new MapTransformComponent(
                        1, 1),
                    ghostMapTransform,
                    random,
                    3));
        }
Example #6
0
        /// <summary>
        /// Method responsible for creating every gameobject.
        /// </summary>
        private void CreateGameObjects()
        {
            keyReader.QuitKeys.Add(ConsoleKey.Enter);
            sceneChanger = new GameObject("Scene Changer");
            SceneChangerComponent sceneChangerComponent =
                new SceneChangerComponent(sceneHandler);

            sceneChanger.AddComponent(sceneChangerComponent);
            sceneChangerComponent.SceneToLoad = "LevelScene";

            ////////////////////////////////////////////////////////////////////

            newLevelCreator = new GameObject("New Level Creator");

            CreateNewLevelComponent createLevel =
                new CreateNewLevelComponent(keyReader, sceneHandler, random);

            newLevelCreator.AddComponent(createLevel);

            ////////////////////////////////////////////////////////////////////

            pacmanLogo = new GameObject("Pacman Logo");
            ConsolePixel logoPixel = new ConsolePixel(
                ' ', ConsoleColor.White, ConsoleColor.DarkYellow);

            Dictionary <Vector2Int, ConsolePixel> logoPixels =
                new Dictionary <Vector2Int, ConsolePixel>();

            ICollection <Vector2Int[]> positionsList = new List <Vector2Int[]>();

            CreatePacmanSprite(positionsList);

            foreach (Vector2Int[] v in positionsList)
            {
                for (int i = v[0].X; i < v[1].X + 1; i++)
                {
                    for (int j = v[0].Y; j < v[1].Y + 1; j++)
                    {
                        logoPixels[new Vector2Int(i, j)] = logoPixel;
                    }
                }
            }

            pacmanLogo.AddComponent(new TransformComponent(20, 1));
            pacmanLogo.AddComponent(new ConsoleSprite(logoPixels));

            ////////////////////////////////////////////////////////////////////

            selector = new GameObject("Selector");
            char[,] selectorSprite = { { '-' }, { '-' }, { '>' }, };

            MapComponent  map = new MapComponent(XSIZE, YSIZE);
            MoveComponent selectorMovement = new MoveComponent();

            selector.AddComponent(keyReader);
            selector.AddComponent(new TransformComponent(2, 34));
            selector.AddComponent(selectorMovement);
            selector.AddComponent(map);
            selector.AddComponent(new ConsoleSprite(
                                      selectorSprite,
                                      ConsoleColor.White,
                                      ConsoleColor.DarkBlue));
            SelectorMovementBehaviour selectorMovementBehaviour =
                new SelectorMovementBehaviour(
                    selector,
                    sceneChanger.
                    GetComponent <SceneChangerComponent>());

            // Adds a movement behaviour
            selectorMovement.AddMovementBehaviour(selectorMovementBehaviour);

            ////////////////////////////////////////////////////////////////////

            startText = new GameObject("Start Game");
            startText.AddComponent(new TransformComponent(6, 34));

            RenderableStringComponent renderStartGame
                = new RenderableStringComponent(
                      () => "START GAME",
                      i => new Vector2Int(i, 0),
                      ConsoleColor.Yellow,
                      ConsoleColor.DarkBlue);

            startText.AddComponent(renderStartGame);

            ////////////////////////////////////////////////////////////////////

            quitText = new GameObject("Quit");
            quitText.AddComponent(new TransformComponent(6, 36));

            RenderableStringComponent renderQuit
                = new RenderableStringComponent(
                      () => "QUIT",
                      i => new Vector2Int(i, 0),
                      ConsoleColor.White,
                      ConsoleColor.DarkBlue);

            quitText.AddComponent(renderQuit);

            ////////////////////////////////////////////////////////////////////

            highScoreText = new GameObject("HighScore Text");

            highScoreText.AddComponent(new TransformComponent(6, 28));

            RenderableStringComponent renderHighScoreText
                = new RenderableStringComponent(
                      () => $"HighScore:",
                      i => new Vector2Int(i, 0),
                      ConsoleColor.White,
                      ConsoleColor.DarkBlue);

            highScoreText.AddComponent(renderHighScoreText);

            ////////////////////////////////////////////////////////////////////

            highScoreNumberText = new GameObject("HighScore Number Text");

            HighScoreComponent highScoreComponent =
                new HighScoreComponent();

            RenderableStringComponent renderHighScoreNumberText
                = new RenderableStringComponent(
                      () => $"{highScoreComponent.HighScore}",
                      i => new Vector2Int(i, 0),
                      ConsoleColor.White,
                      ConsoleColor.DarkBlue);

            highScoreNumberText.AddComponent(new TransformComponent(6, 30));
            highScoreNumberText.AddComponent(renderHighScoreNumberText);
            highScoreNumberText.AddComponent(highScoreComponent);

            ////////////////////////////////////////////////////////////////////

            controlsText = new GameObject("ControlsText");
            controlsText.AddComponent(new TransformComponent(70, 19));

            RenderableStringComponent renderControls
                = new RenderableStringComponent(
                      () => "Controls:",
                      i => new Vector2Int(i, 0),
                      ConsoleColor.White,
                      ConsoleColor.DarkBlue);

            controlsText.AddComponent(renderControls);

            ////////////////////////////////////////////////////////////////////

            movementText = new GameObject("MovementText");
            movementText.AddComponent(new TransformComponent(54, 21));

            RenderableStringComponent renderMovement
                = new RenderableStringComponent(
                      () => "Movement Keys: W, A, S, D",
                      i => new Vector2Int(i, 0),
                      ConsoleColor.White,
                      ConsoleColor.DarkBlue);

            movementText.AddComponent(renderMovement);

            ////////////////////////////////////////////////////////////////////

            actionsText = new GameObject("ActionsText");
            actionsText.AddComponent(new TransformComponent(50, 22));

            RenderableStringComponent renderActions
                = new RenderableStringComponent(
                      () => "Confirm: Enter , Quit: Escape",
                      i => new Vector2Int(i, 0),
                      ConsoleColor.White,
                      ConsoleColor.DarkBlue);

            actionsText.AddComponent(renderActions);

            ////////////////////////////////////////////////////////////////////

            rules = new GameObject("RulesText");
            rules.AddComponent(new TransformComponent(73, 24));

            RenderableStringComponent renderRules
                = new RenderableStringComponent(
                      () => "Rules:",
                      i => new Vector2Int(i, 0),
                      ConsoleColor.White,
                      ConsoleColor.DarkBlue);

            rules.AddComponent(renderRules);

            ////////////////////////////////////////////////////////////////////

            rule1 = new GameObject("Rule1");
            rule1.AddComponent(new TransformComponent(52, 26));

            RenderableStringComponent renderRule1
                = new RenderableStringComponent(
                      () => "Pacman must run from ghosts",
                      i => new Vector2Int(i, 0),
                      ConsoleColor.White,
                      ConsoleColor.DarkBlue);

            rule1.AddComponent(renderRule1);

            ////////////////////////////////////////////////////////////////////

            rule2 = new GameObject("Rule2");
            rule2.AddComponent(new TransformComponent(33, 27));

            RenderableStringComponent renderRule2
                = new RenderableStringComponent(
                      () => "Pacman can pick power pills and eat the ghosts",
                      i => new Vector2Int(i, 0),
                      ConsoleColor.White,
                      ConsoleColor.DarkBlue);

            rule2.AddComponent(renderRule2);

            ////////////////////////////////////////////////////////////////////

            rule3 = new GameObject("Rule3");
            rule3.AddComponent(new TransformComponent(37, 28));

            RenderableStringComponent renderRule3
                = new RenderableStringComponent(
                      () => "Pacman must eat every food to end the game",
                      i => new Vector2Int(i, 0),
                      ConsoleColor.White,
                      ConsoleColor.DarkBlue);

            rule3.AddComponent(renderRule3);

            ////////////////////////////////////////////////////////////////////

            icons = new GameObject("Icons");
            icons.AddComponent(new TransformComponent(73, 30));

            RenderableStringComponent renderIcons
                = new RenderableStringComponent(
                      () => "Icons:",
                      i => new Vector2Int(i, 0),
                      ConsoleColor.White,
                      ConsoleColor.DarkBlue);

            icons.AddComponent(renderIcons);

            ////////////////////////////////////////////////////////////////////

            icon1 = new GameObject("Icon1");
            icon1.AddComponent(new TransformComponent(68, 32));

            RenderableStringComponent renderIcon1
                = new RenderableStringComponent(
                      () => "P -> Pacman",
                      i => new Vector2Int(i, 0),
                      ConsoleColor.White,
                      ConsoleColor.DarkBlue);

            icon1.AddComponent(renderIcon1);

            ////////////////////////////////////////////////////////////////////

            icon2 = new GameObject("Icon2");
            icon2.AddComponent(new TransformComponent(54, 33));

            RenderableStringComponent renderIcon2
                = new RenderableStringComponent(
                      () => "Colored Squares -> Ghosts",
                      i => new Vector2Int(i, 0),
                      ConsoleColor.White,
                      ConsoleColor.DarkBlue);

            icon2.AddComponent(renderIcon2);

            ////////////////////////////////////////////////////////////////////

            icon3 = new GameObject("Icon3");
            icon3.AddComponent(new TransformComponent(68, 34));

            RenderableStringComponent renderIcon3
                = new RenderableStringComponent(
                      () => "F -> Fruits",
                      i => new Vector2Int(i, 0),
                      ConsoleColor.White,
                      ConsoleColor.DarkBlue);

            icon3.AddComponent(renderIcon3);

            ////////////////////////////////////////////////////////////////////

            icon4 = new GameObject("Icon4");
            icon4.AddComponent(new TransformComponent(62, 35));

            RenderableStringComponent renderIcon4
                = new RenderableStringComponent(
                      () => "PUP -> Power Pill",
                      i => new Vector2Int(i, 0),
                      ConsoleColor.White,
                      ConsoleColor.DarkBlue);

            icon4.AddComponent(renderIcon4);

            ////////////////////////////////////////////////////////////////////

            icon5 = new GameObject("Icon5");
            icon5.AddComponent(new TransformComponent(70, 36));

            RenderableStringComponent renderIcon5
                = new RenderableStringComponent(
                      () => ". -> Food",
                      i => new Vector2Int(i, 0),
                      ConsoleColor.White,
                      ConsoleColor.DarkBlue);

            icon5.AddComponent(renderIcon5);
        }