using Pathfinding; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlacementManager : MonoBehaviour { private Transform currentBuilding; public GameObject building; public LayerMask layer; public Camera myCamera; public GameObject aStar; private GameController game; public static List placedBuildings; private void Awake() { placedBuildings = new List(); game = GameObject.Find("Game").GetComponent(); } // Start is called before the first frame update void Start() { if (myCamera == null) { myCamera = GetComponent(); } } private Transform currentBuildLocation; private GraphUpdateObject graphUpdateObject; private bool isBlockingPath; // Update is called once per frame void Update() { if (currentBuilding != null) { Ray ray = myCamera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, Mathf.Infinity, layer)) { if (!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) { Vector3 newPosition = hit.transform.position; newPosition.y = 1; currentBuilding.position = newPosition; if (currentBuildLocation == null) { currentBuildLocation = hit.transform; } else { if (currentBuildLocation != hit.transform) { RestoreOriginalMaterial(currentBuildLocation); currentBuildLocation = hit.transform; if (GraphUpdateUtilities.UpdateGraphsNoBlock(graphUpdateObject, game.spawnLocationNode, game.targetLocationNode, true)) { isBlockingPath = false; Debug.Log("The Path is NOT blocked!"); } else { isBlockingPath = true; Debug.Log("The Path is blocked!"); } } } SetBuildLocationMaterial(hit.transform); } } if (Input.GetMouseButtonDown(0)) { if (IsValidBuildPosition(currentBuilding.position)) { TowerController towerController = currentBuilding.gameObject.GetComponent(); if (game.playerComponents.playerController.currentMoney >= towerController.tower.cost) { game.playerComponents.playerController.currentMoney -= towerController.tower.cost; towerController.placedTime = DateTime.Now; placedBuildings.Add(currentBuilding); currentBuilding.GetComponent().enabled = true; if (currentBuilding.GetComponent() != null) { currentBuilding.GetComponent().doTrigger = true; } UpdateGraph(currentBuilding); RestoreOriginalMaterial(currentBuildLocation); currentBuilding = null; //AstarPath.active.Scan(); } } } if (Input.GetMouseButtonDown(1)) { RestoreOriginalMaterial(currentBuildLocation); Destroy(currentBuilding.gameObject); currentBuilding = null; } } } private void SetBuildLocationMaterial(Transform transform) { if(IsValidBuildPosition(currentBuilding.position)) { transform.GetComponent().sharedMaterial = game.GetMaterial("Green"); } else { transform.GetComponent().sharedMaterial = game.GetMaterial("Red"); } } private void RestoreOriginalMaterial(Transform transform) { transform.GetComponent().sharedMaterial = game.GetMaterial("Forcefield"); } private void UpdateGraph(Transform currentBuilding) { // As an example, use the bounding box from the attached collider Bounds bounds = currentBuilding.GetComponent().bounds; var guo = new GraphUpdateObject(bounds) { // Set some settings updatePhysics = true }; AstarPath.active.UpdateGraphs(guo); } private bool IsValidBuildPosition(Vector3 position) { foreach (Transform item in placedBuildings) { if (Vector3.Distance(item.position, position) < 1.0f) { return false; } } if (isBlockingPath) return false; return true; } public bool IsInBuildMode() { return currentBuilding != null; } public void SetCurrentBuilding(GameObject building) { currentBuilding = Instantiate(building).transform; currentBuilding.GetComponent().enabled = false; if (currentBuilding.GetComponent() != null) { currentBuilding.GetComponent().doTrigger = false; } UpdateGraphObject(); } private void UpdateGraphObject() { Bounds bounds = currentBuilding.GetComponent().bounds; //bounds.size = bounds.size / 2; graphUpdateObject = new GraphUpdateObject(bounds); } }