//Checks if ordering is changed and sorts all mapObjects of same type in map by distance to chosen mapobject. MapObject and geoEntityType are given as parameters //Launches event if order is changed public void SortMapObjects(MapObject mapObject, GeoEntityType geoEntityType) { List <MapObject> objs; //mapObjects.TryGetValue(geoEntityType, out objs); currentFloorMapObjects.TryGetValue(geoEntityType, out objs); if (objs != null) { List <float> distances = new List <float>(); foreach (MapObject obj in objs) { float distSqr = (obj.MapPos - mapObject.MapPos).sqrMagnitude; distances.Add(distSqr); } float tempDist = 0; foreach (float dist in distances) { if (tempDist > dist) { objs.Sort((x, y) => (x.MapPos - mapObject.MapPos).sqrMagnitude.CompareTo((y.MapPos - mapObject.MapPos).sqrMagnitude)); mapObjectsSortingChanged(this, new EventArg <GeoEntityType, List <MapObject> >(geoEntityType, objs)); break; } tempDist = dist; } } }
void InitializeMaps() { //Actions that are launched when somthing Changes in virtual space or guide map. Used as parameters when PlaneMap.Init() is called EventHandler <EventArg <GeoEntityType, MapObject> > vClosestChangedAction = (sender, args) => { GeoEntityType geoEntityType = args.arg1; //Add more actions for different geoentity types here if needed switch (geoEntityType) { case GeoEntityType.viewpoint: closestVP = args.arg2.LinkedGeoEntity.gameObject; closestViewPointChanged(sender, new EventArg <GameObject>(closestVP)); break; default: break; } }; EventHandler <EventArg <GeoEntityType, List <MapObject> > > vSortingChangedAction = (sender, args) => { Debug.LogWarning("Sorting changed" + args.arg2[2].LinkedGeoEntity.gameObject.name); }; if (VirtualSpaceMap != null) { VirtualSpaceMap.Init(vClosestChangedAction, vSortingChangedAction); } if (GuideMap != null) { GuideMap.Init(); } }
//Sets closest mapObject of given type. MapObject to compare distance and geoEntityType are given as parameters. Launches event if closest mapobject is changed //Not sure if this is much faster than sorting all with SortMapObjects() public void setClosest(MapObject mapObject, GeoEntityType geoEntityType) { List <MapObject> objs; //mapObjects.TryGetValue(geoEntityType, out objs); currentFloorMapObjects.TryGetValue(geoEntityType, out objs); if (objs != null) { MapObject closest = null; float distance = float.MaxValue; foreach (MapObject obj in objs) { if (obj != mapObject) { float dist = (obj.MapPos - mapObject.MapPos).sqrMagnitude; if (dist < distance) { closest = obj; distance = dist; } } } if (closest != null) { if (!closestObjects.ContainsKey(geoEntityType) || closestObjects[geoEntityType] != closest) { closestObjects[geoEntityType] = closest; closestMapObjectChanged(this, new EventArg <GeoEntityType, MapObject>(geoEntityType, closest)); } } } }