/// <summary> /// Destroys the object at the given point /// TODO - make sure multi-tile objects are destroyed correctly. /// TODO - make sure objects with listeners (inventory that destroy on empty) have lsiteners removed correctly /// </summary> /// <param name="worldPos"></param> public void DestroyWorldObject(Vec2i worldPos) { Vec2i chunkPos = World.GetChunkPosition(worldPos); ChunkData c = CRManager.GetChunk(chunkPos); c.SetObject(worldPos.x, worldPos.z, null); //c.Objects[worldPos.x % World.ChunkSize, worldPos.z % World.ChunkSize] = null; LoadedChunk loaded = CRManager.GetLoadedChunk(chunkPos); Debug.Log("Destroy: " + worldPos); if (loaded != null) { if (loaded.LoadedWorldObjects[worldPos.x % World.ChunkSize, worldPos.z % World.ChunkSize] != null) { Destroy(loaded.LoadedWorldObjects[worldPos.x % World.ChunkSize, worldPos.z % World.ChunkSize].gameObject); loaded.LoadedWorldObjects[worldPos.x % World.ChunkSize, worldPos.z % World.ChunkSize] = null; } } }
/// <summary> /// Adds the given world object to the given world position. /// If the object is placed in a loaded chunk, then we load the object into the game /// </summary> /// <param name="worldPos"></param> /// <param name="worldObject"></param> public void AddNewObject(WorldObjectData worldObject) { //Get relevent chunk of object placement. Vec2i chunkPos = World.GetChunkPosition(worldObject.WorldPosition); ChunkData c = CRManager.GetChunk(chunkPos); Vec2i localPos = new Vec2i(worldObject.WorldPosition.x % World.ChunkSize, worldObject.WorldPosition.z % World.ChunkSize); c.SetObject(localPos.x, localPos.z, worldObject); //Check if the object has been added to a loaded chunk LoadedChunk loaded = CRManager.GetLoadedChunk(chunkPos); if (loaded != null) { WorldObject newObject = worldObject.CreateWorldObject(loaded.transform); loaded.LoadedWorldObjects[localPos.x, localPos.z] = newObject; } }