/// <summary> /// Merges the tile in the existing liquidbodies. /// If there is no existing neighbors, then make a new liquidbody and add it to the list. /// Note that this merge is not construction based but rather finalized. /// </summary> /// <param name="pos"></param> /// <returns>True if successful.</returns> private bool MergeTile(Vector3Int pos, float[] contents) { List <LiquidBody> neighborLiquidbodies = new List <LiquidBody>(); foreach (LiquidBody l in liquidBodies) { // return false if contains the position already if (l.ContainsTile(pos)) { Debug.LogError("Tile already exists in liquidbody!"); return(false); } else { if (l.NeighborsTile(pos)) { neighborLiquidbodies.Add(l); } } } switch (neighborLiquidbodies.Count) { case 0: // create a new liquidbody LiquidBody newLiquidbody = new LiquidBody(pos, contents, GenerateBodyID()); // append it to the list liquidBodies.Add(newLiquidbody); break; case 1: // append to the existing liquidbody neighborLiquidbodies[0].AddTile(pos, contents); break; default: // merge the liquidbodies LiquidBody mergedLiquidbody = new LiquidBody(neighborLiquidbodies, GenerateBodyID()); mergedLiquidbody.AddTile(pos, contents); // remove the original liquidbodies for (int i = 0; i < neighborLiquidbodies.Count; ++i) { liquidBodies.Remove(neighborLiquidbodies[i]); } // add the newly created body liquidBodies.Add(mergedLiquidbody); break; } Debug.Log("Liquidbody merge successful."); return(true); }
public bool RemoveLiquidContentsFromLiquidbodyAt(Vector3Int pos) { // check if the liquid actually exists if (!CheckLiquidTileAlreadyExistsAt(pos)) { Debug.LogError("Liquid does not exist at " + pos); return(false); } // set up liquidbodies just in case // this works because there should be no tile in two different liquidbodies List <HashSet <Vector3Int> > dividedBodiesTiles = new List <HashSet <Vector3Int> >(); bool hasDivisions = false; LiquidBody originalLiquidbody = new LiquidBody(new List <LiquidBody>()); // find the liquidbody with the tile and remove the tile foreach (LiquidBody l in liquidBodies) { if (l.ContainsTile(pos)) { originalLiquidbody = l; hasDivisions = l.RemoveTile(pos, out dividedBodiesTiles); } } // check if there are any tiles left, if so delete if (originalLiquidbody.TileCount == 0) { liquidBodies.Remove(originalLiquidbody); return(true); } // if there are divisions when removing the tile if (hasDivisions) { // create and add each new liquidbody resulting from the divide for (int i = 0; i < dividedBodiesTiles.Count; ++i) { LiquidBody newLiquidBody = new LiquidBody(dividedBodiesTiles[i], originalLiquidbody.contents, GenerateBodyID()); liquidBodies.Add(newLiquidBody); } liquidBodies.Remove(originalLiquidbody); } return(true); }
public void AddLiquidBody(LiquidBody body) { liquidBodies.Add(body); }