public Tile(Vector2 a_position, int a_height, TileMap a_tileMap) : base(a_position * new Vector2(TILE_WIDTH - 32, TILE_HEIGHT) - new Vector2(0, a_height * 40)) { X = (int)a_position.X; Y = (int)a_position.Y; m_layer = 0.500f - a_position.Y / 1000f; m_tileMap = a_tileMap; if (MathManager.isEven((int)a_position.X)) { m_layer -= 0.0001f; } m_tileState = TileState.Normal; m_color = Color.White; m_heightSprites = new Sprite[a_height]; m_height = a_height; m_tileMap = ((GameState)Game.getInstance().getCurrentState()).getTileMap(); for (int i = 0; i < a_height; i++) { m_heightSprites[i] = new Sprite("Tiles//" + m_tileMap.getTileSet() + "mellangrej.png", 1); } }
private void createTileMap(int width, int height) { m_tileMap = new TileMap(width, height, "test"); m_tileMap.load(); }
private static Stack <Tile> findPath(Tile a_startTile, Tile a_endTile) { TileMap l_tileMap = ((GameState)Game.getInstance().getCurrentState()).getTileMap(); Dictionary <Tile, double> l_closedSet = new Dictionary <Tile, double>(); Dictionary <Tile, double> l_openSet = new Dictionary <Tile, double>(); Dictionary <Tile, Tile> l_cameFrom = new Dictionary <Tile, Tile>(); Tile l_neighbor; int[] Xcheck = new int[] { 0 }; int[] Ycheck = new int[] { 0 }; l_openSet.Add(a_startTile, getPathValue(a_startTile, a_endTile)); l_cameFrom.Add(a_startTile, a_startTile); while (l_openSet.Count > 0) { KeyValuePair <Tile, double> l_current = l_openSet.First(); foreach (KeyValuePair <Tile, double> l_kvPair in l_openSet) { if (l_kvPair.Value < l_current.Value) { l_current = l_kvPair; } } if (l_current.Key.getMapPosition() == a_endTile.getMapPosition()) { Stack <Tile> l_reconstructedPath = new Stack <Tile>(); l_reconstructedPath.Push(l_current.Key); return(reconstructPath(l_cameFrom, l_current.Key, l_reconstructedPath)); } l_openSet.Remove(l_current.Key); l_closedSet.Add(l_current.Key, l_current.Value); if (MathManager.isEven(l_current.Key.X)) { Xcheck = new[] { -1, 0, 1, 1, 0, -1 }; Ycheck = new[] { 1, -1, 0, 1, 1, 0 }; } else { Xcheck = new[] { 1, 0, 1, -1, 0, -1 }; Ycheck = new[] { -1, -1, 0, -1, 1, 0 }; } for (int i = 0; i < Xcheck.Length && i < Ycheck.Length; i++) { int l_newX = (int)l_current.Key.getMapPosition().X + Xcheck[i]; int l_newY = (int)l_current.Key.getMapPosition().Y + Ycheck[i]; l_neighbor = l_tileMap.getTile(l_newX, l_newY); if (l_neighbor != null) { //int l_heightDifference = l_current.Key.p_height + l_neighbor.p_height; if (l_closedSet.ContainsKey(l_neighbor) || l_neighbor.isObstructed()) { continue; } double l_tentativeGScore = l_current.Value + getPathValue(l_neighbor, a_endTile) /* + l_heightDifference*/; if (!l_openSet.ContainsKey(l_neighbor) || l_tentativeGScore < l_openSet[l_neighbor]) { l_openSet[l_neighbor] = l_tentativeGScore; l_cameFrom[l_neighbor] = l_current.Key; } } } } return(new Stack <Tile>()); }
public override Stack <Tile> findPath(Vector2 a_startPos, Vector2 a_endPos) { TileMap l_tileMap = ((GameState)Game.getInstance().getCurrentState()).getTileMap(); return(findPath(l_tileMap.getTile(a_startPos), l_tileMap.getTile(a_endPos))); }