/*This Works, Y Position is hardcoded * could possibly change that later*/ public void SpawnThatTower() { if (Input.GetMouseButtonDown(0)) { //Test for UI element if (EventSystem.current.IsPointerOverGameObject()) { return; } //send a Ray to the Node Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); /* * Check if the Node hits something, store Hit data in RacastHit * LayerMask is the the mask that the nodes are in, only registers hits * on that layer */ if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, nodeMask)) { NodeV2 node = hit.transform.parent.gameObject.GetComponent <NodeV2>(); //check if there is a tower already placed if (!IsThereATower(node) && node.GetIsTowerPlacable()) { //Grab pos of the hit object add 0.5 to y value so it's not inside Vector3 position = node.transform.position + node.GetEnvOffset(); node.DeleteEnvObject(); GameObject temp = Instantiate(tower, position, Quaternion.identity) as GameObject; node.SetTowerObject(temp); //Mark the node as having a placed tower node.towerPlaced = true; //Make tower child to Node temp.transform.parent = hit.transform; spawnable = false; } else { guiStatusText.text = "A tower is not placeable here."; } } } }
// Update is called once per frame void Update() { if (!IsPaused()) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, nodeMask)) { // HOVERING MECHANIC currentNode = hit.transform.parent.gameObject.GetComponent <NodeV2>(); if (currentNode == null) { if (previousNode != null) { previousNode.GetComponentInChildren <NodeHoverV2>().StopHovering(); previousNode = null; } } else if (previousNode == null) { previousNode = currentNode; hit.transform.gameObject.GetComponent <NodeHoverV2>().Hover(); } else if (currentNode.GetInstanceID() != previousNode.GetInstanceID()) { previousNode.GetComponentInChildren <NodeHoverV2>().StopHovering(); hit.transform.gameObject.GetComponent <NodeHoverV2>().Hover(); previousNode = currentNode; } // SELECTION MECHANIC if (Input.GetMouseButtonDown(0)) { if (EventSystem.current.IsPointerOverGameObject()) { return; } selectedNode = currentNode; if (selectedNode.towerPlaced) { towerSelected = selectedNode.GetTowerObject(); // //Test // //EventSystem.current.SetSelectedGameObject(towerSelected); // //Test // //Tower Status Additon, only for upgraded towers if (towerSelected.GetComponent <TowerController>().GetTowerType() != "base_tower") { selectedTowerStatus.text = towerSelected.GetComponent <TowerController>().formattedName; SetNextPurchaseCost(towerSelected); } if (!towerSelected.GetComponent <Outline>().isActiveAndEnabled) { towerSelected.GetComponent <Outline>().enabled = true; SpawnMenu(towerSelected); } else { towerSelected.GetComponent <Outline>().enabled = false; towerSelected = null; DeSpawnMenu(); } } } // 2. IF NO TOWER && SPAWNABLE, SPAWN TOWER else if (spawnable && selectedNode.GetIsTowerPlacable()) { Vector3 position = selectedNode.transform.position + selectedNode.GetEnvOffset(); selectedNode.DeleteEnvObject(); GameObject temp = Instantiate(towerToBuild, position, Quaternion.identity) as GameObject; selectedNode.SetTowerObject(temp); //Mark the node as having a placed tower selectedNode.towerPlaced = true; //Make tower child to Node temp.transform.parent = selectedNode.transform; spawnable = false; } } } else { Debug.Log(Time.timeScale); } }
/* * Check if there is a tower already placed on the node */ bool IsThereATower(NodeV2 node) { return(node.towerPlaced && node.GetIsTowerPlacable()); }