public void ReloadContent() { var gridXLength = MapConstants.MapWidth / MapConstants.GridWidth; var gridYLength = MapConstants.MapHeight / MapConstants.GridHeight; mCollisionMap = new CollisionNode [ gridXLength, gridYLength ]; // movableMatrix is used to construct a StaticGrid object, which is used by the pathfinder. var movableMatrix = new bool[gridXLength][]; for (var i = 0; i < mCollisionMap.GetLength(0); i++) { movableMatrix[i] = new bool[gridYLength]; for (var j = 0; j < mCollisionMap.GetLength(1); j++) { mCollisionMap[i, j] = new CollisionNode(i, j, Optional <ICollider> .Of(null)); movableMatrix[i][j] = Map.IsOnTop(new Vector2(i * MapConstants.GridWidth, j * MapConstants.GridHeight)); } } mWalkableGrid = new StaticGrid(gridXLength, gridYLength, movableMatrix); }
/// <summary> /// Updates the collision map for the given coordinates and id. If the object identified by the id is already present /// in the collision map the coordinates get updated, otherwise it gets added. /// </summary> /// <param name="collider">The collider to be updated.</param> internal void UpdateCollider(ICollider collider) { if (mLookUpTable.ContainsKey(collider.Id) && collider.Moved) { RemoveCollider(collider); } mLookUpTable[collider.Id] = collider.AbsBounds; if (collider.ColliderGrid == null) { return; } //add the given collider to the collision map. for (var i = 0; i < collider.ColliderGrid.GetLength(1); i++) { for (var j = 0; j < collider.ColliderGrid.GetLength(0); j++) { if (!collider.ColliderGrid[j, i]) { continue; } var x = collider.AbsBounds.X / MapConstants.GridWidth + i; var y = collider.AbsBounds.Y / MapConstants.GridHeight + j; mCollisionMap[x, y] = new CollisionNode(x, y, Optional <ICollider> .Of(collider)); mWalkableGrid.SetWalkableAt(x, y, false); } } }
public void CleanGrid() { for (var i = 0; i < mCollisionMap.GetLength(0); i++) { for (var j = 0; j < mCollisionMap.GetLength(1); j++) { if (mCollisionMap[i, j].Collider.IsPresent()) { if ((mCollisionMap[i, j].Collider.Get().Center / new Vector2(MapConstants.GridWidth, MapConstants.GridHeight)).Length() > 2) { mCollisionMap[i, j] = new CollisionNode(i, j, Optional <ICollider> .Of(null)); mWalkableGrid.SetWalkableAt(i, j, true); } } } } }
public void RemoveCollider(ICollider toRemove) { //Check if the location of an already existing collider needs to be updated. if (toRemove.ColliderGrid == null || !mLookUpTable.ContainsKey(toRemove.Id)) { return; } mCounter++; var oldBounds = mLookUpTable[toRemove.Id]; int i = 0, j = 0; for (var x = oldBounds.X / MapConstants.GridWidth; x + i <= (oldBounds.X + oldBounds.Width) / MapConstants.GridWidth; i++) { for (var y = oldBounds.Y / MapConstants.GridHeight; y + j <= (oldBounds.Y + oldBounds.Height) / MapConstants.GridHeight; j++) { try { if (!toRemove.ColliderGrid[j, i]) { continue; } mCollisionMap[x + i, y + j] = new CollisionNode(x + i, y + j, Optional <ICollider> .Of(null)); mWalkableGrid.SetWalkableAt(x + i, y + j, true); } catch (IndexOutOfRangeException e) { // this can happen due to unit map ignoring its errors.. Debug.WriteLine(e); } } } // seems like a reasonable number. Grid Cleaning works. if (mCounter > 100 * mLookUpTable.Count) { CleanGrid(); mCounter = 0; } }