Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates the first Level GameObjects.
        /// </summary>
        private void CreateLevel()
        {
            // Create scene
            ConsoleKey[] quitKeys = new ConsoleKey[] { ConsoleKey.Escape };
            gameScene = new Scene(
                xdim,
                ydim,
                new InputHandler(quitKeys),
                new ConsoleRenderer(xdim, ydim, new ConsolePixel(' ')),
                new CollisionHandler(xdim, ydim));

            // Create walls
            GameObject   walls     = new GameObject("Walls");
            ConsolePixel wallPixel = new ConsolePixel(
                ' ', ConsoleColor.Blue, ConsoleColor.DarkGray);
            Dictionary <Vector2, ConsolePixel> wallPixels =
                new Dictionary <Vector2, ConsolePixel>();

            for (int x = 0; x < xdim; x++)
            {
                // Ground and walls
                if ((x > 0 && x < 25) ||
                    (x > 30 && x < 127) ||
                    (x > 135 && x < xdim))
                {
                    wallPixels[new Vector2(x, ydim - 1)] = wallPixel;
                    occupied.Add(new Vector2(x, ydim - 1));
                    wallPixels[new Vector2(x, ydim - 2)] = wallPixel;
                    occupied.Add(new Vector2(x, ydim - 2));
                    wallPixels[new Vector2(x, ydim - 3)] = wallPixel;
                    occupied.Add(new Vector2(x, ydim - 3));
                    wallPixels[new Vector2(x, ydim - 4)] = wallPixel;
                    occupied.Add(new Vector2(x, ydim - 4));
                    wallPixels[new Vector2(x, ydim - 5)] = wallPixel;
                    occupied.Add(new Vector2(x, ydim - 5));
                    wallPixels[new Vector2(x, ydim - 6)] = wallPixel;
                    occupied.Add(new Vector2(x, ydim - 6));
                    wallPixels[new Vector2(x, ydim - 7)] = wallPixel;
                    occupied.Add(new Vector2(x, ydim - 7));
                }

                // Platform
                if (x > 30 && x < 45)
                {
                    wallPixels[new Vector2(x, ydim - 18)] = wallPixel;
                    occupied.Add(new Vector2(x, ydim - 18));
                }
            }

            for (int y = 0; y < ydim; y++)
            {
                wallPixels[new Vector2(0, y)] = wallPixel;
                occupied.Add(new Vector2(0, y));
                wallPixels[new Vector2(xdim - 1, y)] = wallPixel;
                occupied.Add(new Vector2(xdim - 1, y));
            }

            // Create obstacles
            GameObject   obstacle      = new GameObject("Obstacle");
            ConsolePixel obstaclePixel = new ConsolePixel(
                ' ', ConsoleColor.Blue, ConsoleColor.Green);
            Dictionary <Vector2, ConsolePixel> obstaclePixels =
                new Dictionary <Vector2, ConsolePixel>();

            obstaclePixels[new Vector2(49, 19)] = obstaclePixel;
            occupied.Add(new Vector2(49, 19));

            obstaclePixels[new Vector2(52, 19)] = obstaclePixel;
            occupied.Add(new Vector2(52, 19));

            obstaclePixels[new Vector2(50, 20)] = obstaclePixel;
            occupied.Add(new Vector2(50, 20));

            obstaclePixels[new Vector2(50, 21)] = obstaclePixel;
            occupied.Add(new Vector2(50, 21));

            obstaclePixels[new Vector2(50, 19)] = obstaclePixel;
            occupied.Add(new Vector2(50, 19));

            obstaclePixels[new Vector2(50, 22)] = obstaclePixel;
            occupied.Add(new Vector2(50, 22));

            obstaclePixels[new Vector2(51, 20)] = obstaclePixel;
            occupied.Add(new Vector2(51, 20));

            obstaclePixels[new Vector2(51, 21)] = obstaclePixel;
            occupied.Add(new Vector2(51, 21));

            obstaclePixels[new Vector2(51, 19)] = obstaclePixel;
            occupied.Add(new Vector2(51, 19));

            obstaclePixels[new Vector2(51, 22)] = obstaclePixel;
            occupied.Add(new Vector2(51, 22));

            walls.AddComponent(new ConsoleSprite(wallPixels));
            walls.AddComponent(new Position(0, 0, 1));
            gameScene.AddGameObject(walls);

            obstacle.AddComponent(new ConsoleSprite(obstaclePixels));
            obstacle.AddComponent(new Position(0, 0, 0));
            gameScene.AddGameObject(obstacle);

            // Create game object for showing score
            GameObject score = new GameObject("Score");

            score.AddComponent(new Position(1, 0, 10));
            score.AddComponent(new Score());
            RenderableStringComponent visualScore = new RenderableStringComponent(
                () => "Score: " + 3000.ToString(),
                i => new Vector2(i, 0),
                ConsoleColor.DarkMagenta,
                ConsoleColor.White);

            score.AddComponent(visualScore);
            gameScene.AddGameObject(score);

            // Create Coin Sprite
            char[,] coinSprite =
            {
                { '█' },
            };

            // Coin 1
            GameObject coin1 = new GameObject("Coin1");

            coin1.AddComponent(new ConsoleSprite(coinSprite));
            coin1.AddComponent(new Position(80, 19, 0f));
            coin1.AddComponent(new CoinConfirmation());
            gameScene.AddGameObject(coin1);
            coins.Add(coin1);

            // Coin 2
            GameObject coin2 = new GameObject("Coin2");

            coin2.AddComponent(new ConsoleSprite(coinSprite));
            coin2.AddComponent(new Position(35, 8, 0f));
            coin2.AddComponent(new CoinConfirmation());
            gameScene.AddGameObject(coin2);
            coins.Add(coin2);

            // Box sprite
            char[,] boxSprite =
            {
                { '█', '█', '█', '█' },
                { '█', '?', '?', '█' },
                { '█', '?', '?', '█' },
                { '█', '?', '?', '█' },
                { '█', '?', '?', '█' },
                { '█', '█', '█', '█' },
            };

            // Create Box1
            GameObject box = new GameObject("Box");

            box.AddComponent(new ConsoleSprite(
                                 boxSprite,
                                 ConsoleColor.Yellow,
                                 ConsoleColor.DarkGray));
            box.AddComponent(new Position(100, 8, 0f));
            box.AddComponent(new BoxConfirmation());
            gameScene.AddGameObject(box);
            boxes.Add(box);

            // Create Box2
            GameObject box2 = new GameObject("Box2");

            box2.AddComponent(new ConsoleSprite(
                                  boxSprite,
                                  ConsoleColor.Yellow,
                                  ConsoleColor.DarkGray));
            box2.AddComponent(new Position(150, 8, 0f));
            box2.AddComponent(new BoxConfirmation());
            gameScene.AddGameObject(box2);
            boxes.Add(box2);

            // Create dead text
            GameObject dead = new GameObject("Dead");

            dead.AddComponent(new Position(70, 10, 10));
            RenderableStringComponent deadString = new RenderableStringComponent(
                () => string.Empty,
                i => new Vector2(i, 0),
                ConsoleColor.Red,
                ConsoleColor.Gray);

            dead.AddComponent(deadString);
            gameScene.AddGameObject(dead);

            // Create player object
            // ─▄████▄▄
            // ▄▀█▀▐└─┐
            // █▄▐▌▄█▄┘
            // └▄▄▄▄▄┘
            char[,] playerSprite =
            {
                { '─', '▄', '█', '└' },
                { '▄', '▀', '▄', '▄' },
                { '█', '█', '▐', '▄' },
                { '█', '▀', '▐', '▄' },
                { '█', '▐', '▄', '▄' },
                { '█', '└', '█', '▄' },
                { '▄', '─', '▄', '┘' },
                { '▄', '┐', '┘', ' ' },
            };

            // Create player
            GameObject  player            = new GameObject("Player");
            KeyObserver playerKeyListener = new KeyObserver(
                new ConsoleKey[] {
                ConsoleKey.RightArrow,
                ConsoleKey.Spacebar,
                ConsoleKey.UpArrow,
                ConsoleKey.LeftArrow,
                ConsoleKey.Escape,
            });

            player.AddComponent(playerKeyListener);
            Position playerPos = new Position(1f, 19f, 0f);

            player.AddComponent(playerPos);
            player.AddComponent(new Player(
                                    occupied,
                                    score.GetComponent <Score>(),
                                    boxes,
                                    coins,
                                    dead,
                                    1));
            player.AddComponent(new ConsoleSprite(
                                    playerSprite, ConsoleColor.Red, ConsoleColor.Gray));
            gameScene.AddGameObject(player);

            // Create game object for showing time limit
            GameObject time = new GameObject("Time");

            time.AddComponent(new Position(190, 0, 10));
            time.AddComponent(new Time());
            RenderableStringComponent visualTime = new RenderableStringComponent(
                () => "Time: " + 200.ToString(),
                i => new Vector2(i, 0),
                ConsoleColor.DarkMagenta,
                ConsoleColor.White);

            time.AddComponent(visualTime);
            gameScene.AddGameObject(time);
        }
Exemplo n.º 3
0
        private Game()
        {
            // Create scene
            ConsoleKey[] quitKeys = new ConsoleKey[] { ConsoleKey.Escape };
            gameScene = new Scene(xdim, ydim,
                                  new InputHandler(quitKeys),
                                  new ConsoleRenderer(xdim, ydim, new ConsolePixel('.')),
                                  new CollisionHandler(xdim, ydim));

            // Create quitter object
            GameObject  quitter = new GameObject("Quitter");
            KeyObserver quitSceneKeyListener = new KeyObserver(new ConsoleKey[]
                                                               { ConsoleKey.Escape });

            quitter.AddComponent(quitSceneKeyListener);
            quitter.AddComponent(new Quitter());
            gameScene.AddGameObject(quitter);

            // Create player object
            char[,] playerSprite =
            {
                { '-', '|', '-' },
                { '-', '0', '-' },
                { '-', '|', '-' }
            };
            GameObject  player            = new GameObject("Player");
            KeyObserver playerKeyListener = new KeyObserver(new ConsoleKey[] {
                ConsoleKey.DownArrow,
                ConsoleKey.UpArrow,
                ConsoleKey.RightArrow,
                ConsoleKey.LeftArrow
            });

            player.AddComponent(playerKeyListener);
            Position playerPos = new Position(10f, 10f, 0f);

            player.AddComponent(playerPos);
            player.AddComponent(new Player());
            player.AddComponent(new ConsoleSprite(
                                    playerSprite, ConsoleColor.Red, ConsoleColor.DarkGreen));
            gameScene.AddGameObject(player);

            // Create walls
            GameObject   walls     = new GameObject("Walls");
            ConsolePixel wallPixel = new ConsolePixel(
                '#', ConsoleColor.Blue, ConsoleColor.White);
            Dictionary <Vector2, ConsolePixel> wallPixels =
                new Dictionary <Vector2, ConsolePixel>();

            for (int x = 0; x < xdim; x++)
            {
                wallPixels[new Vector2(x, 0)] = wallPixel;
            }
            for (int x = 0; x < xdim; x++)
            {
                wallPixels[new Vector2(x, ydim - 1)] = wallPixel;
            }
            for (int y = 0; y < ydim; y++)
            {
                wallPixels[new Vector2(0, y)] = wallPixel;
            }
            for (int y = 0; y < ydim; y++)
            {
                wallPixels[new Vector2(xdim - 1, y)] = wallPixel;
            }
            walls.AddComponent(new ConsoleSprite(wallPixels));
            walls.AddComponent(new Position(0, 0, 1));
            gameScene.AddGameObject(walls);

            // Create game object for showing date and time
            GameObject dtGameObj = new GameObject("Time");

            dtGameObj.AddComponent(new Position(xdim / 2 + 1, 0, 10));
            RenderableStringComponent rscDT = new RenderableStringComponent(
                () => DateTime.Now.ToString("F"),
                i => new Vector2(i, 0),
                ConsoleColor.DarkMagenta, ConsoleColor.White);

            dtGameObj.AddComponent(rscDT);
            gameScene.AddGameObject(dtGameObj);

            // Create game object for showing position
            GameObject pos = new GameObject("Position");

            pos.AddComponent(new Position(1, 0, 10));
            RenderableStringComponent rscPos = new RenderableStringComponent(
                () => $"({playerPos.Pos.X}, {playerPos.Pos.Y})",
                i => new Vector2(i, 0),
                ConsoleColor.DarkMagenta, ConsoleColor.White);

            pos.AddComponent(rscPos);
            gameScene.AddGameObject(pos);
        }