public RoadBlock(MapHexa.Coordinate coords, int id) { coord = coords; //roadDirection = dir; blockId=id; nextRoadBlock.roadId = -1; nextRoadBlock.roadBlockId = -1; }
public bool setTowerToSpot(MapHexa.Coordinate coords, GameObject tower) { if (towers[coords.rowId, coords.hexaId] == null && tower.tag == "Tower") { towers[coords.rowId, coords.hexaId] = tower; return true; } return false; }
public void deleteRoadBlock(MapHexa.Coordinate coord) { for (int i = roadBlocks.Count - 1; i >= 0; i--) { if (coord.rowId == roadBlocks [i].coord.rowId && coord.hexaId == roadBlocks [i].coord.hexaId) { roadBlocks.RemoveAt (i); break; } } GameObject.Find ("GameController").GetComponent<MapData> ().getHexa (coord).GetComponent<MapHexa> ().roadBlock = null; GameObject.Find ("GameController").GetComponent<MapData> ().getHexa (coord).GetComponent<MapHexa> ().rbid = 0; GameObject.Find ("GameController").GetComponent<MapData> ().getHexa (coord).GetComponent<MapHexa>().setType(MapHexa.HexType.Grass); }
public void setHexaType(int row, int hexa, MapHexa.HexType type) { GameObject hex = getHexa(row, hexa); hex.GetComponent<MapHexa>().setType(type); }
public GameObject getHexa(MapHexa.Coordinate coords) { return getRow (coords.rowId).GetComponent<MapRow> ().getHexagon (coords.hexaId); }
// Adds direction that excludes given direction and its neighbours private List<MapHexa.HexDir> initDirectionsExcNbours(MapHexa.HexDir dir) { List<MapHexa.HexDir> directions = new List<MapHexa.HexDir> (); switch (dir) { case MapHexa.HexDir.E: directions.Add (MapHexa.HexDir.NW); directions.Add (MapHexa.HexDir.SW); directions.Add (MapHexa.HexDir.W); return directions; case MapHexa.HexDir.W: directions.Add (MapHexa.HexDir.NE); directions.Add (MapHexa.HexDir.SE); directions.Add (MapHexa.HexDir.E); return directions; case MapHexa.HexDir.NE: directions.Add (MapHexa.HexDir.SE); directions.Add (MapHexa.HexDir.SW); directions.Add (MapHexa.HexDir.W); return directions; case MapHexa.HexDir.NW: directions.Add (MapHexa.HexDir.E); directions.Add (MapHexa.HexDir.SW); directions.Add (MapHexa.HexDir.SE); return directions; case MapHexa.HexDir.SE: directions.Add (MapHexa.HexDir.NW); directions.Add (MapHexa.HexDir.NE); directions.Add (MapHexa.HexDir.W); return directions; case MapHexa.HexDir.SW: directions.Add (MapHexa.HexDir.NW); directions.Add (MapHexa.HexDir.NE); directions.Add (MapHexa.HexDir.E); return directions; } Debug.LogError ("initDirectionsExcNbours error"); return null; }
//Check if nearby hexa is near this road, excluding the direction we are coming from public bool isHexaNearThisRoad(MapHexa.Coordinate hexCoords, int roadId, MapHexa.HexDir exclude) { List <MapHexa.HexDir> directions = initDirections (); //Debug.Assert(directions.Count==6); directions.Remove (exclude); //for (int i = 0; i < directions.Count; i++) // Debug.Log ("Excluded directions " +directions[i]); MapHexa hexa = mapData.getHexa (hexCoords).GetComponent<MapHexa>(); //bool ret = false; // Loop all directions //Debug.Assert(directions.Count==5); for (int i = 0; i < directions.Count; i++) { //Debug.Log ("Checking direction " +directions[i]); // Take the nearby hex MapHexa nearby = hexa.getNeighbour (directions [i]).GetComponent<MapHexa>(); // Continue if its no road if (nearby.getHexType () != MapHexa.HexType.Road) continue; // Its a road, now check if it belongs to this road else { Roads.Road road = mapData.getRoads().getRoad (roadId); for (int a = 0; a < road.roadBlocks.Count; a++) { if (road.roadBlocks [a].coord.hexaId == nearby.getCoords ().hexaId && road.roadBlocks [a].coord.rowId == nearby.getCoords ().rowId) { //Debug.Log ("isHexaNearThisRoad TRUE: row "+ road.roadBlocks [a].coord.rowId+" hex "+road.roadBlocks [a].coord.hexaId+" in direction "+directions[i]); return true; } } } } return false; }
public bool isHexaAtTheEdge(MapHexa.Coordinate hexCoords) { if (hexCoords.hexaId == 0 || hexCoords.hexaId == columns - 1 || hexCoords.rowId == 0 || hexCoords.rowId == rows - 1) return true; else return false; }
public bool isEndNear(MapHexa hexa) { //MapHexa.Coordinate endCoords = gameController.GetComponent<MapData> ().getRoadEnd ().getCoords (); List <MapHexa.HexDir> directions = initDirections (); if (hexa == null) return false; for (int i = 0; i < directions.Count; i++) { if (hexa.getNeighbour (directions [i]) == null) continue; MapHexa nearby = hexa.getNeighbour (directions [i]).GetComponent<MapHexa>(); if (nearby.getHexType () == MapHexa.HexType.End) return true; } return false; }
private MapHexa.HexDir getCounterDir(MapHexa.HexDir hexdir) { if (hexdir == MapHexa.HexDir.E) return MapHexa.HexDir.W; if (hexdir == MapHexa.HexDir.W) return MapHexa.HexDir.E; if (hexdir == MapHexa.HexDir.NW) return MapHexa.HexDir.SE; if (hexdir == MapHexa.HexDir.SW) return MapHexa.HexDir.NE; if (hexdir == MapHexa.HexDir.NE) return MapHexa.HexDir.SW; if (hexdir == MapHexa.HexDir.SE) return MapHexa.HexDir.NW; Debug.Log ("Error at getCounterdir"); return MapHexa.HexDir.E; }
// Builds first road from start to end. private int buildMainRoad(ref Roads.Road road,MapHexa currentHexa, Roads.Road.RoadBlock currentBlock,int id,MapHexa.HexDir incDir) { //if (id == 83) // return 1; //MapHexa.Coordinate currentCoords = currentRoadBlock.getCoords (); //bool foundNext = false; //Debug.Log ("<color=red> Now at "+currentHexa.getCoords().rowId+" "+currentHexa.getCoords().hexaId+" </color> "); Roads.Road.RoadBlock newBlock; List <MapHexa.HexDir> possibleDirections = initDirectionsExcNbours (incDir); // Randomize next direction for (int i = possibleDirections.Count-1; i >= 0; i--) { // Randomize direction MapHexa.HexDir dir = possibleDirections [Random.Range (0, i+1)]; possibleDirections.Remove (dir); //Debug.Log ("Checking direction "+dir); // Take hexa from that direction if (currentHexa.getNeighbour (dir) == null) continue; MapHexa hexa = currentHexa.getNeighbour (dir).GetComponent<MapHexa>(); // Check if we have reached end if (isEndNear(hexa)) { //Debug.Log ("End found"); newBlock = new Roads.Road.RoadBlock (hexa.getCoords (),id); currentBlock.nextRoadBlock.roadId = road.roadId; currentBlock.nextRoadBlock.roadBlockId = newBlock.blockId; road.addRoadBlock (newBlock); newBlock.finalRoad = true; newBlock.nextRoadBlock.roadId = Constants.RoadEndId; newBlock.nextRoadBlock.roadBlockId = Constants.RoadEndId; hexa.finalR = true; return 1; } //Debug.Log ("<color=red>Going to </color> "+dir); // Check if we can go there if (isHexaAtTheEdge (hexa.getCoords ())) { // Continue to next direction if we can't continue; } if (isHexaNearThisRoad(hexa.getCoords(),road.roadId,getCounterDir(dir)) ){ // Continue to next direction if we can't continue; } // Add new roadBlock with given id newBlock = new Roads.Road.RoadBlock (hexa.getCoords (),id); road.addRoadBlock (newBlock); // Call next roadblock int ret = buildMainRoad(ref road, hexa,newBlock,id+1,getCounterDir(dir)); // Check if we have been successful reaching the end, set hexa to road if (ret == 1) { currentBlock.nextRoadBlock.roadId = road.roadId; currentBlock.nextRoadBlock.roadBlockId = newBlock.blockId; newBlock.finalRoad = true; hexa.finalR = true; // Finally, remove all the extra blocks if (id == 1) { for (int a = road.roadBlocks.Count-1; a >=0; a--) { if (!road.roadBlocks [a].finalRoad) { road.deleteRoadBlock (road.roadBlocks[a].coord); } } } return 1; } // Leave the block only if ret is 1 // Else we have to continue if we can reach the end from here } // If we fail at this point, clean made block and return to previous block //Debug.Break(); return 0; }
private MapHexa isOtherRoadNearby(MapHexa hexa, int myRoadId) { //Debug.Log ("asdf"); List <MapHexa.HexDir> directions = initDirections (); if (hexa == null) return null; for (int i = 0; i < directions.Count; i++) { if (hexa.getNeighbour (directions [i]) == null) continue; MapHexa nearby = hexa.getNeighbour (directions [i]).GetComponent<MapHexa>(); if (nearby.getHexType () == MapHexa.HexType.Road && nearby.roadBlock != null && nearby.roadBlock.roadId != myRoadId) { return nearby; } } return null; }
private int buildSecRoads(ref Roads.Road road,MapHexa currentHexa, Roads.Road.RoadBlock currentBlock,int id,MapHexa.HexDir incDir) { Roads.Road.RoadBlock newBlock; List <MapHexa.HexDir> possibleDirections = initDirectionsExcNbours (incDir); // Randomize next direction for (int i = possibleDirections.Count-1; i >= 0; i--) { // Randomize direction MapHexa.HexDir dir = possibleDirections [Random.Range (0, i+1)]; possibleDirections.Remove (dir); //Debug.Log ("Checking direction "+dir); // Take hexa from that direction if (currentHexa.getNeighbour (dir) == null) continue; MapHexa hexa = currentHexa.getNeighbour (dir).GetComponent<MapHexa>(); // Check if we have reached end OR we are colliding other road here //Debug.Log("pörrr"); if (isHexaAtTheEdge (hexa.getCoords ())) { // Continue to next direction if we can't continue; } MapHexa nearbyHexa = isOtherRoadNearby (hexa, road.roadId); if (nearbyHexa != null) { newBlock = new Roads.Road.RoadBlock (hexa.getCoords (),id); currentBlock.nextRoadBlock.roadId = road.roadId; currentBlock.nextRoadBlock.roadBlockId = newBlock.blockId; road.addRoadBlock (newBlock); newBlock.finalRoad = true; newBlock.nextRoadBlock.roadId = nearbyHexa.roadBlock.roadId; newBlock.nextRoadBlock.roadBlockId = nearbyHexa.roadBlock.blockId; hexa.finalR = true; return 1; } if (isEndNear(hexa)) { newBlock = new Roads.Road.RoadBlock (hexa.getCoords (),id); currentBlock.nextRoadBlock.roadId = road.roadId; currentBlock.nextRoadBlock.roadBlockId = newBlock.blockId; road.addRoadBlock (newBlock); newBlock.finalRoad = true; newBlock.nextRoadBlock.roadId = Constants.RoadEndId; newBlock.nextRoadBlock.roadBlockId = Constants.RoadEndId; hexa.finalR = true; return 1; } // Check if we can go there if (isHexaNearThisRoad(hexa.getCoords(),road.roadId,getCounterDir(dir)) ){ // Continue to next direction if we can't continue; } // Add new roadBlock with given id newBlock = new Roads.Road.RoadBlock (hexa.getCoords (),id); road.addRoadBlock (newBlock); // Call next roadblock int ret = buildSecRoads(ref road, hexa,newBlock,id+1,getCounterDir(dir)); // Check if we have been successful reaching the end, set hexa to road if (ret == 1) { currentBlock.nextRoadBlock.roadId = road.roadId; currentBlock.nextRoadBlock.roadBlockId = newBlock.blockId; newBlock.finalRoad = true; hexa.finalR = true; // Finally, remove all the extra blocks if (id == 1) { for (int a = road.roadBlocks.Count-1; a >=0; a--) { if (!road.roadBlocks [a].finalRoad) { road.deleteRoadBlock (road.roadBlocks[a].coord); } } //Debug.Log ("Roadhexas block: "+ road.getRoadBlock(2).blockId +" and "+ mapData.getHexa(road.getRoadBlock(2).coord).GetComponent<MapHexa>().roadBlock.blockId); } return 1; } // Leave the block only if ret is 1 // Else we have to continue if we can reach the end from here } // If we fail at this point, clean made block and return to previous block //Debug.Break(); return 0; }
public void setCoords(MapHexa.Coordinate coords) { RowList rows=gameController.GetComponent<MapData> ().getRowList (); rows [coords.rowId].GetComponent<MapRow> ().getHexagon (coords.hexaId).GetComponent<MapHexa> ().setType(MapHexa.HexType.End); endCoordinate = coords; }
public RoadBlock(MapHexa.Coordinate coords, int id) { coord = coords; //roadDirection = dir; setBlockId(id); nextRoadBlock = null; }