/// <summary> /// used to move an entity from one subworld to another /// </summary> /// <param name="entity"></param> /// <param name="newSubworldID"></param> /// <param name="newPosition"></param> public void MoveEntity(Entity entity, int newSubworldID, Vec2i newPosition) { Vec2i oldChunk = World.GetChunkPosition(entity.Position); bool succesful = false; if (EntitiesByLocation.TryGetValue(entity.CurrentSubworldID, out Dictionary <Vec2i, List <int> > subworldEnts)) { if (subworldEnts.TryGetValue(oldChunk, out List <int> entIds)) { //if this entity is here, we remove them if (entIds.Contains(entity.ID)) { entIds.Remove(entity.ID); succesful = true; } } } if (!succesful) { Debug.LogError("Could not find old location of entity: " + entity); throw new System.Exception("loool"); } int id = entity.ID; //get world ID and chunk position Vec2i cPos = World.GetChunkPosition(newPosition); if (!EntitiesByLocation.ContainsKey(newSubworldID)) { EntitiesByLocation.Add(newSubworldID, new Dictionary <Vec2i, List <int> >()); } if (!EntitiesByLocation[newSubworldID].ContainsKey(cPos)) { EntitiesByLocation[newSubworldID].Add(cPos, new List <int>()); } EntitiesByLocation[newSubworldID][cPos].Add(id); Subworld sw = World.Instance.GetSubworld(newSubworldID); if (sw != null) { sw.AddEntity(entity); } entity.SetSubworld(newSubworldID); entity.SetPosition(newPosition.AsVector3()); entity.SetLastChunk(cPos); int loadedSubworldID = Subworld == null ? -1 : Subworld.SubworldID; //If the current entity is loaded... if (IsEntityLoaded(entity.ID)) { Debug.Log("1"); //Check if the new position is loaded, if not then we unload the entity if (!LoadedChunks.Contains(cPos) || (entity.CurrentSubworldID != loadedSubworldID)) { Debug.Log("2"); //If the new position isn't loaded, then we must unload the entity //If correct chunk position is loaded, but wrong world, we also unload UnloadEntity(entity.GetLoadedEntity(), false); //If the entity is now in a subworld after being unloaded, //we must check if they are close enough to the player to have a slow update. if (entity.CurrentSubworldID != -1) { if (sw != null) { //We get the entrance, and check if the relvent chunk is loaded Vec2i entranceChunk = World.GetChunkPosition(sw.ExternalEntrancePos); //If it is, then we make sure we continue to run slow ticks on the unloaded entity if (LoadedChunks.Contains(entranceChunk)) { UnloadedUpdatableEntities.Add(entity.ID); } } } } } else { if (LoadedChunks.Contains(cPos) && (entity.CurrentSubworldID == loadedSubworldID)) { LoadEntity(entity); } } }