//Makes a string[] that indexes all the chars in the text file then for each char in the text file, the char read is turned into a indexer for the block array(changes block value) protected virtual void CreateGrid() { //Dictionary method taken from Algorithms and inScope Tiles = new Dictionary <GetCoordinates, Tile>(); string[] mapData = ReadMapText(); mapSize = new GetCoordinates(mapData[0].ToCharArray().Length, mapData.Length); int mapX = mapData[0].ToCharArray().Length; int mapY = mapData.Length; Vector3 maxTile = Vector3.zero; Vector3 gridStartPosition = Camera.main.ScreenToWorldPoint(new Vector3(0, Screen.height)); for (int y = 0; y < mapY; y++) { char[] newTiles = mapData[y].ToCharArray(); for (int x = 0; x < mapX; x++) { PlaceTile(newTiles[x].ToString(), x, y, gridStartPosition); } } maxTile = Tiles[new GetCoordinates(mapX - 1, mapY - 1)].transform.position; cameraMovement.SetBounds(new Vector3(maxTile.x + TileSize, maxTile.y - TileSize)); GetSpawnPoints(); }
public void Setup(GetCoordinates gridPos, Vector3 worldPosition, Transform parent) { Walkable = true; isEmpty = true; this.GridPosition = gridPos; transform.position = worldPosition; transform.SetParent(parent); UnityGridManager.Instance.Tiles.Add(gridPos, this); }
protected virtual void GetSpawnPoints() { enemySpawn = new GetCoordinates(0, 0); GameObject tmp = Instantiate(enemySpawnPoint, Tiles[enemySpawn].GetComponent <Tile>().WorldPosition, Quaternion.identity); MonsterSpawn = tmp.GetComponent <SpawnManager>(); MonsterSpawn.name = "MossBlock"; castleSpawn = new GetCoordinates(15, 4); Instantiate(castleSpawnPoint, Tiles[castleSpawn].GetComponent <Tile>().WorldPosition, Quaternion.identity); }
/// <summary> /// The async Task for API call, it also cashes the response data for the selected City /// </summary> /// <returns></returns> public async Task GetForecast() { if (!cachedData.Contains(selectedCity)) { var expiration = DateTimeOffset.UtcNow.AddMinutes(5); var response = await _darkSkyService.GetForecastResponsesAsync(GetCoordinates.GetCoordinatesFromConfig(selectedCity)); Forecast = response; cachedData.Add(selectedCity, Forecast, expiration); } else { Forecast = cachedData.GetCacheItem(selectedCity).Value as List <ForecastResponse>; } }
private static bool CheckDiagonals(Node currentNode, Node neighbor) { GetCoordinates direction = neighbor.GridPosition - currentNode.GridPosition; GetCoordinates firstCheck = new GetCoordinates(currentNode.GridPosition.X + direction.X, currentNode.GridPosition.Y + direction.Y); GetCoordinates secondCheck = new GetCoordinates(currentNode.GridPosition.X, currentNode.GridPosition.Y + direction.Y); if (UnityGridManager.Instance.InGrid(firstCheck) && !UnityGridManager.Instance.Tiles[firstCheck].Walkable) { return(false); } if (UnityGridManager.Instance.InGrid(secondCheck) && !UnityGridManager.Instance.Tiles[secondCheck].Walkable) { return(false); } return(true); }
public GetCoordinatesTests() { _getCoordinates = new GetCoordinates(); }
public static Stack <Node> GetPath(GetCoordinates start, GetCoordinates goal) { if (nodes == null) { CreateNodes(); } //Thank you algorithms class HashSet <Node> openList = new HashSet <Node>(); HashSet <Node> closedList = new HashSet <Node>(); Stack <Node> finalPath = new Stack <Node>(); Node currentNode = nodes[start]; openList.Add(currentNode); while (openList.Count > 0) { for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { GetCoordinates neighborPos = new GetCoordinates(currentNode.GridPosition.X - x, currentNode.GridPosition.Y - y); if (UnityGridManager.Instance.InGrid(neighborPos) && UnityGridManager.Instance.Tiles[neighborPos].Walkable && neighborPos != currentNode.GridPosition) { int gCost = 0; if (Math.Abs(x - y) == 1) { gCost = 10; } else { if (!CheckDiagonals(currentNode, nodes[neighborPos])) { continue; } gCost = 14; } Node neighbor = nodes[neighborPos]; if (openList.Contains(neighbor)) { if (currentNode.G + gCost < neighbor.G) { neighbor.GetValues(currentNode, nodes[goal], gCost); } } else if (!closedList.Contains(neighbor)) { openList.Add(neighbor); neighbor.GetValues(currentNode, nodes[goal], gCost); } } } } openList.Remove(currentNode); closedList.Add(currentNode); //sorting list and getting lowest value if (openList.Count > 0) { currentNode = openList.OrderBy(n => n.F).First(); } if (currentNode == nodes[goal]) { while (currentNode.GridPosition != start) { finalPath.Push(currentNode); currentNode = currentNode.Parent; } break; } } return(finalPath); //Debugging... //GameObject.Find("Debugger").GetComponent<Debugger>().DebugPath(openList, closedList, finalPath); }
public bool InGrid(GetCoordinates position) { return(position.X >= 0 && position.Y >= 0 && position.X < mapSize.X && position.Y < mapSize.Y); }
void Awake() { Instance = this; }