public void GetBKeyCompleted(object sender, API.ExecuteCompletedEventArgs e) { this.busy.IsBusy = false; if (e.Result.ReturnValue) { GridKey.ItemsSource = e.Result.Obj as List <API.BookKey>; GridKey.Rebind(); } }
private void BuildKeyList(Int32 columns, Int32 rows) { //At least 1 if(columns < 1 || rows < 1) throw new ArgumentOutOfRangeException(); //Can't be greater than number of pixels if(columns > TiledTexture.Texture.Width || rows > TiledTexture.Texture.Height) throw new ArgumentOutOfRangeException(); //Must be evenly divisible if(TiledTexture.Texture.Width % columns != 0 || TiledTexture.Texture.Height % rows != 0) throw new ArgumentOutOfRangeException(); Int32 tileWidth = TiledTexture.Texture.Width / columns; Int32 tileHeight = TiledTexture.Texture.Height / rows; for(Int32 r = 0; r < rows; r++) { for(Int32 c = 0; c < columns; c++) { Int32 left = c * tileWidth; Int32 right = left + tileWidth; Int32 top = r * tileHeight; Int32 bottom = top + tileHeight; Texture2dArea area = new Texture2dArea(left, top, right, bottom, TiledTexture.Texture.Width, TiledTexture.Texture.Height); GridLocation loc = new GridLocation(c, r); GridKey key = new GridKey(this, c, r, area); Keys.Add(loc, key); } } }
private ShortestPathNode? findShortestPath(Vector start, IReadOnlyList<IGoal> wayPoints, double stepSize) { var open = new BinaryHeapOpenSet<GridKey, GridSearchNode>(); var closed = new ClosedSet<GridKey>(); open.Add( new GridSearchNode( key: new GridKey(start, wayPoints.Count), start, previous: null, distanceFromStart: 0.0, estimatedCost: 0.0, wayPoints, targetWayPoint: 0)); while (!open.IsEmpty) { var nodeToExpand = open.DequeueMostPromissing(); if (nodeToExpand.RemainingWayPoints.Count == 0) { var head = new ShortestPathNode( wayPoints.Last().Position, // use the goal position directly targetWayPoint: wayPoints.Count - 1); // the goal way point should be kept here var backtrackingNode = nodeToExpand.Previous; while (backtrackingNode != null) { var node = new ShortestPathNode(backtrackingNode.Position, backtrackingNode.TargetWayPoint); node.CostToNext = TimeSpan.FromSeconds(Distance.Between(node.Position, head.Position) / maxSpeed); node.Next = head; head = node; backtrackingNode = backtrackingNode.Previous; } return head; } closed.Add(nodeToExpand.Key); for (var dx = -1; dx <= 1; dx++) { for (var dy = -1; dy <= 1; dy++) { if (dx == 0 && dy == 0) continue; var nextPoint = new Vector( nodeToExpand.Position.X + dx * stepSize, nodeToExpand.Position.Y + dy * stepSize); var reachedWayPoint = nodeToExpand.RemainingWayPoints[0].ReachedGoal(nextPoint); var remainingWayPoints = reachedWayPoint ? nodeToExpand.RemainingWayPoints.Skip(1).ToList().AsReadOnly() : nodeToExpand.RemainingWayPoints; var targetWayPoint = wayPoints.Count - nodeToExpand.RemainingWayPoints.Count; // I want to keep the ID of the waypoint in the node which reaches the waypoint and only increase it for its childer var key = new GridKey(nextPoint, remainingWayPoints.Count); if (closed.Contains(key)) { continue; } if (collisionDetector.IsCollision(nextPoint)) { closed.Add(key); continue; } var distance = nodeToExpand.DistanceFromStart + (nextPoint - nodeToExpand.Position).CalculateLength(); var node = new GridSearchNode(key, nextPoint, nodeToExpand, distance, distance + 0, remainingWayPoints, targetWayPoint); if (open.Contains(node.Key)) { if (node.DistanceFromStart < open.Get(node.Key).DistanceFromStart) { open.ReplaceExistingWithTheSameKey(node); } } else { open.Add(node); } } } } return null; }