Пример #1
0
    Texture2D CreateEnemyTexture()
    {
        LevelGenerator LG     = GetComponent <LevelGenerator>();
        Texture2D      retTex = LG.BlankTex(levelMap.width, levelMap.height);

        SpawnableMapping[] mappings       = LG.mappingsHolder.spawnableMappings;
        List <Vector2Int>  takenPositions = new List <Vector2Int>();

        foreach (SpawnableMapping mapping in mappings)        //add spawns for each enemy
        {
            int numToMake = Mathf.RoundToInt(UnityEngine.Random.Range(mapping.minSpawns, mapping.maxSpawns));
            // print(mapping.name + ": " + numToMake);
            while (numToMake > 0)
            {
                List <Vector2Int> possiblePositions;
                switch (mapping.type)
                {
                case SpawnableMapping.SpawnableType.ground:
                    possiblePositions = groundSpawns;
                    break;

                case SpawnableMapping.SpawnableType.air:
                    possiblePositions = airSpawns;
                    break;

                default:
                    possiblePositions = treasureSpawns;
                    break;
                }
                if (possiblePositions.Count == 0)
                {
                    print("Out of positions");
                    break;
                }
                int posIndex;
                int iteratations = 0;
                do                 //select a position that hasn't been used
                {
                    posIndex = Mathf.RoundToInt(UnityEngine.Random.value * (possiblePositions.Count - 1));
                    if (iteratations > 100)
                    {
                        print("iterated too long");
                        break;
                    }
                    iteratations++;
                }while (!IsValidPosition(possiblePositions[posIndex], takenPositions, mapping));
                takenPositions.Add(possiblePositions[posIndex]);
                Vector2Int pos = possiblePositions[posIndex];
                retTex.SetPixel(pos.x, pos.y, mapping.color);
                numToMake--;
            }
        }
        return(retTex);
    }
    Texture2D FlipTexture(Texture2D tex)     //flips around x
    {
        LevelGenerator LG     = GetComponent <LevelGenerator>();
        Texture2D      retTex = LG.BlankTex(tex.width, tex.height);

        for (int x = 0; x < tex.width; x++)
        {
            for (int y = 0; y < tex.height; y++)
            {
                Color pixel = tex.GetPixel(x, y);
                retTex.SetPixel(tex.width - 1 - x, y, pixel);
            }
        }
        return(retTex);
    }
    void ProcessTileMap()     //process all probability tiles and add blocks
    {
        LevelGenerator LG = GetComponent <LevelGenerator>();

        tileMapProcessing = LG.CopyTexture(tileMap);                    //make a copy to edit
        tileMapProcessed  = LG.BlankTex(tileMap.width, tileMap.height); //new map to write to
        for (int x = 0; x < tileMapProcessing.width; x++)
        {
            for (int y = tileMapProcessing.height; y >= 0; y--)
            {
                Color       lookingAtColor = tileMapProcessing.GetPixel(x, y);
                TileMapping thisMapping    = null;
                Color       processedColor = ProcessColor(lookingAtColor, ref thisMapping);
                if (thisMapping != null)
                {
                    if (thisMapping.tileType == TileMapping.SpecialTileType.groundBlock)
                    {
                        InsertBlock(x, y, true);
                        continue;
                    }
                    if (thisMapping.tileType == TileMapping.SpecialTileType.airBlock)
                    {
                        InsertBlock(x, y, false);
                        continue;
                    }
                }
                tileMapProcessed.SetPixel(x, y, processedColor);
            }
        }
        float chanceToFlip = 0.5f;

        if (UnityEngine.Random.value > chanceToFlip)         //flip entire map half the time
        {
            tileMapProcessed = FlipTexture(tileMapProcessed);
        }
    }