/// <summary> /// Handles addition of new tracked object if user is using Unity triggers. /// </summary> private void handleTriggerAddition(GameObject trackedObject, WorldMonitors owner, string objectAffiliation, float threshold) { if (!trackedObject.GetComponent <TrackedObjectTriggers>()) { trackedObject.AddComponent <TrackedObjectTriggers>(); } TrackedObjectTriggers tot = trackedObject.GetComponent <TrackedObjectTriggers>(); GameObjectIDReference.Add(trackedObject, AllocationSpace); gameObjectReference.Add(AllocationSpace, trackedObject); TrackedObjectData TOData = new TrackedObjectData { Object = trackedObject, Threshold = TriggersMimicOctree ? 0.75f * threshold : threshold, ObjectOwners = new List <WorldMonitors>() }; if (owner) { TOData.ObjectOwners.Add(owner); tot.AcceptOwner(owner); } TrackedObjectDataRef.Add(AllocationSpace, TOData); TrackedObjectAffiliations.Add(AllocationSpace, objectAffiliation); TotalTrackedObjects++; AllocationSpace++; tot.Initialize(); }
/// <summary> /// Handles object removal when user is using Unity triggers /// </summary> private void handleTriggerRemoval(GameObject trackedObject, WorldMonitors whoToRemove = default(WorldMonitors), bool retainOtherTrackers = default(bool)) { if (whoToRemove != default(WorldMonitors)) { gameObject.GetComponent <TrackedObjectTriggers>().LoseOwner(whoToRemove); if (retainOtherTrackers) { return; } } TrackedObjectTriggers tot = gameObject.GetComponent <TrackedObjectTriggers>(); if (tot) { tot.wms = new List <WorldMonitors>(); // empty it. } TotalTrackedObjects--; return; }
/// <summary> /// Use a tracked object's OnDestroy (or some other suitable method) to call this method upon removal of a tracked object. /// </summary> /// <param name="trackedObject">The object to be tracked.</param> /// <param name="owner">The agent tracking this object.</param> /// <param name="whoToRemove">Optional - identify a particular WorldMonitors to remove while leaving other trackers in place.</param> /// <param name="retainOtherTrackers">Optional - if selected, only the specified WorldMonitors (agent) will be removed from the Object's tracker list.</param> /// <remarks>Due to the cost associated with this operation, perform minimal additions per frame or run from coroutine</remarks> public void RemoveTrackedObject(GameObject trackedObject, WorldMonitors whoToRemove = default(WorldMonitors), bool retainOtherTrackers = default(bool)) { if (usingTriggers) { handleTriggerRemoval(trackedObject, whoToRemove, retainOtherTrackers); return; } int removalID; GameObjectIDReference.TryGetValue(trackedObject, out removalID); if (whoToRemove != default(WorldMonitors)) { TrackedObjectDataRef[removalID].ObjectOwners.Remove(whoToRemove); if (TrackedObjectDataRef[removalID].ObjectOwners.Count == 0) { Octree.PointOctree.Remove(removalID); } else if (retainOtherTrackers) { return; } } else { Octree.PointOctree.Remove(removalID); } Octree.MasterList[removalID] = new List <int>(); // to reinsert later, TrackedObjectDataRef needs updated. TrackedObjectDataRef.Remove(removalID); /* * TODO: test -- FreeSpace = MaximumObjectsAllowed - AllocationSpace >= 0; * allow for space to be opened back up -- may be LINQ intensive */ TotalTrackedObjects--; }
private void OnEnable() { instance = target as WorldMonitors; maxNumberTrackedFields = show.Length; numberTrackedFields = Mathf.Max(1, numberTrackedFields); if (instance.TrackedObjects.Count == 0) { instance.TrackedObjects.Add(new TrackedObjectContainer()); } base.Enable(); if (!OTIEditorUtility.Instance) { OTIEditorUtility AIE = ScriptableObject.CreateInstance <OTIEditorUtility>(); OTIEditorUtility.Instance = AIE; } serializedObject.ApplyModifiedProperties(); }
/// <summary> /// Runtime objects should be inserted into the tracking system here. /// </summary> /// <param name="trackedObject">The object to be tracked.</param> /// <param name="owner">Provide the WorldMonitors component from the agent tracking this object.</param> /// <param name="objectAffiliation">The class of objects this item is in (e.g. "A", "B", etc.)</param> /// <param name="threshold">Regardless of class type, this object can be inserted with its own threshold size.</param> /// <remarks>Due to the cost associated with this operation, perform minimal additions per frame or run from coroutine</remarks> public void InsertNewTrackedObject(GameObject trackedObject, WorldMonitors owner, string objectAffiliation, float threshold) { if (!FreeSpace) { return; } if (usingTriggers) { handleTriggerAddition(trackedObject, owner, objectAffiliation, threshold); FreeSpace = MaximumObjectsAllowed - AllocationSpace > 0; return; } /* * Do not need to add directly into Octree * Happens in the first update saving time from main thread */ int id; if (GameObjectIDReference.TryGetValue(trackedObject, out id)) { // allow for user to add new trackers to one object TrackedObjectData TOData; TrackedObjectDataRef.TryGetValue(id, out TOData); if (owner) // else the user has tried to add a non-owned object more than once { TOData.ObjectOwners.Add(owner); } } else { GameObjectIDReference.Add(trackedObject, AllocationSpace); gameObjectReference.Add(AllocationSpace, trackedObject); TrackedObjectData TOData = new TrackedObjectData { Object = trackedObject, Threshold = OctreeMimicTriggerInteraction ? 3 / 2 * threshold : threshold, ObjectOwners = new List <WorldMonitors>() }; if (owner) { TOData.ObjectOwners.Add(owner); } Octree.MasterList.Add(AllocationSpace, new List <int>()); TrackedObjectDataRef.Add(AllocationSpace, TOData); TrackedObjectAffiliations.Add(AllocationSpace, objectAffiliation); TotalTrackedObjects++; AllocationSpace++; Octree.TrackedObjectStates = TrackedObjectStates; FreeSpace = MaximumObjectsAllowed - AllocationSpace >= 0; } }
/// <summary> /// Agents notify objects when they are destroyed /// </summary> public void LoseOwner(WorldMonitors wm) { Owners--; wms.Remove(wm); }
/// <summary> /// Agents who find this object in a field will add themselves as owners here /// </summary> public void AcceptOwner(WorldMonitors wm) { Owners++; // quicker than wms.Count wms.Add(wm); }