Ejemplo n.º 1
0
        void Awake()
        {
            Toolbox toolbox = Toolbox.Instance;

            toolbox.allFormations.Add(this);

            if (anchor == null)
            {
                // TODO: alternatively create the anchor here (from prefab?)
                Debug.LogError("FormationGrid.Awake(): anchor not assigned");
                return;
            }
            else
            {
                // Move anchor align with FormationGrid (this) position
                anchor.transform.position = transform.position;
            }

            // Cache the FormationAnchor component from the anchor
            formationAnchor = anchor.GetComponent <FormationAnchor>();
            if (formationAnchor == null)
            {
                Debug.LogError("FormationGrid.Awake(): FormationAnchor component missing on anchor");
                return;
            }
            else
            {
                // Assign a reference to this FormationGrid in the FormationAnchor
                formationAnchor.SetFormation(this);
            }

            if (hasSound)
            {
                audioSource = GetComponent <AudioSource>();
                if (audioSource == null)
                {
                    Debug.LogError("FormationGrid.Awake(): AudioSource component missing on Formation Grid");
                    return;
                }
            }

            // Assign a value (minimum re randomization time difference) to the next re randomization time difference.
            // This time difference is changed everytime a re randomization of the grid points take place to prevent a pattern in the random behaviour.
            reRandomizeNextTime = reRandomizeTimeMin;

            // Cache the object this script is attached to
            formation = transform.gameObject;

            // Setup the grid based on the selected property
            ChangeGridTo(gridType);

            // Change the state to the initial form state
            // ChangeState(FormationStates.Form);
        }
Ejemplo n.º 2
0
        public void CreateNewFormation()
        {
            /*
             * STEP 1: Define where to place the formation and how to name it
             *
             *
             */

            // Use a Ray / Raycast from the middle of the Scene View to place the formation and its anchor on the plane/terrain/object it hits
            //
            // http://answers.unity3d.com/questions/48979/how-can-i-find-the-world-position-of-the-center-of.html
            Ray worldRay = SceneView.lastActiveSceneView.camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 1.0f));
            // http://josbalcaen.com/unity-editor-place-objects-by-raycast/
            Vector3    newPosition;
            RaycastHit hitInfo;

            // Shoot this ray. check in a distance of 10000.
            if (Physics.Raycast(worldRay, out hitInfo, 10000))
            {
                newPosition = hitInfo.point;
            }
            else
            {
                newPosition = new Vector3(0, 0, 0);
            }


            // We'll use the time to name the Formation and the Anchor
            float t = Time.time;
            // Time to string see: http://answers.unity3d.com/questions/45676/making-a-timer-0000-minutes-and-seconds.html

            /*
             * STEP 2: Create the Anchor
             *
             */

            GameObject anchor = new GameObject("Anchor " + string.Format("{0:0}:{1:00}.{2:0}",
                                                                         Mathf.Floor(t / 60),
                                                                         Mathf.Floor(t) % 60,
                                                                         Mathf.Floor((t * 10) % 10))
                                               );

            anchor.transform.position = newPosition; // new Vector3(0, 0, 0);
            anchor.transform.parent   = formationManager.transform;

            FormationAnchor fa = anchor.AddComponent <FormationAnchor>();

#if T7T_ASTAR
#else
            // anchor.AddComponent<NavMeshAgent>();     No need to add since RequireComponent in FormationAnchor's class definition

            NavMeshAgent nma = anchor.GetComponent <NavMeshAgent>();
            if (nma != null)
            {
                nma.radius = 0.1f;
                nma.height = 0.1f;

                nma.stoppingDistance = 1.0f;
            }
#endif

            GameObject sphere1 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            sphere1.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
            sphere1.transform.SetParent(anchor.transform);
            sphere1.transform.localPosition = new Vector3(0f, 0f, 0f);

            sphere1.transform.name = "empty";

            /*
             * STEP 3: Create the Formation and link the Anchor to it
             *
             *
             */

            GameObject formation = new GameObject("Formation " + string.Format("{0:0}:{1:00}.{2:0}",
                                                                               Mathf.Floor(t / 60),
                                                                               Mathf.Floor(t) % 60,
                                                                               Mathf.Floor((t * 10) % 10))
                                                  );
            formation.transform.position = newPosition;
            formation.transform.parent   = formationManager.transform;

            FormationGrid fg = formation.AddComponent <FormationGrid>();

            AudioSource aud = formation.AddComponent <AudioSource>();

            GameObject sphere2 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            sphere2.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
            sphere2.transform.SetParent(formation.transform);
            sphere2.transform.localPosition = new Vector3(0f, 0f, 0f);
            sphere2.transform.name          = "empty";

            //fg.ChangeGridTo(GridTypes.Wedge9);
            fg.SetAnchor(anchor);
        }