コード例 #1
0
        // build area conections dictionary
        public Dictionary <string, Area> UpdateAreaDictionary(Island _island)
        {
            Dictionary <string, Area> areaOutput = new Dictionary <string, Area>();

            foreach (Area _area in _island.IslandAreas)
            {
                areaOutput.Add(_area.AreaName.ToLower(), _island.GetAreaByName(_area.AreaName));
            }
            return(areaOutput);
        }
コード例 #2
0
        public void Run()
        {
            CreateTheLand();

            // set starting island and area
            Island currentIsland = _islandRepo.GetIslandByName("Remer Island");
            Area   currentArea   = currentIsland.GetAreaByName("Sanfew");
            //Area currentArea = currentIsland.GetAreaByName("Port Senyn");

            // build area conections dictionary
            Dictionary <string, Area> areaConnections = UpdateAreaDictionary(currentIsland);

            //build island onections dictionary
            Dictionary <string, Island> islandConnections = UpdateIslandDictionary(currentIsland);

            // starting storyline
            Console.WriteLine("Shipwrecked on an unfamilure island, you find yourself in a small village. \n" +
                              "Here are a few commands: \n" +
                              "\"inv\" will show you inventory. \n" +
                              "\"Exit\" will quit the game. \n" +
                              "Press any key to begin.");
            Console.ReadKey();
            Console.Clear();

            // show starting area splash
            Console.WriteLine(currentArea.AreaSplash);

            // start inventory
            List <Result.Item> inventory = new List <Result.Item>();

            // game ui/logic
            bool gameInPlay = true;

            while (gameInPlay)
            {
                string command = Console.ReadLine().ToLower();
                Console.Clear();

                //check if command contains activity triggerPhrase
                bool commandContainsActivityTrigger = false;
                foreach (Activity areaActivity in currentArea.AreaActivities)
                {
                    if (command.Contains(areaActivity.TriggerPhrase))
                    {
                        commandContainsActivityTrigger = true;
                    }
                }

                // move to different area
                if (command.StartsWith("go ") && !commandContainsActivityTrigger)
                {
                    bool foundConnection = false;
                    foreach (string connection in currentArea.AreaConnections)
                    {
                        if (command.Contains(connection.ToLower()) && areaConnections.ContainsKey(connection.ToLower()))
                        {
                            // change to new area
                            currentArea     = currentIsland.GetAreaByName(connection);
                            foundConnection = true;
                            Console.Write($"You are on your way to {connection}....");

                            // show console spinner
                            var spinner = new SpinnerRepo.Spinner(Console.CursorLeft, Console.CursorTop, 50);
                            spinner.Start();
                            Thread.Sleep(7000);  // to do -- add travel time for each area
                            spinner.Stop();
                            Console.Clear();
                            break;
                        }
                    }
                    if (!foundConnection)
                    {
                        Console.WriteLine("I don't understand. Go where?");
                    }
                }

                // sail to different island
                else if (command.StartsWith("sail ") && !commandContainsActivityTrigger)
                {
                    bool foundConnection = false;
                    foreach (string connection in currentArea.AreaConnections)
                    {
                        if (command.Contains(connection.ToLower()) && islandConnections.ContainsKey(connection))
                        {
                            // change to new island
                            currentIsland = islandConnections[connection];
                            // change to new area
                            currentArea = currentIsland.GetAreaByName(connection);

                            foundConnection = true;
                            Console.Write($"You are on sailing to {connection} on {currentIsland.IslandName}....");

                            // show console spinner
                            var spinner = new SpinnerRepo.Spinner(Console.CursorLeft, Console.CursorTop, 50);
                            spinner.Start();
                            Thread.Sleep(7000);  // to do -- add travel time for each area
                            spinner.Stop();
                            Console.Clear();
                            break;
                        }
                    }
                    if (!foundConnection)
                    {
                        Console.WriteLine("I don't understand. Sail where?");
                    }
                }

                // do activity
                else if (commandContainsActivityTrigger)
                {
                    foreach (Activity areaActivity in currentArea.AreaActivities)
                    {
                        if (command.Contains(areaActivity.TriggerPhrase) && command.Contains(areaActivity.Result.ResultItem.ToString()))
                        {
                            // show activity message
                            Console.Write(areaActivity.Message);

                            // show console spinner
                            var spinner = new SpinnerRepo.Spinner(Console.CursorLeft, Console.CursorTop, 50);
                            spinner.Start();
                            Thread.Sleep(5000);  // to do -- add time for each activity
                            spinner.Stop();

                            // add random fails
                            Random rand           = new Random();
                            int    activityResult = rand.Next(0, 3);
                            // Console.WriteLine(activityResult);   // show activityResult for debuging
                            string resultMessage;
                            switch (activityResult)
                            {
                            case 0:
                                switch (areaActivity.TriggerPhrase)
                                {
                                case "fish":
                                    resultMessage = "That one got away...";
                                    break;

                                case "mine":
                                    resultMessage = "You swing the pickaxe and miss...";
                                    break;

                                case "chop":
                                    resultMessage = "You swing the axe and miss...";
                                    break;

                                case "clean":
                                    resultMessage = "You missed a spot, no gold for you!...";
                                    break;

                                default:
                                    resultMessage = "Good try...";
                                    break;
                                }
                                break;

                            case 1:
                            case 2:
                            default:
                                resultMessage = areaActivity.Result.ResultMessage;
                                // add item to inventory
                                inventory.Add(areaActivity.Result.ResultItem);
                                break;
                            }

                            // show result message
                            Console.WriteLine(resultMessage);
                            Console.WriteLine("Press any key to continue...");
                            Console.ReadKey();

                            Console.Clear();
                        }
                        else
                        {
                            // show result message
                            Console.WriteLine("Did you miss something?");
                            Console.WriteLine("Press any key to continue...");
                            Console.ReadKey();

                            Console.Clear();
                        }
                    }
                }

                // show inventory
                else if (command == "inv")
                {
                    if (inventory.Count == 0)
                    {
                        Console.WriteLine("Your bag is empty!");
                    }
                    else
                    {
                        foreach (Result.Item item in Enum.GetValues(typeof(Result.Item)))
                        {
                            int total = inventory.Count(s => s == item);
                            if (total != 0)
                            {
                                Console.WriteLine($"You have {total} {item}.");
                            }
                        }
                    }
                    Console.WriteLine("Press any key to continue...");
                    Console.ReadKey();
                    Console.Clear();
                }

                // exit game
                else if (command == "exit")
                {
                    Console.WriteLine("Goodbye!");
                    gameInPlay = false;
                }

                // default response
                else
                {
                    Console.WriteLine("You want to try smoething else?");
                }


                if (gameInPlay)
                {
                    Console.WriteLine(currentArea.AreaSplash);
                }
            }



            // keep the console up
            Console.ReadKey();
        }