Пример #1
0
        // Assign the objects in a list to the FormationGridPoint(s) in the gridPoints list
        public bool AssignObjectsToGrid(List <GameObject> list)
        {
            bool result = true;

            if (list.Count > gridPoints.Count)
            {
                Debug.LogWarning("FormationGrid.AssignObjectsToGrid(): too many objects for this grid.");
                result = false;
            }

            for (int i = 0; i < list.Count; i++)
            {
                if (i < gridPoints.Count)
                {
                    GameObject go = list[i];

                    // Now check if the required components are available so we can move the objects
                    if (movementType == MovementType.CharacterController)
                    {
                        CharacterController cc = go.GetComponent <CharacterController>();
                        if (!cc)
                        {
                            Debug.LogError("FormationGrid.AssignObjectsToGrid(): GameObject to be assigned does not have the required CharacterController for this movement type.");
                        }
                    }
                    else if (movementType == MovementType.RigidBody)
                    {
                        Rigidbody rb = go.GetComponent <Rigidbody>();
                        if (!rb)
                        {
                            Debug.LogError("FormationGrid.AssignObjectsToGrid(): GameObject to be assigned does not have the required RigidBody for this movement type.");
                        }
                    }



#if T7T_ASTAR
                    AIPath aip = go.GetComponent <AIPath>();
                    if (aip)
                    {
                        FormationGridPoint fgp = gridPoints[i];
                        fgp.AssignUnit(go);

                        aip.target  = fgp.GetTransform();
                        aip.canMove = true;

                        Debug.Log("FormationGrid.AssignObjectsToGrid(): Assigned new target to object " + go.transform.name);
                    }
                    else
                    {
                        Debug.LogWarning("FormationGrid.AssignObjectsToGrid(): Assigned Object [" + go.transform.name + "] has no AIPath component.");
                        result = false;
                    }
#else
                    NavMeshAgent nma = go.GetComponent <NavMeshAgent>();
                    if (nma)
                    {
                        FormationGridPoint fgp = gridPoints[i];
                        fgp.AssignUnit(go);

                        nma.destination = fgp.GetPosition();

                        Debug.Log("FormationGrid.AssignObjectsToGrid(): Assigned new target to object " + go.transform.name);
                    }
                    else
                    {
                        Debug.LogWarning("FormationGrid.AssignObjectsToGrid(): Assigned Object [" + go.transform.name + "] has no Navmesh component.");
                        result = false;
                    }
#endif
                }
            }

            return(result);
        }