public LinkedList<Point> Route(Point from, Point to) { var openSet = new List<VMWalkableRect>(); var startRect = new VMWalkableRect(from.X, from.Y, from.X, from.Y); ConstructFirstFree(startRect); startRect.Start = true; startRect.ParentSource = from; startRect.OriginalG = 0; openSet.Add(startRect); while (openSet.Count > 0) { var current = openSet[0]; openSet.RemoveAt(0); if (current.Contains(to)) { var result = new LinkedList<Point>(); result.AddFirst(to); if (!to.Equals(current.ParentSource)) result.AddFirst(current.ParentSource); Point last = current.ParentSource; while (current != startRect) { current = current.Parent; if (!last.Equals(current.ParentSource)) result.AddFirst(current.ParentSource); last = current.ParentSource; } return result; } current.State = 2; //this rectangle is now closed //generate all adj ExtendFrom(current, 0); ExtendFrom(current, 1); ExtendFrom(current, 2); ExtendFrom(current, 3); foreach (VMWalkableRect r in current.Adj) { if (r.State == 2) continue; //closed bool newcomer = (r.State == 0); var parentPt = RectIntersect(r, current, current.ParentSource); var originalG = current.OriginalG + PointDist(current.ParentSource, parentPt); var closest = r.Closest(to.X, to.Y); var newGScore = originalG + PointDist(parentPt, closest); if (newcomer || newGScore < r.GScore) { r.State = 1; r.ParentSource = parentPt; r.Parent = current; r.OriginalG = originalG; r.GScore = newGScore; r.FScore = newGScore + PointDist(closest, to); if (newcomer) { OpenSetSortedInsert(openSet, r); } else { openSet.Remove(r); OpenSetSortedInsert(openSet, r); } } } } return null; //failed }
/// <summary>Function to update the view.</summary> /// <param name="gameTime">Provides a snapshot of timing values</param> public override void Update(GameTime gameTime) { Point panDirection = new Point(0,0); Vector2 panDisplacement = Vector2.Zero; timeSinceLastDraw += gameTime.ElapsedGameTime.Milliseconds; if (timeSinceLastDraw > msBetweenFrames) { timeSinceLastDraw = 0; // Poll input MouseState mouseState = Mouse.GetState(); int mouseX = mouseState.X; int mouseY = mouseState.Y; // check for clicking if (prevMouseState.LeftButton == ButtonState.Pressed && mouseState.LeftButton == ButtonState.Released) { Vector3 nearSource = new Vector3((float)mouseX, (float)mouseY, 0f); Vector3 farSource = new Vector3((float)mouseX, (float)mouseY, 1f); } // check for panning if (panningAllowed) { // determine direction if (mouseState.X <= panBuffer) { panDirection.X = 1; } if (mouseState.X >= (clientBounds.Width - panBuffer)) { panDirection.X = -1; } if (mouseState.Y <= panBuffer) { panDirection.Y = 1; } if (mouseState.Y >= (clientBounds.Height - panBuffer)) { panDirection.Y = -1; } // execute pan if (!panDirection.Equals(Point.Zero)) tacticalView.Pan(panDirection); } // update children //gameWindow.Update(gameTime); // record old input state prevMouseState = mouseState; } }
private bool HasMouseMoved(Point position) { return !position.Equals(this.oldPosition); }
public void Update(TimeSpan elapsedTime, bool deviceActive) { #if WINDOWS_PHONE TouchCollection touches = TouchPanel.GetState(); if (touches.Count > 0) { Point mouseLocation = new Point((int)touches[0].Position.X, (int)touches[0].Position.Y); // We only care about the primary touch for MouseInput implementation switch (touches[0].State) { case TouchLocationState.Pressed: if(MousePressEvent != null) MousePressEvent(LeftButton, mouseLocation); mousePressed = true; // Handle drag start startDraggingPos = mouseLocation; prevDraggingPos = mouseLocation; dragButton = LeftButton; break; case TouchLocationState.Released: // RELEASE if (MouseReleaseEvent != null) MouseReleaseEvent(dragButton, mouseLocation); // CLICK if (MouseClickEvent != null) MouseClickEvent(dragButton, mouseLocation); mousePressed = false; break; case TouchLocationState.Moved: if (!prevDraggingPos.Equals(mouseLocation)) { if (MouseDragEvent != null) MouseDragEvent(dragButton, startDraggingPos, mouseLocation); if(MouseMoveEvent != null) MouseMoveEvent(mouseLocation); prevDraggingPos = mouseLocation; } break; } } #elif WINDOWS if (onlyTrackWhenFocused && !deviceActive) return; // Get the current state of the mouse mouseState = Mouse.GetState(); // If only want to track inside of window, then ignore any mouse events // triggered outside of the window if (onlyTrackInsideWindow && (mouseState.X < 0 || mouseState.X >= State.Width || mouseState.Y < 0 || mouseState.Y >= State.Height)) { return; } // HANDLE MOUSE PRESS if (!mousePressed) { if (MouseLeftButtonPressed || MouseRightButtonPressed || MouseMiddleButtonPressed) { int button = LeftButton; if (MouseRightButtonPressed) button = RightButton; else if (MouseMiddleButtonPressed) button = MiddleButton; if(MousePressEvent != null) MousePressEvent(button, MouseLocation); mousePressed = true; // Handle drag start startDraggingPos = MouseLocation; dragButton = button; } } else { // HANDLE MOUSE RELEASE & CLICK if ((dragButton == RightButton && MouseRightButtonReleased) || (dragButton == MiddleButton && MouseMiddleButtonPressed) || (dragButton == LeftButton && MouseLeftButtonReleased)) { // RELEASE if(MouseReleaseEvent != null) MouseReleaseEvent(dragButton, MouseLocation); // CLICK if(MouseClickEvent != null) MouseClickEvent(dragButton, MouseLocation); mousePressed = false; } // HANDLE MOUSE DRAG if ((MouseLeftButtonPressed || MouseRightButtonPressed || MouseMiddleButtonPressed) && !startDraggingPos.Equals(MouseLocation) && !prevDraggingPos.Equals(MouseLocation)) { int button = LeftButton; if (MouseRightButtonPressed) button = RightButton; else if (MouseMiddleButtonPressed) button = MiddleButton; if (dragButton == button) { if(MouseDragEvent != null) MouseDragEvent(button, startDraggingPos, MouseLocation); prevDraggingPos = MouseLocation; } } } // HANDLE MOUSE MOVE if ((lastMouseX != mouseState.X) || (lastMouseY != mouseState.Y)) { if(MouseMoveEvent != null) MouseMoveEvent(MouseLocation); lastMouseX = mouseState.X; lastMouseY = mouseState.Y; } // HANDLE MOUSE WHEEL MOVE mouseWheelDelta = mouseState.ScrollWheelValue - mouseWheelValue; if (mouseWheelDelta != 0) { mouseWheelValue = mouseState.ScrollWheelValue; if(MouseWheelMoveEvent != null) MouseWheelMoveEvent(mouseWheelDelta, mouseWheelValue); } if (MouseRightButtonReleased && MouseLeftButtonReleased && MouseMiddleButtonReleased) mousePressed = false; #endif }
private bool tilesConnected(Point start, Point goal) { if (start.Equals(goal)) { return true; } Queue<Point> queue = new Queue<Point>(); bool[,] connected = new bool[tileGrid.Height, tileGrid.Width]; connected[start.Y - tileGrid.MinY, start.X - tileGrid.MinX] = true; queue.Enqueue(start); while (queue.Count != 0) { Point p = queue.Dequeue(); if (tilesConnectedHelper(queue, connected, new Point(p.X, p.Y + 1), goal) || // east tilesConnectedHelper(queue, connected, new Point(p.X + 1, p.Y), goal) || // north tilesConnectedHelper(queue, connected, new Point(p.X, p.Y - 1), goal) || // west tilesConnectedHelper(queue, connected, new Point(p.X - 1, p.Y), goal)) // south { return true; } } return false; }
private void Step() { if (state == GameState.Dying) { snakeBlocks.RemoveAt(snakeBlocks.Count - 1); if (snakeBlocks.Count == 0) { Vector2 expVec = ScreenVec(nextBlock); expVec.X += (float)blockSize / 2.0f; expVec.Y += (float)blockSize / 2.0f; boom.Play(0.5f, RandomPitch(0.5f), 0.0f); explode.Trigger(ref expVec); state = GameState.Splash; InitSnake(); DoHighScore(); foreach (HighScore s in highScores) Console.WriteLine(s); } return; } KeyboardState keyState = Keyboard.GetState(); keysDown.Clear(); keysDown.AddRange(keyState.GetPressedKeys()); snakeDirection = nextSnakeDirection; Point head = snakeBlocks[0]; Point next = new Point(head.X + (snakeDirection == Direction.Right ? 1 : 0) - (snakeDirection == Direction.Left ? 1 : 0), head.Y + (snakeDirection == Direction.Down ? 1 : 0) - (snakeDirection == Direction.Up ? 1 : 0)); // check for constrict if (!constrictBlocks.Except(snakeBlocks).Any()) { score += 5; stepTime -= 3; Vector2 screenExp = ScreenVec(nextBlock); Vector2 expVec = new Vector2(screenExp.X + (blockSize / 2.0f), screenExp.Y + (blockSize / 2.0f)); explode.Trigger(ref expVec); boom.Play(0.5f, RandomPitch(0.5f), 0.0f); GenerateBlock(); } snakeBlocks.Insert(0, next); if (next.Equals(nextBlock)) { crunch.Play(1.0f, RandomPitch(0.3f), 0.0f); score++; stepTime -= 3; GenerateBlock(); } else // Pop the last block off { snakeBlocks.RemoveAt(snakeBlocks.Count - 1); } }
private ArrowDirection direction(Point p1, Point p2, bool finished) { if (!finished) { Point p3 = new Point(p2.X + p1.X, p2.Y + p1.Y); if (p1.Equals(new Point(1, 0)) && p2.Equals(new Point(1, 0)) || p1.Equals(new Point(-1, 0)) && p2.Equals(new Point(-1, 0))) { return ArrowDirection.Horizontal; } if (p1.Equals(new Point(0, 1)) && p2.Equals(new Point(0, 1)) || p1.Equals(new Point(0, -1)) && p2.Equals(new Point(0, -1))) { return ArrowDirection.Vertical; } if (p1.Equals(new Point(0, -1)) && p2.Equals(new Point(1, 0)) || p1.Equals(new Point(-1, 0)) && p2.Equals(new Point(0, 1))) { return ArrowDirection.BottomRight; } if (p1.Equals(new Point(0, -1)) && p2.Equals(new Point(-1, 0)) || p1.Equals(new Point(1, 0)) && p2.Equals(new Point(0, 1))) { return ArrowDirection.BottomLeft; } if (p1.Equals(new Point(-1, 0)) && p2.Equals(new Point(0, -1)) || p1.Equals(new Point(0, 1)) && p2.Equals(new Point(1, 0))) { return ArrowDirection.TopRight; } if (p1.Equals(new Point(1, 0)) && p2.Equals(new Point(0, -1)) || p1.Equals(new Point(0, 1)) && p2.Equals(new Point(-1, 0))) { return ArrowDirection.TopLeft; } } else { if (p1.Equals(new Point(1, 0))) { return ArrowDirection.Right; } if (p1.Equals(new Point(-1, 0))) { return ArrowDirection.Left; } if (p1.Equals(new Point(0, 1))) { return ArrowDirection.Up; } if (p1.Equals(new Point(0, -1))) { return ArrowDirection.Down; } } return ArrowDirection.None; }
private List<Point> getAdjacentTiles(Point start, ushort room, Point target) { // check all 4 sides to see if the tiles on them: // 1. are not blocked by a wall // 2. do not contain any collidable objects // TODO: optimise by remembering what certain tiles return for their collidable status, as this will not change. var adj = new List<Point>(); Point test; test = new Point(start.X, start.Y + 1); if (!TileSolid(test.X, test.Y, room) || test.Equals(target)) adj.Add(test); //todo, check for wall between test = new Point(start.X + 1, start.Y); if (!TileSolid(test.X, test.Y, room) || test.Equals(target)) adj.Add(test); //todo, check for wall between test = new Point(start.X, start.Y - 1); if (!TileSolid(test.X, test.Y, room) || test.Equals(target)) adj.Add(test); //todo, check for wall between test = new Point(start.X - 1, start.Y); if (!TileSolid(test.X, test.Y, room) || test.Equals(target)) adj.Add(test); //todo, check for wall between return adj; }
/// <summary> /// Gets the pixels between points. /// </summary> /// <param name="p1">Point one</param> /// <param name="p2">Point two</param> /// <param name="spacing">The pixel spacing between the points (default should be 1). Lower than 1 will use 1.</param> /// <returns></returns> public Point[] PixelsBetweenPoints(Point p1, Point p2, int spacing) { if (spacing < 1) spacing = 1; int xDiff = p2.X - p1.X; int yDiff = p2.Y - p1.Y; double maxWidth = Math.Max(Math.Abs(xDiff) , Math.Abs(yDiff)); double xDelta = (xDiff / maxWidth) * spacing; // Console.Out.WriteLine("xDelta: " + xDelta); double yDelta = (yDiff / maxWidth) * spacing; // Console.Out.WriteLine("yDelta: " + yDelta); LinkedList<Point> pointList = new LinkedList<Point>(); Point currentPoint = p1; Point previousPoint = new Point(-100, -100); double currentX = p1.X, currentY = p1.Y; for( int i = 0; i < maxWidth / spacing; i++ ) { currentX += xDelta; currentY += yDelta; currentPoint.X = (int)currentX; currentPoint.Y = (int)currentY; Point newPoint = new Point( currentPoint.X, currentPoint.Y ); if ( previousPoint != null && !previousPoint.Equals(newPoint) ) { // Console.Out.WriteLine(i + ". Adding point to list: " + newPoint); pointList.AddLast(newPoint); } previousPoint = newPoint; } return pointList.ToArray<Point>(); }
public bool MoveEntity(Entity entity, Point start, Point end) { if (start == null || end == null || !InBounds(end) || start.Equals(end)) { return false; } // Retrieve the list of entities at the start position List<Entity> entitiesAtStart; if (entityTable.ContainsKey(start)) { entitiesAtStart = entityTable[start]; } else { // Add a new list for empty positions entitiesAtStart = new List<Entity>(); entityTable.Add(start, entitiesAtStart); } // Remove the entity from the start bucket if (entitiesAtStart.Remove(entity)) { entity.Position = end; // Retrieve the list of entities at the end position List<Entity> entitiesAtEnd; if (entityTable.ContainsKey(end)) { entitiesAtEnd = entityTable[end]; } else { // Adds a new list for empty positions entitiesAtEnd = new List<Entity>(); entityTable.Add(end, entitiesAtEnd); } // Add the entity to the end bucket entitiesAtEnd.Add(entity); // Move successful return true; } // Move failed return false; }
private void GenerateMatrix(Node Prev, Point Aqui, Path Path) { if (CheckedBoard == null) { CheckedBoard = new bool[Board.Width, Board.Height]; } CheckedBoard[Aqui.X, Aqui.Y] = true; if (Path == null) { Path = new Path(this, null); } if (All.ContainsKey(Aqui) && (!Prev.Location.Equals(Aqui) || Path.Points.Count > 1)) { Node node; All.TryGetValue(Aqui, out node); Path.AddEnd(node); Prev.Paths.Add(Path); node.Paths.Add(Path.Reverse()); //foreach (Point Point in Path.Points) //{ // UI.RegisterSprite(new StaticSprite(new Element { X = Point.X, Y = Point.Y }, "fruit/banana", 20)); //} return; } if (SurroundingFreeSpaces(Aqui) > 2 && !Path.Start.Location.Equals(Aqui) && !All.ContainsKey(Aqui)) { //UI.RegisterSprite(new StaticSprite(new Element { X = Aqui.X, Y = Aqui.Y }, "fruit/orange", 20)); Node End = new Node(Aqui); All.Add(Aqui, End); //Path.AddEnd(End); //Prev.Paths.Add(Path); //End.Paths.Add(Path.Reverse()); //foreach (Point Point in Path.Points) //{ // UI.RegisterSprite(new StaticSprite(new Element { X = Point.X, Y = Point.Y }, "fruit/cherry", 20)); //} End.GenerateMatrix(End, End.Location, null); return; } for (double i = 0; i < 2 * MathHelper.Pi; i+= MathHelper.Pi/2) { Point Next = new Point(Aqui.X + (int)Math.Round(Math.Sin(i)), Aqui.Y + (int)Math.Round(Math.Cos(i))); if(IsInBounds(Next) && !Board.IsType(Enum.ElementTypes.Wall, Next) && !CheckedBoard[Next.X, Next.Y]){ GenerateMatrix(Prev, Next, Aqui.Equals(Location)?Path.Diverge():Path.Diverge(Aqui)); } } }
public override void Update(GameTime gameTime) { base.Update(gameTime); var currentState = Mouse.GetState(); LeftButton.Update(currentState.LeftButton); RightButton.Update(currentState.RightButton); MiddleButton.Update(currentState.MiddleButton); var newPoint = new Point(currentState.X, currentState.Y); if (!newPoint.Equals(lastPosition)) { MouseMove(newPoint); lastPosition = newPoint; } }