public void register(StateTrackedObject obj) { if (obj is WaterBehavior) { WaterBehavior water = obj.GetComponent <WaterBehavior>(); Assert.IsNotNull(water); this.water.Add(obj); } else if (obj is MovPlatformBehavior) { MovPlatformBehavior platform = obj.GetComponent <MovPlatformBehavior>(); Assert.IsNotNull(platform); this.movPlatforms.Add(obj); } else if (obj is StillPlatformBehavior) { StillPlatformBehavior platform = obj.GetComponent <StillPlatformBehavior>(); Assert.IsNotNull(platform); this.stillPlatforms.Add(obj); } else { // Should never be reached Assert.IsTrue(false); } updateBounds(obj.transform); }
public StateTrackedObject[,] buildState() { Assert.IsTrue(minpos.x < maxpos.x); Assert.IsTrue(minpos.y < maxpos.y); StateTrackedObject[,] state = new StateTrackedObject[((int)maxpos.x - (int)minpos.x) + 1, ((int)maxpos.y - (int)minpos.y) + 1]; foreach (StateTrackedObject item in water) { StateTrackedObject curr = state[(int)(item.transform.position.x - minpos.x), (int)(item.transform.position.y - minpos.y)]; Assert.IsNull(curr); state[(int)(item.transform.position.x - minpos.x), (int)(item.transform.position.y - minpos.y)] = item; } foreach (StateTrackedObject item in stillPlatforms) { StateTrackedObject curr = state[(int)(item.transform.position.x - minpos.x), (int)(item.transform.position.y - minpos.y)]; Assert.IsNull(curr); state[(int)(item.transform.position.x - minpos.x), (int)(item.transform.position.y - minpos.y)] = item; } foreach (StateTrackedObject item in movPlatforms) { StateTrackedObject curr = state[(int)(item.transform.position.x - minpos.x), (int)(item.transform.position.y - minpos.y)]; Assert.IsTrue(curr == null || curr is WaterBehavior); state[(int)(item.transform.position.x - minpos.x), (int)(item.transform.position.y - minpos.y)] = item; } return(state); }
public StateTrackedObject[,] buildStaticState() { Assert.IsTrue(minpos.x < maxpos.x); Assert.IsTrue(minpos.y < maxpos.y); StateTrackedObject[,] state = new StateTrackedObject[((int)maxpos.x - (int)minpos.x) + 1, ((int)maxpos.y - (int)minpos.y) + 1]; foreach (StateTrackedObject item in water) { StateTrackedObject curr = state[(int)(item.transform.position.x - minpos.x), (int)(item.transform.position.y - minpos.y)]; if (curr != null) { Debug.LogError(String.Format("Overlap! {0} and {1}", item.transform, curr.transform)); } state[(int)(item.transform.position.x - minpos.x), (int)(item.transform.position.y - minpos.y)] = item; } foreach (StateTrackedObject item in stillPlatforms) { StateTrackedObject curr = state[(int)(item.transform.position.x - minpos.x), (int)(item.transform.position.y - minpos.y)]; if (curr != null) { Debug.LogError(String.Format("Overlap! {0} and {1}", item.transform, curr.transform)); } state[(int)(item.transform.position.x - minpos.x), (int)(item.transform.position.y - minpos.y)] = item; } return(state); }
private void flow(Direction direction) { state.incMoves(); StateTrackedObject[,] staticState = this.state.buildStaticState(); StateTrackedObject[,] dynamicState = this.state.buildDynamicState(); int width = staticState.GetLength(0); int height = staticState.GetLength(1); StateTrackedObject[,] nextState = new StateTrackedObject[width, height]; switch (direction) { case Direction.DOWN: for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { checkForMove(staticState, dynamicState, nextState, x, y, 0, -1); } } break; case Direction.UP: for (int y = height - 1; y >= 0; y--) { for (int x = 0; x < width; x++) { checkForMove(staticState, dynamicState, nextState, x, y, 0, 1); } } break; case Direction.LEFT: for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { checkForMove(staticState, dynamicState, nextState, x, y, -1, 0); } } break; case Direction.RIGHT: for (int x = width - 1; x >= 0; x--) { for (int y = 0; y < height; y++) { checkForMove(staticState, dynamicState, nextState, x, y, 1, 0); } } break; default: Assert.IsTrue(false); break; } }
private void checkForMove(StateTrackedObject[,] staticState, StateTrackedObject[,] dynamicState, StateTrackedObject[,] nextState, int x, int y, int dx, int dy) { StateTrackedObject staticCurr = staticState[x, y]; StateTrackedObject dynamicCurr = dynamicState[x, y]; if (staticCurr == null) { return; } else if (dynamicCurr == null) { nextState[x, y] = staticCurr; return; } else if (dynamicCurr is MovPlatformBehavior) { int nextX = x; int nextY = y; while (true) { int testX = nextX + dx; int testY = nextY + dy; if (testX < 0 || testY < 0 || testX >= staticState.GetLength(0) || testY >= staticState.GetLength(1) || nextState[testX, testY] == null || !(nextState[testX, testY] is WaterBehavior)) { nextState[x, y] = staticCurr; nextState[nextX, nextY] = dynamicCurr; BoxCollider2D collider = staticCurr.GetComponent <BoxCollider2D>(); Assert.IsNotNull(collider); collider.enabled = true; collider = staticState[nextX, nextY].GetComponent <BoxCollider2D>(); Assert.IsNotNull(collider); collider.enabled = false; if (x != nextX || y != nextY) { moves.Add(new Move(this.state.gridToGlobal(new Vector2(x, y)), this.state.gridToGlobal(new Vector2(nextX, nextY)), dynamicCurr.transform)); } return; } nextX = testX; nextY = testY; } } else { Assert.IsTrue(false); } }