Exemplo n.º 1
0
    public Texture2D GenerateEnemies(Texture2D _levelMap)
    {
        LevelGenerator LG = GetComponent <LevelGenerator>();

        levelMap = LG.CopyTexture(_levelMap);         //make sure not to edit original texture
        for (int x = 0; x < levelMap.width; x++)
        {
            for (int y = 0; y < levelMap.height; y++)
            {
                Color pixelColor = levelMap.GetPixel(x, y);
                if (pixelColor.a == 0.0f)
                {
                    allValid.Add(new Vector2Int(x, y));                   //find all empty points in level
                }
                TileMapping tile = Array.Find(LG.mappingsHolder.mappings, TileMapping => TileMapping.color == pixelColor);
                if (tile != null && tile.tileType == TileMapping.SpecialTileType.door)
                {
                    doorLocations.Add(new Vector2Int(x, y));
                }
            }
        }

        //remove positions near enterance
        List <Vector2Int> positionsToRemove = new List <Vector2Int>();
        float             minDistFromDoor   = 8;
        Vector2Int        doorPos           = (doorLocations[0].y > doorLocations[1].y) ? doorLocations[0] : doorLocations[1];

        foreach (Vector2Int pos in allValid)
        {
            if (Vector2Int.Distance(pos, doorPos) < minDistFromDoor)
            {
                positionsToRemove.Add(pos);
            }
        }
        foreach (Vector2Int posToRemove in positionsToRemove)
        {
            allValid.Remove(posToRemove);
        }

        foreach (Vector2Int pos in allValid)          //test for valid spawn types
        {
            if (CanSpawnGround(pos))
            {
                groundSpawns.Add(pos);
            }
            if (CanSpawnAir(pos))
            {
                airSpawns.Add(pos);
            }
            if (CanSpawnTreasure(pos))
            {
                treasureSpawns.Add(pos);
            }
        }
        AssignTreasureWeights();
        return(CreateEnemyTexture());
    }
    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);
        }
    }