コード例 #1
0
 public void give(item a)
 {
     inventory.Add(a);
 }
コード例 #2
0
        public static string evaluateActions(Player player, string action, biome Biome)
        {
            string evaluatedAction = action;
            //Check structures for effects
            int chanceOfEncounter       = 7;
            int chanceOfEncounterMining = chanceOfEncounter;
            int forageChanceBoost       = 0;

            foreach (var x in player.structuresBuilt)
            {
                if (x.location == Biome)
                {
                    if (x.effect == 1 || x.effect == 1.1)
                    {
                        if (x.effect == 1.1)
                        {
                            if (chanceOfEncounterMining - x.strength > 0)
                            {
                                chanceOfEncounterMining = chanceOfEncounterMining - x.strength;
                            }
                            else
                            {
                                chanceOfEncounterMining = 0;
                            }
                        }
                        else
                        {
                            if (chanceOfEncounter - x.strength > 0)
                            {
                                chanceOfEncounter = chanceOfEncounter - x.strength;
                            }
                            else
                            {
                                chanceOfEncounter = 0;
                            }
                        }
                    }
                    else
                    {
                        if (x.effect == 2)
                        {
                            forageChanceBoost += x.strength;
                        }
                    }
                }
            }
            if (chanceOfEncounter < chanceOfEncounterMining)
            {
                chanceOfEncounterMining = chanceOfEncounter;
            }
            //Evaluate actions
            switch (action.ToLower())
            {
            case "debugcheats":
            {
                for (int i = 0; i < 50; i++)
                {
                    player.give(wood);
                    player.give(stone);
                    player.give(rope);
                    player.give(stoneAxe);
                }
                unlockAction(useAxe, player);
            } break;

            case "build a structure":
            case "build":
            {
                List <structure> buildables = new List <structure>();
                foreach (var build in structures)
                //Console.WriteLine("{0} x {1}",x.name , player.inventory.Count(item => x.name.Equals(item.name)));
                {
                    bool        canBuild = true;
                    List <item> reqMat   = new List <item>();
                    foreach (var x in build.reqMat)
                    {
                        if (!reqMat.Contains(x))
                        {
                            reqMat.Add(x);
                        }
                    }
                    foreach (var x in reqMat)
                    {
                        if (build.reqMat.Count(req => x.name.Equals(req.name)) > player.inventory.Count(req => x.name.Equals(req.name)))
                        {
                            canBuild = false;
                        }
                    }
                    if (canBuild)
                    {
                        buildables.Add(build);
                    }
                }
                if (!buildables.Any())
                {
                    Console.WriteLine("You don't have the required materials to build anything!\r\n");
                }
                else
                {
                    Console.WriteLine("Which would you like to build: ");
                    foreach (var x in buildables)
                    {
                        Console.WriteLine(x.name);
                    }
                    string    buildStructure = getInput();
                    bool      hasStruct      = false;
                    structure building       = new structure();
                    foreach (var x in buildables)
                    {
                        if (x.name.ToLower() == buildStructure.ToLower())
                        {
                            hasStruct = true; building = x;
                        }
                    }
                    if (hasStruct)
                    {
                        foreach (var x in building.reqMat)
                        {
                            Console.WriteLine("removing {0}", x.name);
                            player.inventory.Remove(x);
                        }
                        building.location = Biome;
                        player.structuresBuilt.Add(building);
                    }
                    building = null;
                }
            } break;

            case "use axe":
            {
                Console.WriteLine("How would you like to use your axe?");
                if (Biome.resources.Contains(wood))
                {
                    Console.WriteLine("cut down a tree? (cut)");
                }
                else if (Biome.resources.Contains(cactus))
                {
                    Console.WriteLine("cut down a cacti? (cut)");
                }
                else
                {
                    Console.WriteLine("There is nothing to cut down!");
                }
                string choice = getInput();
                switch (choice.ToLower())
                {
                case "cut":
                case "cut down a tree":
                {
                    foreach (var x in Biome.resources)
                    {
                        if (x.cuttable == true)
                        {
                            Random rng = new Random();
                            for (int i = 0; i < rng.Next(3, 7); i++)
                            {
                                player.inventory.Add(x);
                            }
                        }
                    }
                } break;

                default:
                {
                } break;
                }
            } break;

            case "forage":
            case "forage for resources":
            case "forage for items":
            {
                for (int i = 0; i < 5; i++)
                {
                    foreach (var x in Biome.resources)
                    {
                        int chance = rand.Next(0, 100);
                        if (Biome.chanceOfGettingResource > (chance - forageChanceBoost) & x.forageable == true)
                        {
                            player.inventory.Add(x);
                            Console.WriteLine("You found a {0}", x.name);
                        }
                    }
                }
                Console.WriteLine("\r\n");
            } break;

            case "look around":
            {
                Console.WriteLine(Biome.description);
            } break;

            case "pick up dropped items":
            {
                foreach (var x in Biome.itemsDropped)
                {
                    Console.WriteLine("\r\n Which item would you like to pick up?");
                    Console.WriteLine(x.name);
                    string itemName = getInput();
                    if (x.name == itemName)
                    {
                        player.inventory.Add(x);
                        Console.WriteLine("Sucessfully picked up item");
                    }
                    else
                    {
                        Console.WriteLine("No such item was dropped!");
                    }
                }
            } break;

            case "access inventory":
            case "inventory":
            {
                player.inventory.Sort((p, q) => p.name.CompareTo(q.name));
                Console.WriteLine("Your inventory contains:");
                List <item> inventoryItems = new List <item>();
                foreach (var x in player.inventory)
                {
                    if (!inventoryItems.Contains(x))
                    {
                        inventoryItems.Add(x);
                    }
                }
                foreach (var x in inventoryItems)
                {
                    Console.WriteLine("{0} x {1}", x.name, player.inventory.Count(item => x.name.Equals(item.name)));
                }
                Console.Write("\r\n What would you like to use? or would you like to return\r\n");
                string inventoryAction = getInput();
                item   inventoryItem   = new item();
                foreach (var x in player.inventory)
                {
                    if (x.name == inventoryAction)
                    {
                        inventoryItem = x;
                    }
                }
                if (player.inventory.Contains(inventoryItem))
                {
                    player.inventory.Remove(inventoryItem);
                }
                if (inventoryItem.name == "stone" || inventoryItem.name == "stick")
                {
                    Console.WriteLine("You have dropped a {0}\r\n", inventoryItem.name);
                    Biome.itemsDropped.Add(inventoryItem);
                }

                evaluatedAction = "inventory";
            } break;

            case "crafting":
            case "craft":
            {
                bool        crafting   = false;
                List <item> craftables = new List <item>();
                foreach (var x in craftableGameItems)
                {
                    bool craftable = true;
                    int  numberOfIngredients;
                    numberOfIngredients = x.craftingIngredients.Count();
                    for (int i = 0; i < numberOfIngredients; i++)
                    {
                        if (!player.inventory.Contains(x.craftingIngredients[i]))
                        {
                            craftable = false;
                        }
                    }
                    if (craftable)
                    {
                        craftables.Add(x);
                    }
                }
                Console.WriteLine("\r\n What would you like to craft?");
                foreach (var x in craftables)
                {
                    Console.WriteLine(x.name);
                }
                string craftItem = getInput();
                Console.Write("\r\n");
                foreach (var x in craftables)
                {
                    if (x.name.ToLower() == craftItem.ToLower())
                    {
                        int numberOfIngredients = x.craftingIngredients.Count();
                        for (int i = 0; i < numberOfIngredients; i++)
                        {
                            player.inventory.Remove(x.craftingIngredients[i]);
                        }
                        for (int i = 0; i < x.craftingQuantity; i++)
                        {
                            player.inventory.Add(x);
                            if (x.type == 1)
                            {
                                if (!player.actions.Contains(useAxe))
                                {
                                    unlockAction(useAxe, player);
                                }
                            }
                            crafting = true;
                        }
                    }
                }
                if (crafting)
                {
                    Console.ForegroundColor = ConsoleColor.DarkCyan;
                    Console.WriteLine("CRAFTING");
                    Thread.Sleep(500); Console.Write("-");
                    Thread.Sleep(500); Console.Write("-");
                    Thread.Sleep(500); Console.Write("-");
                    Thread.Sleep(500); Console.Write("-\r\n");
                    Console.ForegroundColor = ConsoleColor.White;
                }
                else
                {
                    Console.WriteLine("Unable to craft {0} \r\n", craftItem);
                }
            } break;

            case "leave":
            {
                evaluatedAction = "leave";
            } break;

            case "exit":
            {
                Environment.Exit(0);
            } break;

            default:
            {
                Console.WriteLine("Unknown Action");
            } break;
            }
            bool inventoryHasBuilding = false;

            foreach (var x in player.inventory)
            {
                if (x.type == 10 & x != stone)
                {
                    inventoryHasBuilding = true;
                }
            }
            if (inventoryHasBuilding == true & !player.actions.Contains(mainGame.build))
            {
                unlockAction(build, player);
            }

            player.actions.Sort((p, q) => p.title.CompareTo(q.title));
            return(evaluatedAction);
        }