public void OverlapTilesOut(List <TileStateManager> overlap, float rad) { allOverlappingTilesOut.Clear(); for (int i = 0; i < overlap.Count; i++) { allOverlappingColliders = Physics.OverlapSphere(allOverlappingTilesIn[i].transform.position, rad); for (int j = 0; j < allOverlappingColliders.Length; j++) { TileStateManager tileScript = allOverlappingColliders [j].transform.parent.GetComponent <TileStateManager> (); float heightDifference = Mathf.Abs(overlap[i].transform.localScale.y - tileScript.transform.localScale.y); if (heightDifference < maxHeight && !allOverlappingTilesOut.Contains(tileScript) && tileScript.territory == TileTerritories.Empty) { if (!overlap.Contains(tileScript)) { allOverlappingTilesOut.Add(tileScript); } } else { if (!inconstructibleOverlappingTiles.Contains(tileScript) && !overlap.Contains(tileScript) && !allOverlappingTilesOut.Contains(tileScript)) { inconstructibleOverlappingTiles.Add(tileScript); } } } } }
public void OverlapTilesIn(Transform overObject, float rad) { allOverlappingTilesIn.Clear(); inconstructibleOverlappingTiles.Clear(); allOverlappingColliders = Physics.OverlapSphere(overObject.transform.position, rad); for (int i = 0; i < allOverlappingColliders.Length; i++) { TileStateManager tileScript = allOverlappingColliders [i].transform.parent.GetComponent <TileStateManager> (); float heightDifference = Mathf.Abs(overObject.transform.parent.transform.localScale.y - tileScript.transform.localScale.y); if (heightDifference < maxHeight) { if (!allOverlappingTilesIn.Contains(tileScript)) { allOverlappingTilesIn.Add(tileScript); } } else { if (!allOverlappingTilesIn.Contains(tileScript) && !inconstructibleOverlappingTiles.Contains(tileScript)) { inconstructibleOverlappingTiles.Add(tileScript); } } } }
void RetracePath(TileStateManager startTile, TileStateManager endTile) { path = new List <TileStateManager> (); TileStateManager currentTile = endTile; while (currentTile != startTile) { path.Add(currentTile); currentTile = currentTile.parent; } path.Reverse(); hexGrid.path = path; pathLine.positionCount = path.Count; for (int i = 0; i < path.Count; i++) { if (i == 0) { pathLine.SetPosition(0, startTile.floorPosition + Vector3.up * .5f); } else { pathLine.SetPosition(i, path [i].floorPosition + Vector3.up * .5f); } } }
public void ComplexPathfinding(TileStateManager st, TileStateManager e) { Debug.Log("Pathfinding"); TileStateManager startTile = st; TileStateManager targetTile = e; List <TileStateManager> openSet = new List <TileStateManager> (); HashSet <TileStateManager> closedSet = new HashSet <TileStateManager> (); openSet.Add(startTile); while (openSet.Count > 0) { TileStateManager currentNode = openSet [0]; for (int i = 1; i < openSet.Count; i++) { if (openSet [i].fCost < currentNode.fCost || openSet[i].fCost == currentNode.fCost && openSet[i].hCost < currentNode.hCost) { currentNode = openSet [i]; //Debug.Log ("Pass"); } } openSet.Remove(currentNode); closedSet.Add(currentNode); if (currentNode == targetTile) { RetracePath(startTile, targetTile); return; } foreach (TileStateManager neighbour in GetNeighbours(currentNode, 2)) { Debug.Log("Pass"); if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour); if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newMovementCostToNeighbour; neighbour.hCost = GetDistance(neighbour, targetTile); neighbour.parent = currentNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } } } } }
int GetDistance(TileStateManager tileA, TileStateManager tileB) { int dstX = Mathf.Abs(tileA.gridX - tileB.gridX); int dstY = Mathf.Abs(tileA.gridY - tileB.gridY); if (dstX > dstY) { return(dstY + dstX - (dstY / 2)); } return(dstX + dstY - (dstX / 2)); }
public List <TileStateManager> GetNeighbours(TileStateManager tile) { List <TileStateManager> neighbours = new List <TileStateManager> (); Collider[] cols = Physics.OverlapSphere(tile.worldPosition, 2); foreach (Collider c in cols) { neighbours.Add(c.transform.parent.GetComponent <TileStateManager> ()); } return(neighbours); }
public void SimplePathfinding() { if (overObject != null && selectedObject != null) { if (selectedObject.transform.parent.GetComponent <TileStateManager> ().unit == TileUnits.Empty) { return; } TileStateManager startTile = selectedObject.transform.parent.GetComponent <TileStateManager> (); TileStateManager endTile = overObject.transform.parent.GetComponent <TileStateManager> (); pathLine.SetPosition(0, startTile.transform.position + Vector3.up * startTile.transform.localScale.y + Vector3.up * .5f); pathLine.SetPosition(1, endTile.transform.position + Vector3.up * endTile.transform.localScale.y + Vector3.up * .5f); } }
public List <TileStateManager> GetNeighbours(TileStateManager tile, float rad) { Collider[] overlapCols = Physics.OverlapSphere(tile.transform.position, rad); List <TileStateManager> nbours = new List <TileStateManager> (); Debug.Log(overlapCols.Length); for (int i = 0; i < overlapCols.Length; i++) { TileStateManager tileScript = overlapCols [i].transform.parent.GetComponent <TileStateManager> (); //nbours.Add (tileScript); float heightDifference = Mathf.Abs(tile.transform.localScale.y - tileScript.transform.localScale.y); if (heightDifference < maxHeight && tileScript != tile) { nbours.Add(tileScript); } } return(nbours); }
void Update() { //Inputs if (Input.GetKeyDown(KeyCode.Space)) { CenterOnSelect(); } else if (Input.GetKeyDown(KeyCode.X)) { ShowAllFeeds(); } else if (Input.GetKeyDown(KeyCode.C)) { SettlingMode(); } else if (Input.GetAxis("Mouse ScrollWheel") > 0 && pitch < 1) { cameraPitch(true); } else if (Input.GetAxis("Mouse ScrollWheel") < 0 && pitch > 0) { cameraPitch(false); } else if (mousePos != Input.mousePosition) { PanningCamera(); } else if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); } else if (Input.GetKeyDown(KeyCode.E)) { if (effectIndex < inconstructibleEffects.Length - 1) { effectIndex++; } else { effectIndex = 0; } ChangeEffects(); } else if (Input.GetKeyDown(KeyCode.R)) { SceneManager.LoadScene(0); } else if (Input.GetKeyDown(KeyCode.A)) { SpawnMode(); } else if (Input.GetKeyDown(KeyCode.M)) { armyPath = (armyPath == true ? false : true); } else if (Input.GetKeyUp(KeyCode.P)) { ComplexPathfinding(selectedObject.transform.parent.GetComponent <TileStateManager> (), overObject.transform.parent.GetComponent <TileStateManager> ()); } //OnMouseClick else if (Input.GetMouseButton(0) && overObject != null) { TileStateManager overTile = overObject.transform.parent.GetComponent <TileStateManager> (); if (settlingTown) { Construct(town); } else if (settlingDistrict && overTile.territory != TileTerritories.Inside) { FilterTileFrom(TileTerritories.Empty, false); Construct(district); DeleteGhostDistrict(); } else if (spawningArmy) { overTile.SpawnArmyOnTile(army, new Vector3(overObject.transform.position.x, overObject.transform.parent.transform.localScale.y, overObject.transform.position.z)); Armies.Add(overTile); //overTile.ShowBuilding (true); Destroy(buildingInstance); constructionEffect(false); spawningArmy = false; } else if (!settlingTown && !settlingDistrict && !spawningArmy) { Select(overObject.transform); } } //OnOver ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { //Si l'objet sous le pointeur n'est pas une tuile ne rien faire if (hit.collider.gameObject.tag != "Case") { return; } //si une tuile à déja été survolée if (overObject != null) { //si c'est la même retour if (overObject == hit.collider.gameObject) { return; } } //si aucune tuile n'a été survolée précédemment else { overObject = hit.collider.gameObject; } //si aucun bouton n'est pressé if (!settlingTown && !settlingDistrict && !spawningArmy) { ClearTilesHighlight(); overObject = hit.collider.gameObject; } //si le bouton créer une ville à été préssé else if (settlingTown) { ClearTilesHighlight(); overObject = hit.collider.gameObject; OverlapTilesIn(overObject.transform, objectRadius); OverlapTilesOut(allOverlappingTilesIn, objectRadius); foreach (TileStateManager t in allOverlappingTilesOut) { t.ShowTile(true); t.tile.GetComponent <MeshRenderer> ().material.color = territoriesColor[2]; } foreach (TileStateManager t in allOverlappingTilesIn) { t.ShowFeeds(true); t.ShowTile(true); t.tile.GetComponent <MeshRenderer> ().material.color = territoriesColor[1]; } foreach (TileStateManager t in inconstructibleOverlappingTiles) { t.ShowTile(true); t.tile.GetComponent <MeshRenderer> ().material.color = territoriesColor[4]; } if (overObject != null) { if (buildingInstance == null) { buildingInstance = Instantiate(town, new Vector3(overObject.transform.position.x, overObject.transform.parent.transform.localScale.y, overObject.transform.position.z), Quaternion.identity); buildingInstance.GetComponentInChildren <MeshRenderer> ().material = ghostTown; } else { buildingInstance.transform.position = new Vector3(overObject.transform.position.x, overObject.transform.parent.transform.localScale.y, overObject.transform.position.z); } } } else if (settlingDistrict && hit.collider.transform.parent.GetComponent <TileStateManager> ().territory == TileTerritories.Inside && hit.collider.transform.parent.GetComponent <TileStateManager> ().building == TileBuildings.Empty) { ClearTilesHighlight(); overObject = hit.collider.gameObject; OverlapTilesIn(hit.collider.transform, objectRadius); OverlapTilesOut(allOverlappingTilesIn, objectRadius); for (int i = 0; i < allOverlappingTilesOut.Count; i++) { if (allOverlappingTilesOut [i].territory == TileTerritories.Empty && allOverlappingTilesOut [i] != overObject.transform) { allOverlappingTilesOut [i].ShowTile(true); } } for (int i = 0; i < allOverlappingTilesIn.Count; i++) { if (allOverlappingTilesIn [i].territory == TileTerritories.Outside && allOverlappingTilesIn [i] != overObject.transform) { allOverlappingTilesIn [i].ShowFeeds(true); allOverlappingTilesIn [i].ShowTile(true); } } foreach (TileStateManager t in allOverlappingTilesOut) { if (t.territory == TileTerritories.Empty && t != overObject.transform) { t.ShowTile(true); t.tile.GetComponent <MeshRenderer> ().material.color = territoriesColor[2]; } } foreach (TileStateManager t in allOverlappingTilesIn) { if (t.territory == TileTerritories.Outside && t != overObject.transform) { t.ShowFeeds(true); t.ShowTile(true); t.tile.GetComponent <MeshRenderer> ().material.color = territoriesColor[1]; } } foreach (TileStateManager t in inconstructibleOverlappingTiles) { if (t.territory == TileTerritories.Outside && t != overObject.transform) { t.ShowTile(true); t.tile.GetComponent <MeshRenderer> ().material.color = territoriesColor[4]; } } if (overObject != null) { if (buildingInstance == null) { buildingInstance = Instantiate(district, new Vector3(overObject.transform.position.x, overObject.transform.parent.transform.localScale.y, overObject.transform.position.z), Quaternion.identity); buildingInstance.GetComponentInChildren <MeshRenderer> ().material = ghostTown; } else { buildingInstance.transform.position = new Vector3(overObject.transform.position.x, overObject.transform.parent.transform.localScale.y, overObject.transform.position.z); } } } else if (spawningArmy && hit.collider.transform.parent.GetComponent <TileStateManager> ().building != TileBuildings.Town && hit.collider.transform.parent.GetComponent <TileStateManager> ().building != TileBuildings.District) { overObject = hit.collider.gameObject; if (overObject != null) { if (buildingInstance == null) { buildingInstance = Instantiate(army, new Vector3(overObject.transform.position.x, overObject.transform.parent.transform.localScale.y, overObject.transform.position.z), Quaternion.identity); } else { buildingInstance.transform.position = new Vector3(overObject.transform.position.x, overObject.transform.parent.transform.localScale.y, overObject.transform.position.z); } } } } }