Пример #1
0
    void Start()
    {
        // sequence to parse textfile
        string txtFileAsOneLine = mapTextFile.text;

        string[]      oneLine;
        List <string> txtFileAsLines = new List <string>();

        txtFileAsLines.AddRange(txtFileAsOneLine.Split("\n"[0]));
        oneLine = txtFileAsLines[0].Split(" "[0]);

        boardsize[0] = int.Parse(oneLine[0]);
        boardsize[1] = int.Parse(oneLine[1]);

        tileProperties = new int[boardsize[0], boardsize[1]];
        for (int j = 0; j < boardsize[1]; j++)
        {
            oneLine = txtFileAsLines[1 + j].Split(" "[0]);
            for (int i = 0; i < boardsize[0]; i++)
            {
                tileProperties[i, j] = int.Parse(oneLine[i]);
            }
        }

        // sequence to parse textfile. This time for heights
        txtFileAsOneLine = heightTextFile.text;
        txtFileAsLines   = new List <string>();
        txtFileAsLines.AddRange(txtFileAsOneLine.Split("\n"[0]));

        heightProperties = new float[boardsize[0], boardsize[1]];
        for (int j = 0; j < boardsize[1]; j++)
        {
            oneLine = txtFileAsLines[j].Split(" "[0]);
            for (int i = 0; i < boardsize[0]; i++)
            {
                heightProperties[i, j] = float.Parse(oneLine[i]) * 0.1f;
            }
        }

        tileMatrix     = new GameObject[boardsize[0], boardsize[1]];
        unitMatrix     = new GameObject[boardsize[0], boardsize[1]];
        nextStepMatrix = new GameObject[boardsize[0], boardsize[1]];
        hexMath        = gameObject.GetComponent <HexMath>(); // Takes the HexMath script from the Game Control

        // find the camera for the move to camera function
        Camera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent <CameraController>();


        for (int i = 0; i < boardsize[0]; i++) // Generate the X hexagons
        {
            float x = hexMath.matrix2HexX(i);

            for (int j = 0; j < boardsize[1]; j++) // Generate the Y hexagons
            {
                float y = hexMath.matrix2HexY(i, j);

                GameObject hexagon = Instantiate(HexagonTile);
                hexagon.transform.SetParent(transform);                                                               // Puts the tile under TileManager and gives the same transform
                hexagon.transform.localPosition = new Vector3(x, heightProperties[i, j], y);
                hexagon.transform.GetChild(0).transform.position += new Vector3(0, 0.5f - heightProperties[i, j], 0); // first child is undiscovered
                hexagon.GetComponent <HexagonScript>().set(i, j);

                if (tileProperties[i, j] == 0)
                {
                    //empty

                    hexagon.GetComponent <MeshRenderer>().enabled = false;
                    hexagon.GetComponent <MeshCollider>().enabled = false;
                    hexagon.transform.GetChild(0).GetComponent <MeshRenderer>().enabled = false;
                    hexagon.transform.GetChild(1).GetComponent <MeshRenderer>().enabled = false;
                }
                if (tileProperties[i, j] == 2)
                {
                    //mountain
                    GameObject mountain = Instantiate(mountainPrefab);
                    mountain.transform.SetParent(transform); // Puts the tile under TileManager and gives the same transform
                    mountain.transform.localPosition = new Vector3(x, 0, y);
                    hexagon.GetComponent <MeshCollider>().enabled = false;
                    hexagon.GetComponent <MeshRenderer>().enabled = false;
                }

                if (tileProperties[i, j] == 3)
                {
                    //forrest
                    GameObject forrest = Instantiate(forrestPrefab);
                    forrest.transform.SetParent(transform); // Puts the tile under TileManager and gives the same transform
                    forrest.transform.localPosition = new Vector3(x, 0, y);
                }

                // Create pathing ring on it instantly
                GameObject pathing = Instantiate(pathingRing);
                pathing.transform.SetParent(hexagon.transform);
                pathing.transform.localPosition = new Vector3(0, 0, 0.05f);
                pathing.GetComponent <MeshRenderer>().enabled = false;

                tileMatrix[i, j] = hexagon;
            }
        }

        int[] unitLoc = new int[2] {
            0, 0
        };
        // Spawn the first units

        Vector3[] angles =
        {
            new Vector3 {
                x = 0, y = 0, z = 0
            },
            new Vector3 {
                x = 0, y = 60, z = 0
            },
            new Vector3 {
                x = 0, y = 120, z = 0
            },
            new Vector3 {
                x = 0, y = 180, z = 0
            },
            new Vector3 {
                x = 0, y = 240, z = 0
            },
            new Vector3 {
                x = 0, y = 300, z = 0
            }
        };


        //player 1's units
        unitLoc = new int[2] {
            6, 18
        };
        GameObject Unit = Instantiate(Hoplite);

        Unit.GetComponent <UnitController>().set(unitLoc[0], unitLoc[1]);
        Unit.GetComponent <UnitController>().setTileGoal(unitLoc[0], unitLoc[1], 0);
        unitMatrix[unitLoc[0], unitLoc[1]] = Unit;
        Unit.transform.position            = tileMatrix[unitLoc[0], unitLoc[1]].transform.position;
        unitList.Add(Unit);
        Unit.GetComponent <UnitController>().setPlayerID(0);
        Unit.GetComponent <UnitController>().setTeamID(0);
        Unit.transform.eulerAngles = angles[0];

        unitLoc = new int[2] {
            2, 14
        };
        Unit = Instantiate(Hoplite);
        Unit.GetComponent <UnitController>().set(unitLoc[0], unitLoc[1]);
        Unit.GetComponent <UnitController>().setTileGoal(unitLoc[0], unitLoc[1], 0);
        unitMatrix[unitLoc[0], unitLoc[1]] = Unit;
        Unit.transform.position            = tileMatrix[unitLoc[0], unitLoc[1]].transform.position;
        unitList.Add(Unit);
        Unit.GetComponent <UnitController>().setPlayerID(0);
        Unit.GetComponent <UnitController>().setTeamID(0);
        Unit.transform.eulerAngles = angles[5];

        unitLoc = new int[2] {
            6, 6
        };
        Unit = Instantiate(Hoplite);
        Unit.GetComponent <UnitController>().set(unitLoc[0], unitLoc[1]);
        Unit.GetComponent <UnitController>().setTileGoal(unitLoc[0], unitLoc[1], 0);
        unitMatrix[unitLoc[0], unitLoc[1]] = Unit;
        Unit.transform.position            = tileMatrix[unitLoc[0], unitLoc[1]].transform.position;
        unitList.Add(Unit);
        Unit.GetComponent <UnitController>().setPlayerID(0);
        Unit.GetComponent <UnitController>().setTeamID(0);
        Unit.transform.eulerAngles = angles[4];

        //player 2's units
        unitLoc = new int[2] {
            14, 14
        };
        Unit = Instantiate(Hoplite);
        Unit.GetComponent <UnitController>().set(unitLoc[0], unitLoc[1]);
        Unit.GetComponent <UnitController>().setTileGoal(unitLoc[0], unitLoc[1], 0);
        unitMatrix[unitLoc[0], unitLoc[1]] = Unit;
        Unit.transform.position            = tileMatrix[unitLoc[0], unitLoc[1]].transform.position;
        unitList.Add(Unit);
        Unit.GetComponent <UnitController>().setPlayerID(1);
        Unit.GetComponent <UnitController>().setTeamID(1);
        Unit.transform.eulerAngles = angles[1];

        unitLoc = new int[2] {
            18, 6
        };
        Unit = Instantiate(Hoplite);
        Unit.GetComponent <UnitController>().set(unitLoc[0], unitLoc[1]);
        Unit.GetComponent <UnitController>().setTileGoal(unitLoc[0], unitLoc[1], 0);
        unitMatrix[unitLoc[0], unitLoc[1]] = Unit;
        Unit.transform.position            = tileMatrix[unitLoc[0], unitLoc[1]].transform.position;
        unitList.Add(Unit);
        Unit.GetComponent <UnitController>().setPlayerID(1);
        Unit.GetComponent <UnitController>().setTeamID(1);
        Unit.transform.eulerAngles = angles[2];

        unitLoc = new int[2] {
            14, 2
        };
        Unit = Instantiate(Hoplite);
        Unit.GetComponent <UnitController>().set(unitLoc[0], unitLoc[1]);
        Unit.GetComponent <UnitController>().setTileGoal(unitLoc[0], unitLoc[1], 0);
        unitMatrix[unitLoc[0], unitLoc[1]] = Unit;
        Unit.transform.position            = tileMatrix[unitLoc[0], unitLoc[1]].transform.position;
        unitList.Add(Unit);
        Unit.GetComponent <UnitController>().setPlayerID(1);
        Unit.GetComponent <UnitController>().setTeamID(1);
        Unit.transform.eulerAngles = angles[3];

        // Start the timer
        StartCoroutine(TurnTimer()); // Start the turntimer!!!
    }