Beispiel #1
0
        public override void OnInspectorGUI()
        {
            OTIEditorUtility.Instance.OTIHead("Agent World Monitoring", _WorldMonitoringExplanation);
            OTIEditorUtility.Instance.HorizontalLine();

            numberTrackedFields = Mathf.Max(1, instance.TrackedObjects.Count);

            if (numberTrackedFields >= 701)
            {
                Debug.LogWarning("701 tracked fields is the limit, additional fields must be added at runtime.");
            }

            if (instance.TrackedObjects.Count == 0)
            {
                instance.TrackedObjects.Add(new TrackedObjectContainer());
            }

            if (instance.ThresholdSet.Count == 0)
            {
                instance.ThresholdSet.Add(new float());
            }

            addMinusIntValueButtons(ref numberTrackedFields, ref instance.TrackedObjects, ref instance.ThresholdSet, "Add Tracked Field", "Remove Tracked Field", maxNumber: maxNumberTrackedFields);

            if (instance.ThresholdSet.Count > 0)
            {
                float tThreshold = instance.ThresholdSet[0];
                OTIEditorUtility.Instance.HorizontalLineProperty(ref instance.ThresholdSet, 0, "Tracked Object Set A", "Threshold Distance A", "Assign objects for tracking against each other. Set A will be tracked against all others.");
                trackedObjectListManager(0, guiContent, headingStyle, subHeadingStyle, "", "Tracked Objects", ref show[0], padding: 1);

                if (instance.ThresholdSet[0] != tThreshold)
                {
                    enforceStaticThreshold(0, instance.ThresholdSet[0]);
                }

                for (int i = 1; i < numberTrackedFields; i++)
                {
                    tThreshold = instance.ThresholdSet[i];
                    OTIEditorUtility.Instance.HorizontalLineProperty(ref instance.ThresholdSet, i, "Tracked Object Set " + OTIUtilities._AlphabetAssembler(i), "Threshold Distance " + OTIUtilities._AlphabetAssembler(i), "Assign objects for tracking against each other. Set " + OTIUtilities._AlphabetAssembler(i) + " will be tracked against all others.");
                    trackedObjectListManager(i, guiContent, headingStyle, subHeadingStyle, "", "Tracked Objects", ref show[i], padding: 1);
                    serializedObject.ApplyModifiedProperties();

                    if (instance.ThresholdSet[i] != tThreshold)
                    {
                        enforceStaticThreshold(i, instance.ThresholdSet[i]);
                    }
                }
            }

            serializedObject.ApplyModifiedProperties();
        }
        public void Start()
        {
            usingTriggers = TrackingMode == TrackingMode.UnityTriggers;

            agentMonitors = GameObject.FindObjectsOfType <WorldMonitors>();

            /*
             * Start procedure is O(i*j*k) and a faster solution may exist
             */

            for (int i = 0; i < agentMonitors.Length; i++)
            {
                for (int j = 0; j < agentMonitors[i].TrackedObjects.Count; j++)
                {
                    for (int k = 0; k < agentMonitors[i].TrackedObjects[j].TrackedObjects.Count; k++)
                    {
                        // double threshold size if user wishes for point octree to mimic trigger interaction distances
                        float threshold = (OctreeMimicTriggerInteraction && TrackingMode == TrackingMode.Octree) ? agentMonitors[i].ThresholdSet[j] * 3 / 2 : agentMonitors[i].ThresholdSet[j];

                        GameObject go = agentMonitors[i].TrackedObjects[j].TrackedObjects[k];

                        if (go) // allows user to leave empty gameobject slots in tracked object inspector
                        {
                            int id;
                            if (GameObjectIDReference.TryGetValue(go, out id))
                            {
                                TrackedObjectData TOData;
                                TrackedObjectDataRef.TryGetValue(id, out TOData);
                                TOData.ObjectOwners.Add(agentMonitors[i]);
                                TrackedObjectDataRef[id] = TOData;

                                if (usingTriggers)
                                {
                                    // WorldMonitors will declare ownership in its Start() method

                                    if (!go.GetComponent <TrackedObjectTriggers>())
                                    {
                                        go.AddComponent <TrackedObjectTriggers>();
                                    }

                                    go.GetComponent <TrackedObjectTriggers>().Initialize();
                                }
                            }
                            else
                            {
                                GameObjectIDReference.Add(go, TotalTrackedObjects); //object ID = current number of tracked objects
                                gameObjectReference.Add(TotalTrackedObjects, go);   //using IDs necessary to run aux thread

                                TrackedObjectData TOData = new TrackedObjectData
                                {
                                    Object       = go,
                                    Threshold    = threshold,
                                    ObjectOwners = new List <WorldMonitors>()
                                };

                                TOData.ObjectOwners.Add(agentMonitors[i]);
                                TrackedObjectDataRef.Add(TotalTrackedObjects, TOData);
                                TrackedObjectAffiliations.Add(TotalTrackedObjects, OTIUtilities._AlphabetAssembler(j));

                                if (usingTriggers)
                                {
                                    if (!go.GetComponent <TrackedObjectTriggers>())
                                    {
                                        go.AddComponent <TrackedObjectTriggers>();
                                    }
                                    go.GetComponent <TrackedObjectTriggers>().Initialize();
                                }

                                TotalTrackedObjects++;
                            }
                        }
                    }
                }
            }

            AllocationSpace = TotalTrackedObjects;
            FreeSpace       = MaximumObjectsAllowed - AllocationSpace >= 0;

            if (usingTriggers || ExhaustiveMethod)
            {
                return; // no more set up required; switching between modes is not allowed at runtime.
            }
            Octree = new Octree();

            //configure tracked object states at start
            for (int i = 0; i < TotalTrackedObjects; i++)
            {
                List <int> locals = new List <int>();

                if (Octree.MasterList.ContainsKey(i))
                {
                    Octree.MasterList[i] = locals;
                }
                else
                {
                    Octree.MasterList.Add(i, locals);
                }
            }

            OctreeThreadParameters otp = new OctreeThreadParameters
            {
                ObjectIDs           = new List <int>(TrackedObjectDataRef.Keys),
                TotalTrackedObjects = TotalTrackedObjects,
                Coordinates         = getUpdatedPositions(new List <int>(TrackedObjectDataRef.Keys)),
                DynamicObjects      = TrackedObjectAffiliations
            };

            //construct initial octree
            Octree.Initialize(InitialWorldSize, WorldOrigin, MinimumObjectSize);

            Octree.IsDone = true; //allows an initial pass into job start
            Octree.ThreadOctreeInit(otp, RestrictToMainThread);

            while (!Octree.UpdateOctree())
            {
            }                                  //wait until conflict states are established

            TrackedObjectStates = Octree.TrackedObjectStates;

            // wipe initial results so conflicts existing before start register
            for (int i = 0; i < Octree.MasterList.Count; i++)
            {
                Octree.MasterList[i] = new List <int>();
            }
        }