private static void Fall(DijkstraHelper <int> dijkstra, int x, int y, int dist, int fallDist, int dx1, int dx2) { while (y + 1 < ysize && map[y + 1, x] == '.') { y++; fallDist++; } if (fallDist <= maxFall) { if (fallDist > 1) { dx1 = dx2 = x; } dijkstra.Add(Encode(x, y, dx1, dx2), dist); } }
public override void Update(GameTime gameTime) { mouseInputState = CONTENT_MANAGER.inputState.mouseState; lastMouseInputState = CONTENT_MANAGER.lastInputState.mouseState; keyboardInputState = CONTENT_MANAGER.inputState.keyboardState; lastKeyboardInputState = CONTENT_MANAGER.lastInputState.keyboardState; selectedMapCell = Utility.HelperFunction.TranslateMousePosToMapCellPos(mouseInputState.Position, camera, map.Width, map.Height); if (HelperFunction.IsKeyPress(Keys.OemTilde)) { console.IsVisible = !console.IsVisible; } #region change unit and animation if (console.IsVisible) //suck all input in to the input box { console.Update(CONTENT_MANAGER.inputState, CONTENT_MANAGER.lastInputState); } else //accept input { //cylce through unit if (HelperFunction.IsKeyPress(Keys.Left)) { if (currentUnit == UnitType.Soldier) { currentUnit = UnitType.Battleship; } else { currentUnit = currentUnit.Previous(); } } if (HelperFunction.IsKeyPress(Keys.Right)) { if (currentUnit == UnitType.Battleship) { currentUnit = UnitType.Soldier; } else { currentUnit = currentUnit.Next(); } } //cycle through animation if (HelperFunction.IsKeyPress(Keys.Up)) { if (currentAnimation == AnimationName.idle) { currentAnimation = AnimationName.done; } else { currentAnimation = currentAnimation.Previous(); } } if (HelperFunction.IsKeyPress(Keys.Down)) { if (currentAnimation == AnimationName.done) { currentAnimation = AnimationName.idle; } else { currentAnimation = currentAnimation.Next(); } } //cycle through color if (HelperFunction.IsKeyPress(Keys.E)) { if (currentColor == GameData.Owner.Yellow) { currentColor = GameData.Owner.Red; } else { currentColor = currentColor.Next(); } } if (HelperFunction.IsKeyPress(Keys.Q)) { if (currentColor == GameData.Owner.Red) { currentColor = GameData.Owner.Yellow; } else { currentColor = currentColor.Previous(); } } } Unit unit = map[position].unit; UnitType nextUnit = unit.UnitType; GameData.Owner nextOwner = unit.Owner; AnimationName nextAnimation = unit.Animation.CurntAnimationName.ToEnum <AnimationName>(); bool isChanged = false; if (nextUnit != currentUnit) { isChanged = true; } if (nextOwner != currentColor) { isChanged = true; } if (nextAnimation != currentAnimation) { isChanged = true; } if (isChanged) { map[position].unit = UnitCreationHelper.Create(currentUnit, currentColor, startingAnimation: currentAnimation); } map[position].unit.Animation.Update(gameTime); #endregion if (mouseInputState.LeftButton == ButtonState.Released && lastMouseInputState.LeftButton == ButtonState.Pressed) { SelectUnit(); } //if (isMovingUnitAnimPlaying) //{ // UpdateMovingUnit(gameTime); //} //calculate movepath if (isMovePathCalculated) { if (movementRange.Contains(selectedMapCell) && selectedMapCell != lastSelectedMapCell) { //update movement path movementPath = DijkstraHelper.FindPath(dijkstarGraph, selectedMapCell); dirarrowRenderer.UpdatePath(movementPath); lastSelectedMapCell = selectedMapCell; } } base.Update(gameTime); }
private void DisplayMovementRange(Unit unit, Point position) { dijkstarGraph = DijkstraHelper.CalculateGraph(map, unit, position); movementRange = DijkstraHelper.FindRange(dijkstarGraph); }
private static void Solve(TextReader sr, TextWriter sw) { string[] strings = sr.ReadLine().Split(' '); ysize = int.Parse(strings[0]); xsize = int.Parse(strings[1]); maxFall = int.Parse(strings[2]); map = new char[ysize, xsize]; for (int i = 0; i < ysize; i++) { string line = sr.ReadLine(); for (int j = 0; j < xsize; j++) { map[i, j] = line[j]; } } var dijkstra = new DijkstraHelper <int>(xsize * ysize * 120); dijkstra.Add(Encode(0, 0, 0, 0), 0); int ans = -1; while (true) { int node = dijkstra.GetNext(); if (node < 0) { break; } int x, y, dx1, dx2; Decode(node, out x, out y, out dx1, out dx2); int curDist = dijkstra.GetDistance(node); // Console.WriteLine("At " + x + "," + y + " [" + dx1 + "," + dx2 + "] after " + curDist + " digs."); if (y == ysize - 1) { ans = curDist; break; } // Find move interval int left = x, right = x; while (left - 1 >= 0 && map[y + 1, left - 1] == '#' && (map[y, left - 1] == '.' || (left - 1 >= dx1 && left - 1 <= dx2))) { left--; } while (right + 1 < xsize && map[y + 1, right + 1] == '#' && (map[y, right + 1] == '.' || (right + 1 >= dx1 && right + 1 <= dx2))) { right++; } if (left - 1 >= 0 && (map[y, left - 1] == '.' || (left - 1 >= dx1 && left - 1 <= dx2)) && map[y + 1, left - 1] == '.') { Fall(dijkstra, left - 1, y, curDist, 0, left - 1, left - 1); } if (right + 1 < xsize && (map[y, right + 1] == '.' || (right + 1 >= dx1 && right + 1 <= dx2)) && map[y + 1, right + 1] == '.') { Fall(dijkstra, right + 1, y, curDist, 0, right + 1, right + 1); } for (int nx = left; nx <= right; nx++) { // Dig the interval [nx,tx] and fall down into tx for (int tx = nx; tx < right; tx++) { Fall(dijkstra, tx, y + 1, curDist + (tx - nx + 1), 1, nx, tx); } // Dig the interval [tx,nx] and fall down into tx for (int tx = left + 1; tx <= nx; tx++) { Fall(dijkstra, tx, y + 1, curDist + (nx - tx + 1), 1, tx, nx); } } } if (ans < 0) { sw.WriteLine("No"); } else { sw.WriteLine("Yes " + ans); } }