Ejemplo n.º 1
0
        public void Update(FileParser p)
        {
            world         = new World();
            visualization = new Interface();
            levelGen      = new LevelGenerator();
            player        = new Player();
            parser        = p;
            messages      = new List <string>();
            Tuple <int, int>    playerPos;
            Tuple <int, int>    exitPos;
            List <Object>       currentTile;
            List <IDealsDamage> tileDmg;
            List <NPC>          tileNPC;
            List <IItem>        tileItems;
            List <IItem>        inventoryItems;
            int            level = 1;
            bool           quit  = false;
            bool           action;
            ConsoleKeyInfo option;
            HighScore      hS;
            short          itemNum, npcNum;

            if (keyBinds.Count == 0)
            {
                AddKeys();
            }
            messages.Add("Welcome to the game!");

            do
            {
                exitPos   = levelGen.GenerateLevel(world, player, level, parser);
                playerPos = new Tuple <int, int>(player.X, player.Y);

                do
                {
                    // Clear our command flags to update next
                    CommandFlag = Command.None;
                    action      = false;
                    tileItems   = new List <IItem>();
                    tileDmg     = new List <IDealsDamage>();
                    tileNPC     = new List <NPC>();

                    currentTile = world.WorldArray[player.X, player.Y].
                                  GetInfo().ToList();

                    foreach (IItem obj in currentTile.OfType <IItem>())
                    {
                        tileItems.Add(obj);
                    }

                    foreach (IDealsDamage obj in
                             currentTile.OfType <IDealsDamage>())
                    {
                        tileDmg.Add(obj);
                    }

                    foreach (IDealsDamage obj in tileDmg)
                    {
                        if (obj is Trap)
                        {
                            if (!(obj as Trap).FallenInto)
                            {
                                obj.OnDetectingPlayer(this);
                            }
                        }
                        if (obj is NPC)
                        {
                            tileNPC.Add(obj as NPC);
                            if (((obj as NPC).Hostile))
                            {
                                obj.OnDetectingPlayer(this);
                            }
                        }
                    }

                    inventoryItems = player.Inventory.GetInfo().ToList();

                    if (player.HP <= 0)
                    {
                        break;
                    }

                    visualization.ShowWorld(world, player, level);
                    visualization.ShowStats(world, player);
                    visualization.ShowLegend(world);
                    visualization.ShowMessages(world, messages);
                    visualization.ShowSurrounds
                        (world.GetSurroundingInfo(player));
                    visualization.ShowOptions(new
                                              List <ConsoleKey>(keyBinds.Keys));

                    messages.Clear();

                    // Update our input for everything else to use
                    option = Console.ReadKey();
                    if (keyBinds.TryGetValue(option.Key, out var command))
                    {
                        CommandFlag |= command;

                        switch (CommandFlag)
                        {
                        case Command.Quit:
                            string wantsQuit;
                            do
                            {
                                visualization.AskQuit();
                                wantsQuit = Console.ReadLine();
                            } while ((wantsQuit.ToUpper() != "Y") &&
                                     (wantsQuit.ToUpper() != "N"));
                            if (wantsQuit.ToUpper() == "Y")
                            {
                                quit = true;
                            }
                            break;

                        case Command.MoveNorth:
                            if (player.MoveNorth())
                            {
                                playerPos =
                                    world.UpdatePlayer(playerPos, player);
                                action = true;
                                messages.Add("You moved NORTH");
                            }
                            else
                            {
                                messages.Add("You tried to move NORTH." +
                                             " But you hit a wall instead");
                            }
                            break;

                        case Command.MoveSouth:
                            if (player.MoveSouth(world.X))
                            {
                                playerPos =
                                    world.UpdatePlayer(playerPos, player);
                                action = true;
                                messages.Add("You moved SOUTH");
                            }
                            else
                            {
                                messages.Add("You tried to move SOUTH." +
                                             "  But you hit a wall instead");
                            }
                            break;

                        case Command.MoveWest:
                            if (player.MoveWest())
                            {
                                playerPos =
                                    world.UpdatePlayer(playerPos, player);
                                action = true;
                                messages.Add("You moved WEST");
                            }
                            else
                            {
                                messages.Add("You tried to move WEST." +
                                             "  But you hit a wall instead");
                            }
                            break;

                        case Command.MoveEast:
                            if (player.MoveEast(world.Y))
                            {
                                playerPos =
                                    world.UpdatePlayer(playerPos, player);
                                action = true;
                                messages.Add("You moved EAST");
                            }
                            else
                            {
                                messages.Add("You tried to move EAST." +
                                             "  But you hit a wall instead");
                            }
                            break;

                        case Command.AttackNPC:
                            if ((tileNPC.Count > 0))
                            {
                                if (player.SelectedWeapon != null)
                                {
                                    do
                                    {
                                        visualization.ShowNPCsToAttack(
                                            tileNPC);
                                        short.TryParse(
                                            Console.ReadLine(),
                                            out npcNum);
                                    } while ((npcNum < 0) ||
                                             (npcNum > tileItems.Count));

                                    if (npcNum != tileItems.Count)
                                    {
                                        player.AttackNPC(this,
                                                         tileNPC[npcNum]);
                                        action = true;
                                    }
                                }
                                else
                                {
                                    messages.Add("You tried to attack an" +
                                                 " NPC but you don't have a " +
                                                 "weapon equipped");
                                }
                            }
                            else
                            {
                                messages.Add("You tried to attack an NPC" +
                                             " but there are no NPC in the " +
                                             "current tile");
                            }
                            break;

                        case Command.PickUpItem:
                            if (tileItems.Count > 0)
                            {
                                do
                                {
                                    visualization.ShowItems(tileItems,
                                                            "Pick Up");
                                    short.TryParse(
                                        Console.ReadLine(), out itemNum);
                                } while ((itemNum < 0) ||
                                         (itemNum > tileItems.Count));

                                if (itemNum != tileItems.Count)
                                {
                                    tileItems[itemNum].OnPickUp(this);
                                    action = true;
                                }
                            }
                            else
                            {
                                messages.Add("You tried to PICK UP an " +
                                             "item but there are no items " +
                                             "available to be picked up");
                            }
                            break;

                        case Command.UseItem:
                            if (inventoryItems.Count > 0)
                            {
                                do
                                {
                                    visualization.ShowItems(inventoryItems,
                                                            "Use");
                                    short.TryParse(
                                        Console.ReadLine(), out itemNum);
                                } while ((itemNum < 0) ||
                                         (itemNum > inventoryItems.Count));

                                if (itemNum != inventoryItems.Count)
                                {
                                    inventoryItems[itemNum].OnUse(this);
                                    action = true;
                                }
                            }
                            else
                            {
                                messages.Add("You tried to USE an " +
                                             "item but you currently don't have" +
                                             " any items in the inventory");
                            }
                            break;

                        case Command.DropItem:
                            if (inventoryItems.Count > 0)
                            {
                                do
                                {
                                    visualization.ShowItems(inventoryItems,
                                                            "Drop");
                                    short.TryParse(
                                        Console.ReadLine(), out itemNum);
                                } while ((itemNum < 0) ||
                                         (itemNum > inventoryItems.Count));

                                if (itemNum != inventoryItems.Count)
                                {
                                    inventoryItems[itemNum].OnDrop(this);
                                    action = true;
                                }
                            }
                            else
                            {
                                messages.Add("You tried to DROP an " +
                                             "item but you currently don't have" +
                                             " any items in the inventory");
                            }
                            break;

                        case Command.Information:
                            messages.Add("You sought more info in the " +
                                         "elder scrolls");
                            visualization.ShowInformation(parser);
                            Console.ReadKey();
                            break;
                        }

                        if (action)
                        {
                            player.LoseHP(1);
                        }
                    }
                    else
                    {
                        Console.WriteLine();
                        visualization.WrongOption(option.Key.ToString());
                        Console.ReadKey();
                    }
                } while ((!playerPos.Equals(exitPos)) && (!quit) &&
                         (player.HP > 0));

                if (!quit)
                {
                    level++;
                }
            } while ((player.HP > 0) && (!quit));

            if (!quit)
            {
                visualization.ShowWorld(world, player, level);
                visualization.ShowStats(world, player);
                visualization.ShowLegend(world);

                visualization.ShowDeath(level);
            }

            if (CheckHighScore(level))
            {
                visualization.Success(level);
                string name = Console.ReadLine();
                if (name.Length > 3)
                {
                    name = name.Substring(0, 3);
                }
                hS = new HighScore(name, level);
                parser.UpdateHighScores(hS);
            }
            else
            {
                visualization.Failure(level);
                Console.ReadKey();
            }
        }