Esempio n. 1
0
        /* Change the grid to a new type:
         *      If the grid already has existing FormationGridPoints then
         *          Collect the assigned units
         *          Destroy the spheres in the FormationGridPoints to cleanup memory
         *      Clear the grid points list
         *      Setup the new grid points list
         *      Setup the new grid
         *          Assign the previously assigned units to the FormationGridPoints
         *      Calculate the new positions
         */

        public void ChangeGridTo(GridTypes gridtype)
        {
            gridType = gridtype;

            // Create the list for collecting the already assigned units
            List <GameObject> units = new List <GameObject>();

            Debug.Log("FormationGrid.ChangeGridTo(): change state to " + gridType);


            if (gridPoints != null)
            {
                if (gridPoints.Count > 0)
                {
                    // collect units assigned to gridpoint so we can reassign automatically after grid change

                    Debug.Log("FormationGrid.ChangeGridTo(): grid exists so check if it has assigned units");

                    for (int i = 0; i < gridPoints.Count; i++)
                    {
                        FormationGridPoint fgp = gridPoints[i];
                        GameObject         go  = fgp.GetAssignedUnit();
                        if (go)
                        {
                            units.Add(go);
                            Debug.Log("FormationGrid.ChangeGridTo(): found one unit " + go.name);
                        }
                    }

                    // destroy list items first: cleanup the spheres
                    for (int i = 0; i < gridPoints.Count; i++)
                    {
                        FormationGridPoint fgp = gridPoints[i];
                        fgp.DestroySphere();
                    }
                }
                gridPoints.Clear();
            }

            // Create a new list to completely start a new grid from scratch.
            gridPoints = new List <FormationGridPoint>();
            if (gridPoints == null)
            {
                Debug.LogError("FormationGrid.ChangeGridTo(): gridPoints not initialized");
                return;
            }

            // Setup the new grid for the new gridtype
            bool result = SetupGrid(gridtype);

            // Now add the units we has assigned to the previous grid
            if (units.Count > 0)
            {
                AssignObjectsToGrid(units);
            }

            // Calculate the real positions of the grid based on the offsets in the grid definition
            CalculatePositionsAllGridPoints();

            Debug.Log("FormationGrid.ChangeGridTo(): result SetupGrid()=" + result);
        }