void makeBridge(DIsland island, DIsland other) { int x1 = island.center_x; int x2 = other.center_x; int z1 = island.center_z; int z2 = other.center_z; while (x1 != x2) { if (map_data[x1, z1].ID == 0 || map_data[x1, z1].ID == 3) { map_data[x1, z1] = new DTile(x1, z1, 4); } x1 += x1 <= x2 ? +1 : -1; //Debug.Log("New x : " + x1 ); } while (z1 != z2) { if (map_data[x1, z1].ID == 0 || map_data[x1, z1].ID == 3) { map_data[x1, z1] = new DTile(x1, z1, 4); } z1 += z1 <= z2 ? +1 : -1; //Debug.Log("New z : " + z1 ); } }
public void makeIsland(int left, int bottom, int width, int height) { DIsland island = new DIsland(left, bottom, width, height); if (!isColliding(island)) { islands.Add(island); //Debug.Log( island.center_x + " , " + island.center_z ); for (int x = 0; x < width; x++) { for (int z = 0; z < height; z++) { if ((x == 0 || x == width - 1 || z == 0 || z == height - 1)) { map_data[left + x, bottom + z] = new DTile(left + x, bottom + z, 3); } else if (x == width / 2 && z == height / 2 && width > 4 && height > 4) { map_data[left + x, bottom + z] = new DTile(left + x, bottom + z, 2); if (width % 2 == 0 && height % 2 == 0) { map_data[left + x - 1, bottom + z - 1] = new DTile(left + x - 1, bottom + z - 1, 2); } if (width % 2 == 0) { map_data[left + x - 1, bottom + z] = new DTile(left + x - 1, bottom + z, 2); } if (height % 2 == 0) { map_data[left + x, bottom + z - 1] = new DTile(left + x, bottom + z - 1, 2); } } else { map_data[left + x, bottom + z] = new DTile(left + x, bottom + z, 1); } } } } else { return; } foreach (DIsland other in islands) { fuseIslands(other); } }
public DTileMap(int size_x, int size_z) { this.size_x = size_x; this.size_z = size_z; map_data = new DTile[size_x, size_z]; for (int x = 0; x < size_x; x++) { for (int z = 0; z < size_z; z++) { map_data[x, z] = new DTile(x, z, 0); } } islands = new List <DIsland> (); }
void OnMouseUpAsButton() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo; if (GetComponent <Collider>().Raycast(ray, out hitInfo, Mathf.Infinity)) { DTile hitTile = dataTileMap.getDTileAt((int)hitInfo.point.x, (int)hitInfo.point.z); if (hitTile.walkable) { if (!useDjkstra) { cube.position = new Vector3(hitTile.x + cubeParent.position.x, currentTileCoord.y, hitTile.z + cubeParent.position.z); } else { generatePathTo((int)(hitTile.x + cubeParent.position.x), (int)(hitTile.z + cubeParent.position.z)); } } } }
void generatePathTo(int x, int z) { cube.GetComponent <UnitManager>().path = null; Dictionary <DTile, float> dist = new Dictionary <DTile, float>(); Dictionary <DTile, DTile> prev = new Dictionary <DTile, DTile>(); List <DTile> unvisited = new List <DTile>(); List <DTile> currentPath = new List <DTile>(); DTile source = dataTileMap.getDTileAt((int)cube.position.x, (int)cube.position.z); DTile target = dataTileMap.getDTileAt(x, z); dist[source] = 0; prev[source] = null; foreach (DTile v in dataTileMap.getMap_Data()) { if (v != source) { dist[v] = Mathf.Infinity; prev[v] = null; } unvisited.Add(v); } while (unvisited.Count > 0) { DTile u = null; foreach (DTile iter in unvisited) { if (u == null || dist[iter] < dist[u]) { u = iter; } } if (u == target) { break; } unvisited.Remove(u); foreach (DTile v in u.neighboors) { float alt = dist[u] + u.distanceTo(v) * v.weight; if (alt < dist[v]) { dist[v] = alt; prev[v] = u; } } } if (prev[target] == null) { return; } DTile next = target; while (next != null) { currentPath.Add(next); next = prev[next]; } currentPath.Reverse(); cube.GetComponent <UnitManager>().path = currentPath.ToArray(); }
void fuseIslands(DIsland island) { if (island.left == 0) { for (int z = 1; z < island.heigth - 1; z++) { map_data[0, island.bottom + z] = new DTile(0, island.bottom + z, 1); } } if (island.bottom == 0) { for (int x = 1; x < island.width - 1; x++) { map_data[island.left + x, 0] = new DTile(island.left + x, 0, 1); } } if (island.top == size_z) { for (int x = 1; x < island.width - 1; x++) { map_data[island.left + x, size_z] = new DTile(island.left + x, size_z, 1); } } if (island.right == 0) { for (int z = 1; z < island.heigth - 1; z++) { map_data[size_x, island.bottom + z] = new DTile(size_x, island.bottom + z, 1); } } if (island.left == 0 && island.bottom == 0) { map_data[0, 0] = new DTile(0, 0, 1); } if (island.left == 0 && island.top == size_z) { map_data[0, size_z] = new DTile(0, size_z, 1); } if (island.right == size_x && island.bottom == 0) { map_data[size_x, 0] = new DTile(size_x, 0, 1); } if (island.right == size_x && island.top == size_z) { map_data[size_x, size_z] = new DTile(size_x, size_z, 1); } for (int x = island.left; x <= island.right; x++) { if (!isNearOcean(x, island.bottom)) { map_data[x, island.bottom] = new DTile(x, island.bottom, 1); } if (!isNearOcean(x, island.top)) { map_data[x, island.top] = new DTile(x, island.top, 1); } } for (int z = island.bottom; z <= island.top; z++) { if (!isNearOcean(island.left, z)) { map_data[island.left, z] = new DTile(island.left, z, 1); } if (!isNearOcean(island.right, z)) { map_data[island.right, z] = new DTile(island.right, z, 1); } } }
public float distanceTo(DTile dTile) { return(Vector2.Distance(new Vector2(x, z), new Vector2(dTile.x, dTile.z))); }