// Returns a random coord from the list of coords public Map.Coord GetRandomCoord() { // Pop coord from shuffled coords Map.Coord randomCoord = shuffledTileCoords.Dequeue(); // Return it to back of queue shuffledTileCoords.Enqueue(randomCoord); return(randomCoord); }
// Ensures map is fully accessible using flood fill algorithm bool MapIsFloodable(bool[,] obstacleMap, int currentObstacleCount) { bool[,] visitedNeighbors = new bool[obstacleMap.GetLength(0), obstacleMap.GetLength(1)]; Queue <Map.Coord> queue = new Queue <Map.Coord>(); //Basis: mapCenter is empty queue.Enqueue(currentMap.Center); visitedNeighbors[currentMap.Center.x, currentMap.Center.y] = true; int accessibleTileCount = 1; Map.Coord[] neighborOffsets = { new Map.Coord(-1, 0), // Left new Map.Coord(0, 1), // Top new Map.Coord(1, 0), // Right new Map.Coord(0, -1) // Bottom }; // Recursive step: check neighbors of accessible tiles, if accessible check neighbor's neighbors. while (queue.Count > 0) { Map.Coord tile = queue.Dequeue(); //loop through adjacent tiles to "tile" foreach (Map.Coord neighborOffset in neighborOffsets) { Map.Coord neighbor = tile + neighborOffset; // Check if we are in buonds of obstacle map if ((neighbor.x >= 0 && neighbor.x < currentMap.size.x) && (neighbor.y >= 0 && neighbor.y < currentMap.size.y)) { // Check that neighbor has NOT been checked already and that neighbor is NOT an obstacle if (visitedNeighbors[neighbor.x, neighbor.y] || obstacleMap[neighbor.x, neighbor.y]) { continue; } visitedNeighbors[neighbor.x, neighbor.y] = true; //Enqueue this neighbor queue.Enqueue(neighbor); accessibleTileCount++; } } } int targetAccessibleTileCount = (int)(currentMap.size.x * currentMap.size.y - currentObstacleCount); return(targetAccessibleTileCount == accessibleTileCount); }
public void Init(Map map, Map.Coord pos, Faction faction) { this.map = map; this.pos = pos; this.faction = faction; pos.x = (int)transform.position.x; pos.y = (int)transform.position.z; // Set our color to our faction color rend = gameObject.GetComponent <Renderer>(); //Material tempMaterial = new Material(rend.sharedMaterial); //tempMaterial.color = faction.color; rend.material.color = faction.color; Debug.Log("inited"); map.tiles[pos.x, pos.y].SetOccupationStatus(true); faction.AddUnit(this); }
public void FinishTurn() { // clear our pathfinding info currentPath = null; // Reset our available movement points. SetMoveSpeed(map.GetTileValue(pos)); remainingMovement = moveSpeed; turnStartPos = pos; //Let our resting tile know we have occupied it var currentTile = map.tiles[pos.x, pos.y]; currentTile.SetOccupationStatus(true); // Send out event message that our turn is complete if (turnCompleteEvent != null) { turnCompleteEvent(); } }
Transform CreateObstacle(Map.Coord randomCoord, float heightScale) { float obstacleHeight = Mathf.Lerp(currentMap.minObstacleHeight, currentMap.maxObstacleHeight, heightScale); Vector3 obstaclePosition = CoordToPosition(randomCoord, 0f); // instantiate obstacle Transform newObstacle = Instantiate(obstaclePrefab, obstaclePosition, Quaternion.identity) as Transform; newObstacle.localScale = new Vector3(tileSize, obstacleHeight, tileSize); newObstacle.transform.position = new Vector3(newObstacle.transform.position.x, obstacleHeight / 2 + 0.5f, newObstacle.transform.position.z); newObstacle.localScale = new Vector3((1 - outlinePercent) * tileSize, obstacleHeight, (1 - outlinePercent) * tileSize); // make tiles that obstacles occupy unwalkable currentMap.tiles[randomCoord.x, randomCoord.y].SetWalkability(false); //adjust color Renderer obstacleRenderer = newObstacle.GetComponent <Renderer>(); Material obstacleMaterial = new Material(obstacleRenderer.sharedMaterial); float colorPercent = randomCoord.y / (float)currentMap.size.y; obstacleMaterial.color = Color.Lerp(currentMap.foregroundColor, currentMap.backgroundColor, colorPercent); obstacleRenderer.sharedMaterial = obstacleMaterial; return(newObstacle); }
// Converts coord into real world location Vector3 CoordToPosition(Map.Coord coord, float y) { Vector3 pos = new Vector3(coord.x, 0f, coord.y) * tileSize; return(new Vector3(pos.x, y, pos.z)); }
void Start() { turnStartPos = pos; SetMoveSpeed(map.GetTileValue(pos)); remainingMovement = moveSpeed; }