public ChemBlock(ChemicalElement element, Texture2D texture, Vector2 pos, Vector2 size, Color color) : base(texture, pos, size, color, PlatformObjectType.Pushable) { this.element = element; chemGrid = new ChemGrid(this); }
public void UpdateConnectivity() { Dictionary <Point, ChemBlock> blockMapping = new Dictionary <Point, ChemBlock>(); HashSet <Point> inactiveBlocks = new HashSet <Point>(); foreach (KeyValuePair <ChemBlock, Point> kv in blocks) { blockMapping.Add(kv.Value, kv.Key); inactiveBlocks.Add(kv.Value); } //now flood fill regions until everything is filled while (inactiveBlocks.Count > 0) { Point originPoint = inactiveBlocks.First(); inactiveBlocks.Remove(originPoint); ChemBlock originBlock = blockMapping[originPoint]; ChemGrid newGrid = new ChemGrid(originBlock); originBlock.chemGrid = newGrid; originBlock.UnbondFromGroup(); List <Point> activeBlocks = new List <Point>() { originPoint }; List <Point> nextActiveBlocks = new List <Point>(); List <Point> finalPositions = new List <Point>(); while (activeBlocks.Count > 0) { foreach (Point p in activeBlocks) { Func <Point, bool> addBlock = pos => { inactiveBlocks.Remove(pos); nextActiveBlocks.Add(pos); ChemBlock block = blockMapping[pos]; block.UnbondFromGroup(); block.chemGrid = null; newGrid.AddNail(blockMapping[p], block); return(false); }; Point up = new Point(p.X, p.Y - 1); Point down = new Point(p.X, p.Y + 1); Point left = new Point(p.X - 1, p.Y); Point right = new Point(p.X + 1, p.Y); if (verticalBonds.Contains(up) && inactiveBlocks.Contains(up)) { addBlock(up); } if (verticalBonds.Contains(p) && inactiveBlocks.Contains(down)) { addBlock(down); } if (horizontalBonds.Contains(left) && inactiveBlocks.Contains(left)) { addBlock(left); } if (horizontalBonds.Contains(p) && inactiveBlocks.Contains(right)) { addBlock(right); } } List <Point> temp = activeBlocks; temp.Clear(); activeBlocks = nextActiveBlocks; nextActiveBlocks = temp; } } }