// Manual Creation code for now... pass in an Object that will handle MapCreation public void CreateMap(Texture2D ground, Texture2D wall, Texture2D resource) { Random r = new Random(); for (int y = 0; y < _height; y++) { for (int x = 0; x < _width; x++) { if (r.Next(0, 100) > 90) { _mapLayout[x, y] = new Tile(x, y, wall, 1, false); _searchGrid.SetWalkableAt(new GridPos(x, y), false); } else { _mapLayout[x, y] = new Tile(x, y, ground, 1, true); _searchGrid.SetWalkableAt(new GridPos(x, y), true); } } } for (int i = 0; i < 25; i++) { int x = r.Next(0, 24); int y = r.Next(0, 24); _tileObjects.Add(new Resource(x, y, resource, ResourceType.Rock)); //_searchGrid.SetWalkableAt(new GridPos(x, y), false); } }
/// <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 OkObstacleTest() { _searchGrid.SetWalkableAt(15, 10, false); GridPos startPos = new GridPos(10, 10); GridPos endPos = new GridPos(20, 10); JumpPointParam jpParam = new JumpPointParam(_searchGrid, startPos, endPos); List <GridPos> resultPathList = JumpPointFinder.FindPath(jpParam); Assert.Greater(resultPathList.Count, 2); Assert.AreEqual(startPos.x, resultPathList.First().x); Assert.AreEqual(startPos.y, resultPathList.First().y); Assert.AreEqual(endPos.x, resultPathList.Last().x); Assert.AreEqual(endPos.y, resultPathList.Last().y); }
void CreateGrid() { bool[][][] movableMatrix = new bool[width][][]; for (int widthTrav = 0; widthTrav < width; widthTrav++) { movableMatrix[widthTrav] = new bool[length][]; for (int lengthTrav = 0; lengthTrav < length; lengthTrav++) { movableMatrix[widthTrav][lengthTrav] = new bool[height]; for (int heightTrav = 0; heightTrav < height; heightTrav++) { movableMatrix[widthTrav][lengthTrav][heightTrav] = true; } } } searchGrid = new StaticGrid(width, length, height, movableMatrix); for (int i = 0; i < Obstacles.numObstacles; i++) { if (CheckWithinBounds(Obstacles.obstacleList[i, 0], Obstacles.obstacleList[i, 1], Obstacles.obstacleList[i, 2])) { searchGrid.SetWalkableAt(Obstacles.obstacleList[i, 0], Obstacles.obstacleList[i, 1], Obstacles.obstacleList[i, 2], false); } } }
public void ConstructGrid() { NodePool nodePool = new NodePool(); searchgrid = new DynamicGridWPool(nodePool); foreach (Vector2 p in map.grid.Game_map.Keys) { if (map.grid.Game_map[p].type == TileType.Floor) { searchgrid.SetWalkableAt(new GridPos(p.X, p.Y), true); } else { searchgrid.SetWalkableAt(new GridPos(p.X, p.Y), false); } } }
private void initGrid(BaseGrid grid, bool[][] array) { grid.Reset(); for (int x = 0; x < array.Length; x++) { for (int y = 0; y < array[0].Length; y++) { grid.SetWalkableAt(x, y, array[x][y]); } } }
public Routing(GridPos theCurrent, GridPos theDestination, BaseGrid theGrid) { myCurrent = theCurrent; myDestination = theDestination; theGrid.SetWalkableAt(theDestination, true); myGrid = new JumpPointParam(theGrid, true, DiagonalMovement.IfAtLeastOneWalkable); //myGrid.AllowEndNodeUnWalkable = true; GetNodes(); }
public void SetWalkable(Tower tower) { Vector2 v2 = MapPosToGrid(tower.m_Pos.ToVector3()); int distince = 6; if (tower.m_TowerData.m_Type == ObjectType.ARROW_TOWER) { distince = 3; } int xMin = (int)v2.x - distince; int xMax = (int)v2.x + distince; int yMin = (int)v2.y - distince; int yMax = (int)v2.y + distince; for (int i = xMin; i <= xMax; i++) { for (int y = yMin; y < yMax; y++) { grids.SetWalkableAt(i, y, true); } } }
public void InitGrid() { var mapSource = Resources.Load <TextAsset>("JsonData/MapSourceConfig"); JSONNode data = JSON.Parse(mapSource.text); width = System.Convert.ToInt32(data[0]["width"].AsFloat); height = Convert.ToInt32(data[0]["height"].AsFloat); width = System.Convert.ToInt32(data[0]["width"].AsFloat); height = Convert.ToInt32(data[0]["height"].AsFloat); var cenArray = data[0]["center"].AsObject; var cx = cenArray["x"].AsFloat; var cy = cenArray["y"].AsFloat; var cz = cenArray["z"].AsFloat; center = new Vector3(cx, cy, cz); nodeSize = data[0]["nodeSize"].AsFloat; var mapData = data[0]["mapdata"].AsArray; grids = new StaticGrid(width, height); var i = 0; foreach (SimpleJSON.JSONNode d in mapData) { var v = d.AsInt; if (v == 0) { var r = i / width; var c = i % width; grids.SetWalkableAt(c, r, true); } i++; } var mh = data[0]["mapHeight"].AsArray; mapHeight = new List <float>(width * height); foreach (JSONNode d in mh) { mapHeight.Add(d.AsFloat); } }
private void btnSearch_Click(object sender, EventArgs e) { for (int resultTrav = 0; resultTrav < m_resultLine.Count; resultTrav++) { m_resultLine[resultTrav].Dispose(); } m_resultLine.Clear(); for (int resultTrav = 0; resultTrav < m_resultBox.Count; resultTrav++) { m_resultBox[resultTrav].Dispose(); } m_resultBox.Clear(); GridPos startPos = new GridPos(); GridPos endPos = new GridPos(); for (int widthTrav = 0; widthTrav < width; widthTrav++) { for (int heightTrav = 0; heightTrav < height; heightTrav++) { if (m_rectangles[widthTrav][heightTrav].boxType != BoxType.Wall) { searchGrid.SetWalkableAt(new GridPos(widthTrav, heightTrav), true); } else { searchGrid.SetWalkableAt(new GridPos(widthTrav, heightTrav), false); } if (m_rectangles[widthTrav][heightTrav].boxType == BoxType.Start) { startPos.x = widthTrav; startPos.y = heightTrav; } if (m_rectangles[widthTrav][heightTrav].boxType == BoxType.End) { endPos.x = widthTrav; endPos.y = heightTrav; } } } jumpParam.DiagonalMovement = (DiagonalMovement)cbbJumpType.SelectedIndex; jumpParam.UseRecursive = cbUseRecursive.Checked; jumpParam.Reset(startPos, endPos); List <GridPos> resultList = JumpPointFinder.FindPath(jumpParam); for (int resultTrav = 0; resultTrav < resultList.Count - 1; resultTrav++) { m_resultLine.Add(new GridLine(m_rectangles[resultList[resultTrav].x][resultList[resultTrav].y], m_rectangles[resultList[resultTrav + 1].x][resultList[resultTrav + 1].y])); } for (int widthTrav = 0; widthTrav < jumpParam.SearchGrid.width; widthTrav++) { for (int heightTrav = 0; heightTrav < jumpParam.SearchGrid.height; heightTrav++) { if (jumpParam.SearchGrid.GetNodeAt(widthTrav, heightTrav) == null) { continue; } if (jumpParam.SearchGrid.GetNodeAt(widthTrav, heightTrav).isOpened) { ResultBox resultBox = new ResultBox(widthTrav * 20, heightTrav * 20 + 50, ResultBoxType.Opened); m_resultBox.Add(resultBox); } if (jumpParam.SearchGrid.GetNodeAt(widthTrav, heightTrav).isClosed) { ResultBox resultBox = new ResultBox(widthTrav * 20, heightTrav * 20 + 50, ResultBoxType.Closed); m_resultBox.Add(resultBox); } } } this.Invalidate(); }
private void btnSearch_Click(object sender, EventArgs e) { for (var resultTrav = 0; resultTrav < resultLine.Count; resultTrav++) { resultLine[resultTrav].Dispose(); } resultLine.Clear(); for (var resultTrav = 0; resultTrav < resultBox.Count; resultTrav++) { resultBox[resultTrav].Dispose(); } resultBox.Clear(); var startPos = new GridPos(); var endPos = new GridPos(); for (var x = 0; x < width; x++) { for (var y = 0; y < height; y++) { _ = rectangles[x][y].BoxType != BoxType.Wall ? searchGrid.SetWalkableAt(x, y, true) : searchGrid.SetWalkableAt(x, y, false); if (rectangles[x][y].BoxType == BoxType.Start) { startPos.X = x; startPos.Y = y; } if (rectangles[x][y].BoxType == BoxType.End) { endPos.X = x; endPos.Y = y; } } } jumpParam.DiagonalMovement = (DiagonalMovement)cbbJumpType.SelectedIndex; jumpParam.CurIterationType = cbUseRecursive.Checked ? IterationType.Recursive : IterationType.Loop; jumpParam.Reset(startPos, endPos); var resultList = JumpPointFinder.FindPath(jumpParam); for (var resultTrav = 0; resultTrav < resultList.Count - 1; resultTrav++) { resultLine.Add(new GridLine(rectangles[resultList[resultTrav].X][resultList[resultTrav].Y], rectangles[resultList[resultTrav + 1].X][resultList[resultTrav + 1].Y])); } for (var x = 0; x < jumpParam.SearchGrid.Width; x++) { for (var y = 0; y < jumpParam.SearchGrid.Height; y++) { if (jumpParam.SearchGrid.GetNodeAt(x, y) == null) { continue; } if (jumpParam.SearchGrid.GetNodeAt(x, y).IsOpened) { var resultBox = new ResultBox(x * 20, (y * 20) + 50, ResultBoxType.Opened); this.resultBox.Add(resultBox); } if (jumpParam.SearchGrid.GetNodeAt(x, y).IsClosed) { var resultBox = new ResultBox(x * 20, (y * 20) + 50, ResultBoxType.Closed); this.resultBox.Add(resultBox); } } } Invalidate(); }