Beispiel #1
0
    async void MapBackgroundGenerator()
    {
        try
        {
//            Debug.Log("Task");
            while (true)
            {
                //Debug.Log("Stage-1");
                WorldMapCell worldMapCell = null;
                //  Debug.Log("Stage-0.5");
                try
                {
                    worldMapCell = GeneratorQuery[0];
                }
                catch (System.Exception ex)
                {
                    Debug.Log(ex.Message);
                }
                //Debug.Log("Stage-0.1");
                Generator generator1 = new Generator();
                //Debug.Log("Stage0");
                await Task.Run(() => {
                    generator1.Map = loader.GetNeighborWorldMapCell(new Vector2(worldMapCell.PosX, worldMapCell.PosY));
                    //  Debug.Log("Stage1");
                    generator1.Build(5);
                    //Debug.Log("Stage2");
                    generator1.ConnectCaves();
                    //Debug.Log("Stage3");
                    generator1.ConnectCaves();
                    //Debug.Log("Stage4");
                    generator1.EmptyCellSet();
                    //Debug.Log("Stage5");
                    generator1.EndGeneration();
                    loader.MapGenered(worldMapCell, generator1.ResultMap);
                    //       loader.MapAccess(worldMapCell);
                });

                GeneratorQuery.Remove(worldMapCell);
                if (GeneratorQuery.Count == 0)
                {
                    GeneratorQuery.Add(GetCell());
                }
                if (GeneratorQuery[0] == null)
                {
                    //Debug.Log("QWERTYUIOP");
                    return;
                }
                if (OnGenerationEnded != null)
                {
                    GenerationCallback callback = new GenerationCallback();
                    callback.Map     = generator1.ResultMap;
                    callback.mapCell = worldMapCell;
                    OnGenerationEnded(callback);
                }
                cellcount = GeneratorQuery.Count;


                //   Debug.Log("Stage6");
                if (Stop)
                {
                    return;
                }
            }
        }
        catch (System.Exception ex)
        {
            Debug.Log(ex.Message);
        }
    }
Beispiel #2
0
    public static void GenerateField(LevelData levelData, GameObject emptyElementField, GenerationCallback callback)
    {
        Vector2 beginCamera = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, 0));
        Vector2 endCamera   = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, 0));
        Rect    cameraRect  = new Rect(beginCamera, endCamera - beginCamera);
        float   width       = cameraRect.width / (levelData.levelWidth + 1);
        float   height      = cameraRect.height / (levelData.levelHeight + 1);
        float   offsetX     = cameraRect.x + width;
        float   offsetY     = cameraRect.y + height;

        if (width > height)
        {
            offsetX += (width - height) * levelData.levelWidth / 2;
            width    = height;
        }
        else if (width < height)
        {
            offsetY += (height - width) * levelData.levelHeight / 2;
            height   = width;
        }
        for (int x = 0; x < levelData.levelWidth; x++)
        {
            for (int y = 0; y < levelData.levelHeight; y++)
            {
                GameObject element = Instantiate(emptyElementField, new Vector3(offsetX + x * width, offsetY + y * height), Quaternion.identity);
                Rect       r       = element.GetComponent <RectTransform>().rect;
                element.transform.localScale = new Vector3(width / r.width, height / r.height, 1);
                callback(element, levelData, x, levelData.levelHeight - y - 1);
            }
        }
    }
Beispiel #3
0
    public void CreateWorldMapCell(int DX, int DY, MapRogulikeGenerator myCanvas, Vector3 position)
    {
        //Debug.Log("HOLHELL");

        //if (X < 0 || Y < 0 || X > 100 || Y > 100)
        //{
        //    Debug.Log("ERROR:CreateWorldMapCell Uncorrect (WorldState.css)");
        //    return;
        //}
        WorldMapCell tmpCell    = myCanvas.ThisCell;
        WorldMapCell targetCell = StaticData.MapData[tmpCell.PosY + DY][tmpCell.PosX + DX];

        for (int i = 0; i < MapCells.Length; i++)
        {
            if (MapCells[i] == null)
            {
                continue;
            }
            if (MapCells[i].transform.position == myCanvas.transform.position + new Vector3(DX * StaticData.WorldCellSize, DY * -StaticData.WorldCellSize))
            {
                if (!MapCells[i].Setted)
                {
                    return;
                }
                if (!MapCells[i].Setted && targetCell.Accesed)
                {
                    MapCells[i].Setter(loader, targetCell);
                }
                else if (!MapCells[i] && targetCell.Generated && !tmpCell.GetKey(DX, DY).gameObject.active)
                {
                    tmpCell.GetKey(DX, DY).gameObject.SetActive(true);
                    tmpCell.GetKey(DX, DY).Set(MapCells[i], targetCell, loader);
                }
                return;
            }
        }

        //Debug.Log("Creating new cell");
        MapRogulikeGenerator MapCell = Instantiate(WorldCellPerhub);

        MapCell.transform.SetParent(gameObject.transform);
        MapCell.transform.position = myCanvas.transform.position + new Vector3(DX * StaticData.WorldCellSize, DY * -StaticData.WorldCellSize);
        if (StaticData.MapData[tmpCell.PosY + DY][tmpCell.PosX + DX].Accesed)
        {
            MapCell.Setter(loader, StaticData.MapData[tmpCell.PosY + DY][tmpCell.PosX + DX]);
        }
        else if (StaticData.MapData[tmpCell.PosY + DY][tmpCell.PosX + DX].Generated)
        {
            Debug.Log("yy .gg");
            if (tmpCell.GetKey(DX, DY) != null)
            {
                Debug.Log("pesda1 .gg");
                tmpCell.GetKey(DX, DY).gameObject.SetActive(true);
                tmpCell.GetKey(DX, DY).Set(MapCell, StaticData.MapData[tmpCell.PosY + DY][tmpCell.PosX + DX], loader);
            }
        }
        else if (!StaticData.MapData[tmpCell.PosY + DY][tmpCell.PosX + DX].Generated)
        {
            //Debug.LogWarning("Adding to list " + tmpCell.PosY + DY + "  " + tmpCell.PosX + DX);
            GeneratorQuery.Add(StaticData.MapData[tmpCell.PosY + DY][tmpCell.PosX + DX]);
            OnGenerationEnded += (x) =>
            {
                Debug.Log("genered .gg");
                GenerationCallback callback = (GenerationCallback)x;
                if (callback.mapCell == StaticData.MapData[tmpCell.PosY + DY][tmpCell.PosX + DX])
                {
                    //MapCell.Setter(loader, StaticData.MapData[tmpCell.PosY + DY][tmpCell.PosX + DX]);
                    if (tmpCell.GetKey(DX, DY) != null)
                    {
                        Debug.Log("pesda2 .gg");
                        tmpCell.GetKey(DX, DY).gameObject.SetActive(true);
                        tmpCell.GetKey(DX, DY).Set(MapCell, StaticData.MapData[tmpCell.PosY + DY][tmpCell.PosX + DX], loader);
                    }
                }
            };
        }
        //Debug.Log($"Coordinate:{DY}, DX:{DX}");
        //Debug.Log($"ResultCoordinate:{tmpCell.PosY + DY},:{tmpCell.PosX + DX}");
        //Debug.Log($"MessageThis:{StaticData.MapData[tmpCell.PosY + DY][tmpCell.PosX + DX].Message}");
        //Debug.Log($"MessageThis:{StaticData.MapData[tmpCell.PosY + DY][tmpCell.PosX + DX].PosY}");
        AddMapCell(MapCell, position);
    }