Exemplo n.º 1
0
        public static void Start()
        {
            Scene scene = new Scene(80, 30);

            // create a bunch of entities at random spots in the scene
            Random        random   = new Random();
            List <Entity> entities = new List <Entity>();

            for (int i = 0; i < 20; i++)
            {
                Entity entity = new Entity(Utils.GetTestGraphics());
                entities.Add(entity);
                scene.AddEntity(entity);
                entity.Pos.X = random.Next(0, 80);
                entity.Pos.Y = random.Next(0, 30);
            }

            // find out which entities are colliding with a rectangle at the top left of the scene
            AARect2          someRegion        = new AARect2(new Vector2(0, 0), 20, 10);
            HashSet <Entity> collidingEntities = Game.HitTest(entities, someRegion);

            foreach (var entity in collidingEntities)
            {
                // for each entity that collided w the rect, change some of its graphics to say "hit"
                entity.Characters[0, 0] = 'h';
                entity.Characters[0, 1] = 'i';
                entity.Characters[0, 2] = 't';
            }

            Game.Start(scene, 80, 30);
        }
Exemplo n.º 2
0
        public static void Start()
        {
            // = start scene =
            _startScene = new Scene(200, 60);
            Entity title = new Entity(Utils.FileToCharArray("../../../resources/ascii_art/title.txt"));

            _startScene.AddEntity(title);
            title.Pos = new Vector2(70, 20);

            MenuEntity menu = new MenuEntity();

            _startScene.AddEntity(menu);
            _startScene.KeyFocusedEntity = menu;
            menu.Pos.X = title.Pos.X + 20;
            menu.Pos.Y = title.Pos.Y + 10;
            menu.AddItem("play");
            menu.AddItem("exit");
            menu.ItemSelected += (sender, item) =>
            {
                if (item == "exit")
                {
                    Environment.Exit(0);
                }
                else if (item == "play")
                {
                    Game.Scene = _mainScene;
                }
            };

            // = main scene =
            _mainScene = new Scene(200, 60);

            // UI
            _fpsCounter = new FpsCounter();
            _mainScene.AddEntity(_fpsCounter);

            _scoreLabel = new TextEntity("score: 0");
            _mainScene.AddEntity(_scoreLabel);
            _scoreLabel.Pos      = new Vector2(0, 2);
            _scoreLabel.Updated += (scoreLabel, dt) => (scoreLabel as TextEntity).Text = "score: " + _score;

            // player
            _player = new Entity(Utils.FileToCharArray("../../../resources/ascii_art/plane.txt", true));
            _mainScene.AddEntity(_player);
            _player.Pos.X = _mainScene.Width / 2 - _player.BoundingBox.Width / 2;
            _player.Pos.Y = _mainScene.Height - _player.BoundingBox.Height;
            float    fireDelay     = 0.5f;
            DateTime timeLastFired = DateTime.Now;
            var      missileChars  = Utils.FileToCharArray("../../../resources/ascii_art/missile.txt", true);

            _player.KeyEventReceived += (sender, info) =>
            {
                var     h_amount   = 2;
                var     v_amount   = 1;
                var     key        = info.Key;
                AARect2 playerBBox = _player.BoundingBox;
                if (key == ConsoleKey.W)
                {
                    if (playerBBox.Top > 0)
                    {
                        _player.Pos.Y -= v_amount;
                    }
                }
                else if (key == ConsoleKey.S)
                {
                    if (playerBBox.Bottom < _mainScene.Height)
                    {
                        _player.Pos.Y += v_amount;
                    }
                }
                else if (key == ConsoleKey.A)
                {
                    if (playerBBox.Left > -13)
                    {
                        _player.Pos.X -= h_amount;
                    }
                }
                else if (key == ConsoleKey.D)
                {
                    if (playerBBox.Right < _mainScene.Width + 14)
                    {
                        _player.Pos.X += h_amount;
                    }
                }

                if (key == ConsoleKey.Spacebar)
                {
                    var now     = DateTime.Now;
                    var elapsed = now - timeLastFired;
                    if (elapsed.TotalSeconds >= fireDelay)
                    {
                        Entity missile = new Entity(missileChars);
                        missile.Pos    = _player.Pos.Copy();
                        missile.Pos.Y -= missile.BoundingBox.Height / 2;
                        missile.Pos.X += 16;
                        _mainScene.AddEntity(missile);

                        missile.Updated += MissileOnUpdated;
                        timeLastFired    = now;
                    }
                }
            };
            _mainScene.KeyFocusedEntity = _player;

            // enemy spawning
            Random random     = new Random();
            var    enemyChars = Utils.FileToCharArray("../../../resources/ascii_art/plane2.txt", true);
            Timer  spawner    = new Timer(-1, 4);

            Game.AddTimer(spawner);
            spawner.Start();
            int wave = 0;

            spawner.Fired += sender =>
            {
                for (int i = 0; i < wave / 5 + 1; i++)
                {
                    int    rx    = random.Next(0, _mainScene.Width - enemyChars.GetLength(1));
                    int    ry    = random.Next(-50, -10);
                    Entity enemy = new Entity(enemyChars);
                    enemy.Pos = new Vector2(rx, ry);
                    _mainScene.AddEntity(enemy);

                    enemy.Updated += (entity, dt) =>
                    {
                        float speed = 10;
                        entity.Pos.Y += speed * dt;

                        if (entity.Pos.Y > Game.Window.Height)
                        {
                            _mainScene.RemoveEntity(entity);
                        }

                        if (enemy.BoundingBox.CollidesWith(_player.BoundingBox))
                        {
                            Entity explosion = new Entity(_explosionChars);
                            _mainScene.AddEntity(explosion);
                            explosion.Pos    = _player.Pos.Copy();
                            explosion.Pos.X += 1;
                            Game.DoLater(() =>
                            {
                                _mainScene.RemoveEntity(explosion);
                            }, 0.2f);
                            Game.DoLater(() =>
                            {
                                Game.Scene = _endScene;
                            }, 2);

                            _mainScene.RemoveEntity(_player);
                            _mainScene.RemoveEntity(enemy);
                        }
                    };
                }

                wave += 1;
            };

            // = end scene =
            _endScene = new Scene(200, 60);
            Entity gameOver = new Entity(Utils.FileToCharArray("../../../resources/ascii_art/game_over.txt"));

            _endScene.AddEntity(gameOver);
            gameOver.Pos.X = _endScene.Width / 2 - gameOver.BoundingBox.Width / 2;
            gameOver.Pos.Y = 20;

            // start the game!
            Game.Start(_startScene, 200, 60);
        }