Exemplo n.º 1
0
        public void ResetLevel(int tilemapRow, int tilemapCol)
        {
            GameMananger.Score = 0;

            for (var index = 0; index < Program.Enemies.Count; index++)
            {
                var enemy = Program.Enemies[index];
                enemy.Destroy();
            }


            for (var index = 0; index < Program.Crates.Count; index++)
            {
                var crate = Program.Crates[index];
                crate.Desactivate();
            }

            tileset = new Tileset(LoadTileset(11, "img/tileset/Level1/tileset_lvl1_"));
            tilemap = new Tilemap(tilemapCol, tilemapRow, 32, tileset, "img/tileset/background_lvl1.png");
            SetMap("maps/Lvl1.csv");
            player    = PlayerFactory.CreatePlayer(new Vector2D(400, 200), tilemap);
            generator = new EnemyGenerator(tilemap, player);
            crates    = new CrateGenerator(tilemap, player);
        }
        private static void StartGame()
        {
            Console.Clear();
            Console.Write("Enter your name: ");
            Player player = new Player(Console.ReadLine());

            Console.Clear();
            Window         consoleWindow  = new Window();
            Printer        printer        = new Printer(consoleWindow);
            StartupHelper  startupHelper  = new StartupHelper(consoleWindow);
            CoinGenerator  coinGenerator  = new CoinGenerator();
            EnemyGenerator enemyGenerator = new EnemyGenerator();
            Stopwatch      watch          = new Stopwatch();

            watch.Start();
            Random random = new Random();
            Timer  timer  = new Timer(t =>
            {
                if (watch.Elapsed.Seconds % 3 == 0)
                {
                    Coin newCoin = coinGenerator.GenerateCoin(consoleWindow, random);
                    printer.PrintCoin(newCoin);
                }
                if (watch.Elapsed.Seconds % 2 == 0)
                {
                    if (enemyGenerator.Enemies.Count < 15)
                    {
                        Enemy newEnemy = enemyGenerator.GenerateEnemy(consoleWindow, random);
                        printer.PrintEnemy(newEnemy);
                    }
                }
                if (watch.Elapsed.Seconds % 1 == 0)
                {
                    printer.PrintTime(watch.Elapsed);
                }
            }, null, 1000, 1000);

            startupHelper.SetProperties();
            printer.PrintFrame();
            printer.PrintInfo(player);

            while (true)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key          = Console.ReadKey();
                    Direction      newDirection = GetDirection(key.Key);
                    printer.ClearPlayer(player);
                    player.Move(newDirection);
                    if (player.IsDead(consoleWindow.Height, consoleWindow.Width) ||
                        enemyGenerator.IsPlayerKilled(player))
                    {
                        timer.Dispose();
                        printer.EndGame(player);
                        return;
                    }
                    CollectCoin(player, coinGenerator);
                    printer.PrintStatsOnly(player);
                    printer.PrintPlayer(player);
                }
            }
        }