Example #1
0
    private void SetGameRulesFromSeed(int seed)
    {
        // Apply the seed.
        Random.InitState(seed);

        Debug.Log("Generating Rules From Seed: " + seed);

        //////////////// First create the terrain types, looping through each possible sprite.
        TerrainType.terrainTypes = new List <TerrainType> ();
        foreach (var sprite in Resources.LoadAll <Sprite>("Terrain"))
        {
            // Loop through a set of colours for this sprite. There will be one terrain type per sprite / colour combination.
            foreach (Color color in GenerateRandomColours(NTerrainColours, 0.85f, 0.55f))
            {
                TerrainType.GenerateNewTerrainType(sprite, color);
            }
        }

        /////////////// Generate the creature types by looping through each creature sprite.
        CreatureType.creatureTypes = new List <CreatureType> ();
        foreach (Sprite sprite in Resources.LoadAll <Sprite> ("Creatures"))
        {
            // Loop through a set of colours for this sprite. There will be one creature type per sprite / colour combination.
            foreach (Color color in GenerateRandomColours(NCreatureColours, 0.85f, 0.9f))
            {
                CreatureType.GenerateNewCreatureType(sprite, color);
            }
        }

        // Now for each creature generate its stance with each other creature.
        foreach (CreatureType creatureType in CreatureType.creatureTypes)
        {
            creatureType.GenerateStancesWithOtherCreatures();
        }

        /////////////// Genearte BASIC items.

        ItemType.itemTypes = new List <ItemType> ();

        // First create the basic items.
        Sprite[]        sprites    = Resources.LoadAll <Sprite> ("Items/Basic");
        List <ItemType> basicItems = new List <ItemType> ();

        for (int i = 0; i < sprites.Length * 3; i++)
        {
            basicItems.Add(ItemType.GenerateNewBasicItemType());
        }

        // Now sort the basic items by rarity, rarist first.
        basicItems.Sort(((ItemType x, ItemType y) => {
            if (x.rarity < y.rarity)
            {
                return(1);
            }
            return(-1);
        }));

        // Now sort the creatures by rarity, rarist first.
        CreatureType.creatureTypes.Sort(((CreatureType x, CreatureType y) => {
            if (x.diff < y.diff)
            {
                return(1);
            }
            return(-1);
        }));

        // Loop throug each creature.
        for (int i = 0; i < CreatureType.creatureTypes.Count; i++)
        {
            // If there is an ith creature, assign the ith item type as its drop type. This will give the rarist items to the most powerfull creatures.
            CreatureType.creatureTypes [i].itemTypeDropped = basicItems [i];

            // Give this item a sprite and colour which "matches" the creature's.
            string spriteName = CreatureType.creatureTypes [i].sprite.name;
            basicItems [i].sprite  = Resources.Load <Sprite> ("Items/Basic/" + spriteName);
            basicItems [i].color   = CreatureType.creatureTypes [i].color;
            basicItems [i].color.a = 0.8f;
        }

        // Loop through unassigned item types, however do it by sprite / colour.
        Sprite[] itemSprites = Resources.LoadAll <Sprite>("Items/Basic");
        int      itemAt      = CreatureType.creatureTypes.Count;

        for (int spriteAt = CreatureType.creatureTypes.Count / 3; spriteAt < itemSprites.Length; spriteAt++)
        {
            foreach (Color color in GenerateRandomColours(3, 0.85f, 0.9f))
            {
                // This item will spawn naturally in a world. Pick a terrain type for it to spawn in.
                TerrainType tt = TerrainType.terrainTypes [Random.Range(0, TerrainType.terrainTypes.Count)];
                tt.itemsFoundHere.Add(basicItems [itemAt]);

                // Set the colour and texture.
                basicItems [itemAt].sprite  = itemSprites [spriteAt];
                basicItems [itemAt].color   = color;
                basicItems [itemAt].color.a = 0.8f;

                // Increase the item index.
                itemAt += 1;
            }
        }


        /////////////// Genearte CRAFTED items.

        CraftingRecipe.craftingRecipes = new List <CraftingRecipe> ();

        // Loop through sprites.
        foreach (Sprite sprite in Resources.LoadAll <Sprite>("Items/Crafted"))
        {
            // Loop through colours.
            foreach (var color in GenerateRandomColours(3, 0.85f, 0.9f))
            {
                // Create a crafted item wit the sprite and colours.
                ItemType it = ItemType.GenerateNewCraftedItemType(sprite, color);

                // Generate the two crafting recipes for the item.
                CraftingRecipe.BuildRecipeFor(it, basicItems);
                CraftingRecipe.BuildRecipeFor(it, basicItems);
            }
        }

        // Reset Random Seed.
        Random.InitState(System.DateTime.Now.Millisecond);

        Debug.Log("Rules created with "
                  + TerrainType.terrainTypes.Count + " terrain types, "
                  + CreatureType.creatureTypes.Count + " creature types, "
                  + ItemType.itemTypes.Count + " item types, and "
                  + CraftingRecipe.craftingRecipes.Count + " crafting recipes.");
    }