コード例 #1
0
        public static void PlaceItems(Tile[,] map)
        {
            Dictionary <string, int> ItemChances = new Dictionary <string, int>
            {
                { "Potion", 60 },
                { "Sword", 10 },
                { "MagicAmulet", 10 },
                { "Shield", 10 },
                { "Armor", 10 }
            };

            Dictionary <String, int> PotionChances = new Dictionary <string, int>
            {
                { "Health", 50 },
                { "Mana", 50 }
            };

            for (int i = 0; i < ITEM_NUMBER; i++)
            {
                bool foundSuitablePosition = false;
                int  iter = 0;
                int  x    = -1;
                int  y    = -1;
                while (!foundSuitablePosition)
                {
                    iter++;
                    x = Dice.GetRandint(0, GameManager.MAP_WIDTH);
                    y = Dice.GetRandint(0, GameManager.MAP_HEIGHT);


                    if (!map[x, y].Blocked)
                    {
                        foundSuitablePosition = true;
                    }
                    {
                        foreach (GameObject obj in GameManager.Objects)
                        {
                            if (obj.Position.x == x && obj.Position.y == y && obj.Item != null)
                            {
                                foundSuitablePosition = false;
                                break;
                            }
                        }
                    }


                    if (iter > MAX_ITER)
                    {
                        break;
                    }
                }
                if (foundSuitablePosition)
                {
                    string chosen = RandomChoice(ItemChances);
                    if (chosen == "Potion")
                    {
                        string subtype = RandomChoice(PotionChances);
                        if (subtype == "Health")
                        {
                            GameManager.Objects.Add(ItemFactory.CreateHealthPotion(x, y));
                        }
                        else if (subtype == "Mana")
                        {
                            GameManager.Objects.Add(ItemFactory.CreateManaPotion(x, y));
                        }
                    }
                    else if (chosen == "Sword")
                    {
                        GameManager.Objects.Add(EquipmentFactory.CreateSword(x, y));
                    }
                    else if (chosen == "MagicAmulet")
                    {
                        GameManager.Objects.Add(EquipmentFactory.CreateMagicAmulet(x, y));
                    }
                    else if (chosen == "Shield")
                    {
                        GameManager.Objects.Add(EquipmentFactory.CreateShield(x, y));
                    }
                    else if (chosen == "Armor")
                    {
                        GameManager.Objects.Add(EquipmentFactory.CreateArmor(x, y));
                    }
                }
            }
        }