Пример #1
0
    public void CreateMap()
    {
        float mapSize = GameObject.Find("MapSize").GetComponent <Slider>().value;

        mapWidth = Mathf.RoundToInt(mapSize);
        if (mapWidth % 2 == 0)
        {
            mapWidth = mapWidth + 1;
        }
        castleNumber = numberOfGerm();
        tileLoader   = new TileLoader();
        tileLoader.ConstructTileTab();

        int heightColumn = 3;

        mapColumnSize.Add(heightColumn);
        float positionX = -((mapWidth - 1) / 2);
        float positionZ = -1;

        //Initialisation de la map
        map = new List <TileForGeneration[]>();
        for (int width = 0; width < mapWidth; width++)
        {
            map.Add(new TileForGeneration[heightColumn]);
            if (width <= mapWidth / 2)
            {
                heightColumn += 1;
                mapColumnSize.Add(heightColumn);
            }
            else
            {
                heightColumn -= 1;
                mapColumnSize.Add(heightColumn);
            }
        }
        heightColumn = 3;

        for (int width = 0; width < mapWidth; width++)
        {
            for (int height = 0; height < heightColumn; height++)
            {
                Vector3 position = new Vector3(positionX, 0, positionZ + height);
                map[width][height]                = new TileForGeneration();
                map[width][height].position       = position;
                map[width][height].name           = "Tile" + width + height;
                map[width][height].stratElt       = strategic.nothing;
                map[width][height].neighbors      = new List <TileForGeneration>();
                map[width][height].neighborsNames = new List <string>();
                map[width][height].gameObject     = null;
            }
            positionX += 0.86f;
            if (positionX <= 0)
            {
                positionZ    -= 0.5f;
                heightColumn += 1;
            }
            else
            {
                positionZ    += 0.5f;
                heightColumn -= 1;
            }
        }


        placeGerm();
        placeCastle();
        placeResources();
        Map.instance.InitializeMap(mapWidth);
        //Construction de la map
        positionX    = -((mapWidth - 1) / 2);
        positionZ    = -1;
        heightColumn = 3;
        for (int width = 0; width < mapWidth; width++)
        {
            for (int height = 0; height < heightColumn; height++)
            {
                map[width][height].style = belongsZone(map[width][height]);
                string path = tileLoader.ChooseTile(map[width][height]);

                string controller;
                //Tile attribution to players
                if ((width == 0 && height == 0) || (width == 0 && height == 1))
                {
                    controller = "redPlayer";
                }
                else if ((width == mapWidth - 1 && height == 0) || (width == mapWidth - 1 && height == 1))
                {
                    controller = "bluePlayer";
                }
                else
                {
                    controller = "neutral";
                }

                DetermineNeighbors(map[width][height], width, height, heightColumn);

                //Préparation des datas pour initialisation des Tiles chez le client
                int      sizeNeighborsList = map[width][height].neighbors.Count;
                string[] neighborsName     = new string[sizeNeighborsList];
                for (int i = 0; i < sizeNeighborsList; i++)
                {
                    neighborsName[i] = map[width][height].neighbors[i].name;
                }

                object[] tileInitDataForClient = new object[7 + neighborsName.Length];
                tileInitDataForClient[0] = map[width][height].style;
                tileInitDataForClient[1] = map[width][height].name;
                tileInitDataForClient[2] = map[width][height].stratElt.ToString();
                tileInitDataForClient[3] = controller;
                tileInitDataForClient[4] = map[width][height].foodIncome;
                tileInitDataForClient[5] = map[width][height].goldIncome;
                tileInitDataForClient[6] = map[width][height].strategicIncome;

                for (int i = 7; i < 7 + neighborsName.Length; i++)
                {
                    tileInitDataForClient[i] = neighborsName[i - 7];
                }

                map[width][height].gameObject = PhotonNetwork.InstantiateSceneObject(path, map[width][height].position, Quaternion.Euler(0, -30, 0), 0, tileInitDataForClient);

                map[width][height].gameObject.transform.parent     = mapGroup.transform;
                map[width][height].gameObject.transform.localScale = new Vector3(1, 1, 1);
                map[width][height].gameObject.name = map[width][height].name;

                //Initialisation tiles coté serveur
                Tile tileScript = map[width][height].gameObject.GetComponent <Tile>();
                tileScript.neighboursName = new List <string>();
                foreach (string tilename in neighborsName)
                {
                    tileScript.neighboursName.Add(tilename);
                }
                tileScript.InitializeTile(map[width][height].style, map[width][height].stratElt, map[width][height].foodIncome,
                                          map[width][height].goldIncome, map[width][height].strategicIncome);
                Map.instance.tileList.Add(tileScript);
            }

            positionX += 0.86f;
            if (positionX <= 0)
            {
                heightColumn += 1;
            }
            else
            {
                heightColumn -= 1;
            }
        }

        Map.instance.InitializeTargetingSphere();

        foreach (Tile tile in Map.instance.tileList)
        {
            tile.CreateNeighboursList();
        }
    }