Esempio n. 1
0
    //use this to Load a map to be made in to a prefab
    public void LoadMap(string name)
    {
        var allMaps = GameManager.Instance.GetComponent <Maps>().allMapsDict;



        mapName = name;

        mapInformation.playerEnergy     = 10;
        mapInformation.playerEnergyMax  = 100;
        mapInformation.mapHealthMax     = allMaps[mapName].maxHP;
        mapInformation.mapHealthCurrent = allMaps[mapName].maxHP;



        if (allMaps.ContainsKey(mapName))
        {
            mapCode = allMaps[mapName].mapCode;



            //get a list of all possible path codes from the map data and creates a list of them
            foreach (string pathCode in allMaps[mapName].pathCode)
            {
                pathCodes.Add(pathCode);
            }
            //pathCode = allMaps[mapName].pathCode;
            enemyMax      = allMaps[mapName].enemyMax;
            levelMin      = allMaps[mapName].levelMin;
            levelMax      = allMaps[mapName].levelMax;
            spawnInterval = allMaps[mapName].spawnInterval;
            width         = allMaps[mapName].width;
            height        = allMaps[mapName].height;
            mapLevel      = allMaps[mapName].mapLevel;
            mapWeather    = allMaps[mapName].weather;



            columns = int.Parse(width.ToString()) / 50;
            rows    = int.Parse(height.ToString()) / 50;

            //add the IDs for the possible enemies this map can have
            foreach (int enemy in allMaps[mapName].enemies)
            {
                enemies.Add(enemy);
            }

            //add the IDs for the possible enemies and their spawn rates this map can have
            for (int e = 0; e < allMaps[mapName].enemies.Length; e++)
            {
                spawnRates.Add(allMaps[mapName].enemies[e], allMaps[mapName].enemyChance[e]);
            }


            int charCount = 0;

            for (int i = 1; i < rows * 2; i++)
            {
                //Debug.Log(i);

                for (int c = 1; c <= columns; c++)
                {
                    var tile  = Instantiate(mapTile, transform.position, Quaternion.identity);
                    var tile2 = Instantiate(mapTile, transform.position, Quaternion.identity);



                    //tile.GetComponent<MapTile>().GetType(int.Parse(chars[charCount]));

                    tile.GetComponent <MapTile>().tileNumber = tileNumber;
                    tile.name = tileNumber.ToString();
                    tile.GetComponent <MapTile>().info.row    = c * 2;
                    tile.GetComponent <MapTile>().info.column = i * 2 - 1;

                    tile.GetComponent <MapTile>().mapDetails  = this;
                    tile2.GetComponent <MapTile>().mapDetails = this;


                    tileNumber += 1;


                    charCount += 1;


                    //tile2.GetComponent<MapTile>().GetType(int.Parse(chars[charCount]));

                    tile2.GetComponent <MapTile>().tileNumber = tileNumber;
                    tile2.name = tileNumber.ToString();
                    tile2.GetComponent <MapTile>().info.row    = c * 2 - 1;
                    tile2.GetComponent <MapTile>().info.column = i * 2;

                    tileNumber += 1;



                    tile.transform.position  = new Vector2((-width / 2) + (i * 50), (height / 2) - (c * 25));
                    tile2.transform.position = new Vector2((-width / 2) + (i * 50) + 25, (height / 2) - (c * 25) + 12.50f);


                    //tile.transform.position = new Vector2((-width / 2) + (i * 75) - 25, (height / 2) - (c * 25));
                    //tile2.transform.position = new Vector2((-width / 2) + (i * 75) + 25, (height / 2) - (c * 25) + 12.50f);


                    tile.GetComponent <SpriteRenderer>().sortingOrder         = (int)-tile.transform.position.y;
                    tile.GetComponent <MapTile>().roadSprite.sortingLayerName = "Pathways";
                    tile.GetComponent <MapTile>().roadSprite.sortingOrder     = 2 + (int)-tile.transform.position.y;



                    tile2.GetComponent <SpriteRenderer>().sortingOrder         = (int)-tile2.transform.position.y;
                    tile2.GetComponent <MapTile>().roadSprite.sortingLayerName = "Pathways";
                    tile2.GetComponent <MapTile>().roadSprite.sortingOrder     = 2 + (int)-tile2.transform.position.y;

                    tile.transform.SetParent(mapCanvas.transform, false);

                    tile2.transform.SetParent(mapCanvas.transform, false);


                    charCount += 1;
                }
            }
            //set the tile attributes based on their attribute code
            string[] tileChars = new string[mapCode.Length];
            int      g         = 1;


            for (int t = 0; t < mapCode.Length / 2; t++)
            {
                tileChars[t] = mapCode[g - 1].ToString() + mapCode[g].ToString();
                g           += 2;
                var tile = GameObject.Find(t.ToString()).GetComponent <MapTile>();
                tile.GetAttribute(int.Parse(tileChars[t]));
                tile.Build();

                //add the tile to the list of active tiles
                GameManager.Instance.activeTiles.Add(t, tile.GetComponent <MapTile>());
                //set the level and EXP of the tile
                tile.SetLevel(mapLevel);
            }

            //make a path code for each possible path, and add them to a Dictionary of PathCodes
            for (int p = 0; p < pathCodes.Count; p++)
            {
                //break up each path code in to sections of 3, since each tile is a 3 digit number, and store them in a dictionary of path codes that an enemy will choose at random upon their spawn
                //string[] pathChars = new string[pathCode.Length];
                string[]       pathChars = new string[pathCodes[p].Length];
                string         code      = pathCodes[p];
                List <string>  pathCode  = new List <string>();
                List <MapTile> pathTiles = new List <MapTile>();

                //make sure to clear this if there is more than 1 path so that the game knows to generate more than just one path
                path.Clear();

                int h = 2;
                for (int i = 0; i < code.Length / 3; i++)
                {
                    pathChars[i] = code[h - 2].ToString() + code[h - 1].ToString() + code[h].ToString();
                    h           += 3;
                    int tileCheck = int.Parse(pathChars[i]);


                    //path[i] = GameObject.Find(tileCheck.ToString()).GetComponent<MapTile>();


                    //path[i] = allTiles[tileCheck];
                    path.Add(allTiles[tileCheck]);
                    path[i].Road();
                    pathTiles.Add(path[i]);

                    ////add the first tile in each path to a dictionary, so the spawning monster knows where to spawn
                    //if (i == 0)
                    //{
                    //    spawnPoints.Add(p, path[i].transform.position);
                    //}

                    if (i > 0)
                    {
                        //get the direction of the road sprite from this class
                        //TileSprite tileSprite = new TileSprite(path[i], path[i - 1], "road");
                        TileSprite tileSprite = new TileSprite(pathTiles[i], pathTiles[i - 1], "road");
                    }
                }
            }



            //pathEnd.transform.position = new Vector2(pathEndX, pathEndY);
            InvokeRepeating("SpawnEnemy", 4f, spawnInterval);
        }


        GetComponentInParent <MonsterInfoMenus>().LoadYourTowers();
        weatherSystem.intensity = UnityEngine.Random.Range(0, 3);
        weatherSystem.StartWeather(this);
    }