public void StartMoveGroup(PlatformMoveType type, Int2 playerIdx) { if (gridSystem.ExistMovingGroup) { return; } int units = ComputeMoveUnits(type, playerIdx); //HashSet<PlatformGroup> colGroups = ComputeNeighborGroups(); if (units == 0) { HashSet <PlatformGroup> colGroups = ComputeNeighborGroups(); colGroups.Add(this); PlatformGroup.Combine(colGroups, playerIdx); return; } gridSystem.ExistMovingGroup = true; StartCoroutine(MoveGroupCoroutine(type, units, playerIdx)); }
//private private IEnumerator MoveGroupCoroutine(PlatformMoveType type, int units, Int2 playerIdx) { foreach (Platform pf in container) { pf.StartMove(type, units); } bool waitAll = true; while (waitAll) { waitAll = false; foreach (Platform pf in container) { if (pf.IsMoving) { waitAll = true; } } yield return(null); } //maintain gridSystem foreach (Platform pf in container) { gridSystem[pf.index] = null; } switch (type) { case PlatformMoveType.AxisX: { foreach (Platform pf in container) { pf.index._x += units; gridSystem[pf.index] = pf; } break; } case PlatformMoveType.AxisZ: { foreach (Platform pf in container) { pf.index._z += units; gridSystem[pf.index] = pf; } break; } default: throw new NotImplementedException(); } //trigger combination HashSet <PlatformGroup> colGroups = ComputeNeighborGroups(); colGroups.Add(this); //should update player's platform group here PlatformGroup.Combine(colGroups, playerIdx); //consider using event instead //LevelController.platformMoved = true; if (OnGroupMoved != null) { OnGroupMoved(); } gridSystem.ExistMovingGroup = false; }
public static HashSet <PlatformGroup> Restructure(HashSet <PlatformGroup> groups, Int2 playerIdx) { //NO EMPTY GROUP ALLOWED bool[,] seen = new bool[gridSystem.gridSizeX, gridSystem.gridSizeZ]; HashSet <HashSet <PlatformGroup> > components = new HashSet <HashSet <PlatformGroup> >(); foreach (PlatformGroup group in groups) { var iter = group.container.GetEnumerator(); iter.MoveNext(); var pf = iter.Current; if (seen[pf.index._x, pf.index._z]) { continue; } HashSet <PlatformGroup> component = new HashSet <PlatformGroup>(); //flood fill Queue <Int2> indices = new Queue <Int2>(); indices.Enqueue(pf.index); while (indices.Count > 0) { Int2 curr = indices.Dequeue(); seen[curr._x, curr._z] = true; if (gridSystem[curr] != null) { component.Add(gridSystem[curr].group); if (curr._x > 0 && !seen[curr._x - 1, curr._z]) { indices.Enqueue(new Int2(curr._x - 1, curr._z)); } if (curr._x < gridSystem.gridSizeX - 1 && !seen[curr._x + 1, curr._z]) { indices.Enqueue(new Int2(curr._x + 1, curr._z)); } if (curr._z > 0 && !seen[curr._x, curr._z - 1]) { indices.Enqueue(new Int2(curr._x, curr._z - 1)); } if (curr._z < gridSystem.gridSizeZ - 1 && !seen[curr._x, curr._z + 1]) { indices.Enqueue(new Int2(curr._x, curr._z + 1)); } } } components.Add(component); } //components should have no overlapping HashSet <PlatformGroup> output = new HashSet <PlatformGroup>(); foreach (var component in components) { output.Add(PlatformGroup.Combine(component, playerIdx)); } return(output); }