public bool CheckRoadsAroundExist(Vector2Int currTile) { RoadManager roadManager = MapManager.GetInstance().GetRoadManager(); if (roadManager == null) { return(false); } return(roadManager.CheckMapAvailability(currTile + new Vector2Int(0, 1)) || roadManager.CheckMapAvailability(currTile + new Vector2Int(0, -1)) || roadManager.CheckMapAvailability(currTile + new Vector2Int(1, 0)) || roadManager.CheckMapAvailability(currTile + new Vector2Int(-1, 0))); }
public bool CheckRoadPathExists() { RoadManager roadManager = MapManager.GetInstance().GetRoadManager(); if (roadManager != null) { //check if curr or next road still exist, or if theres any roads around if (!CheckRoadsAroundExist(m_CurrentTile) || !roadManager.CheckMapAvailability(m_CurrentTile) || !roadManager.CheckMapAvailability(m_NextTile)) { return(false); //road doesnt exist } } return(true); }
public void Init(Vector2Int currentTilePos, AnimatorOverrideController animator = null) { m_CurrentTile = currentTilePos; //check if theres anything around, if no road start despawning, this is to prevent infinite loop RoadManager roadManager = MapManager.GetInstance().GetRoadManager(); if (!CheckRoadsAroundExist(m_CurrentTile) || !roadManager.CheckMapAvailability(m_CurrentTile)) { gameObject.SetActive(false); return; } //get the speed and set the animation speed accordingly m_Speed = Random.Range(m_MinMaxSpeed.x, m_MinMaxSpeed.y); m_NPCAnimator.speed = (m_Speed / m_MinMaxSpeed.y) * (m_MinMaxAnimationSpeed.y - m_MinMaxAnimationSpeed.x) + m_MinMaxAnimationSpeed.x; //set the new animation type for the NPC m_NPCAnimator.runtimeAnimatorController = animator; //register the transform position based on current tile pos if (MapManager.GetInstance() != null) { transform.position = MapManager.GetInstance().GetCellCentrePosToWorld(currentTilePos); } else { gameObject.SetActive(false); return; } m_Dir = Vector2Int.zero; m_VistedRoads.Clear(); m_RoadsQueue.Clear(); m_GoingIntoBuilding = false; if (m_SpriteRenderer != null) { m_SpriteRenderer.color = Color.white; } DFSNextRoad(m_CurrentTile); //get the next tile to go to }
void DFSNextRoad(Vector2Int currTile) //get the next block to go { RoadManager roadManager = MapManager.GetInstance().GetRoadManager(); if (roadManager == null) { return; } List <Vector2Int> neighbourRoads = new List <Vector2Int>(); //check up first, if never visited and theres a tile there add it Vector2Int newGridoffsetPos = currTile + new Vector2Int(0, 1); if (roadManager.CheckMapAvailability(newGridoffsetPos) && !m_VistedRoads.ContainsKey(newGridoffsetPos)) { neighbourRoads.Add(newGridoffsetPos); } //check down newGridoffsetPos = currTile + new Vector2Int(0, -1); if (roadManager.CheckMapAvailability(newGridoffsetPos) && !m_VistedRoads.ContainsKey(newGridoffsetPos)) { neighbourRoads.Add(newGridoffsetPos); } //check right newGridoffsetPos = currTile + new Vector2Int(1, 0); if (roadManager.CheckMapAvailability(newGridoffsetPos) && !m_VistedRoads.ContainsKey(newGridoffsetPos)) { neighbourRoads.Add(newGridoffsetPos); } //check left newGridoffsetPos = currTile + new Vector2Int(-1, 0); if (roadManager.CheckMapAvailability(newGridoffsetPos) && !m_VistedRoads.ContainsKey(newGridoffsetPos)) { neighbourRoads.Add(newGridoffsetPos); } if (neighbourRoads.Count == 0) //if theres nothing { //look into the queue to see if theres any other previous roads //if queue is empty, reset the position to NPC current tile and reset the visited list if (m_RoadsQueue.Count == 0) { m_VistedRoads.Clear(); DFSNextRoad(m_CurrentTile); } else { int lastAddedIndex = m_RoadsQueue.Count - 1; Vector2Int prev = m_RoadsQueue[lastAddedIndex]; m_RoadsQueue.RemoveAt(lastAddedIndex); //remove the last element added InitNextTile(prev); } } else //go to next neighbouring tile { m_RoadsQueue.Add(m_CurrentTile); int randomNeighbourIndex = Random.Range(0, neighbourRoads.Count); InitNextTile(neighbourRoads[randomNeighbourIndex]); } }