public void OnEndDrag(PointerEventData eventData) { if (Tower && allowPlacement) { // set object to white (default colours) when it is dropped Renderer[] rend = Tower.GetComponentsInChildren <Renderer>(); foreach (Renderer r in rend) { r.material.SetColor("_BaseColor", Color.white); } // once the tower is placed, set the towerObject of the tile (used to decide whether or not towers can connect) Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out RaycastHit hit)) { // recalculate these variables as the click is released or sometimes on the rare occasion, // the tower is placed on one square, but appears to be on another Vector3 clickInWorldspace = hit.collider.gameObject.transform.position; Vector3 clickInTilespace = new Vector3(Mathf.RoundToInt(clickInWorldspace.x), 0.0f, Mathf.RoundToInt(clickInWorldspace.z)); // this line is effectively the same as the one above, but when you remove it, conveyors break about 15% of the time. Tower.transform.position = clickInTilespace; Tile t = Tile.Vector3ToTile(clickInTilespace); t.SetTower(Tower); if (Tower.TryGetComponent <Tower>(out Tower towerClassInstance)) { towerClassInstance.OnPlaced(t, moneyCost, ironCost); //t.isWalkable = false; // tile can no longer be walked on since there has been a tower placed on it } if (Tower.TryGetComponent <Conveyors.ConveyorManager>(out Conveyors.ConveyorManager conveyorClassInstance)) { conveyorClassInstance.OnPlaced(moneyCost, ironCost); } if (!developerMode) { PlayerResources.AddMoney(-moneyCost); PlayerResources.AddIron(-ironCost); } } Tower = null; } if (Tower && !allowPlacement) { Destroy(Tower); } }
private void Update() { if (PlayerResources.GetPlayerHealth() < 1) { mainCamera.transform.position = new Vector3(0.0f, 300.0f, 0.0f); //Sets the position and rotation of the camera to be away from the map. mainCamera.transform.rotation = Quaternion.Euler(0.0f, 0.0f, 0.0f); gameCanvas.enabled = false; //Changes canvas being used. gameOverCanvas.enabled = true; string minutes = ((int)(timeSurvived / 60)).ToString(); string seconds = ((int)(timeSurvived % 60)).ToString(); scoreText.text = "You lasted " + minutes + " minutes, " + seconds + " seconds"; gameOver = true; } else { timeSurvived += Time.deltaTime; //Game is not over, so update the timeSurvived timer. } }
public void AttackPlayer() { PlayerResources.AddLives(-1); playerReference.LostHealth(); }
public void EnemyDeath() { Destroy(this.gameObject); PlayerResources.AddMoney(1); }
public void OnDrag(PointerEventData eventData) { //dont do anything until the player has dragged their cursor onto the map - do not place towers through the UI if (EventSystem.current.IsPointerOverGameObject()) { if (Tower) // { // player can cancel placing a tower by dragging it back onto the UI Destroy(Tower); // readyToPlace = true; // } // return; } // on the first frame that the player has dragged their cursor onto the map, create the object if (readyToPlace) { Tower = Instantiate(prefab, Vector3.zero, Quaternion.identity); readyToPlace = false; } Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out RaycastHit hit)) { // convert cursor position into worldspace and tilespace Vector3 clickInWorldspace = hit.collider.gameObject.transform.position; Vector3 clickInTilespace = new Vector3(Mathf.RoundToInt(clickInWorldspace.x), 0.0f, Mathf.RoundToInt(clickInWorldspace.z)); // postion the tower under the cursor Tower.transform.position = clickInTilespace; allowPlacement = false; // if tower is walkable, change model colour to green if (isPlacableWithOrthogonal(clickInTilespace)) { if (developerMode || // if dev mode turned on, place tower OR (PlayerResources.GetMoney() >= moneyCost && PlayerResources.GetIron() >= ironCost)) // of player can afford it, place tower { Renderer[] rend = Tower.GetComponentsInChildren <Renderer>(); foreach (Renderer r in rend) { r.material.SetColor("_BaseColor", Color.green); } allowPlacement = true; } // if player can not afford tower, show as red, and do not allow placement when click released else { Renderer[] rend = Tower.GetComponentsInChildren <Renderer>(); foreach (Renderer r in rend) { r.material.SetColor("_BaseColor", Color.red); } } } // if tower is NOT walkable, change model colour to red else { Renderer[] rend = Tower.GetComponentsInChildren <Renderer>(); foreach (Renderer r in rend) { r.material.SetColor("_BaseColor", Color.red); } allowPlacement = false; } if (Tile.Vector3ToTile(clickInTilespace).towerObject != null) { Renderer[] rend = Tower.GetComponentsInChildren <Renderer>(); foreach (Renderer r in rend) { r.material.SetColor("_BaseColor", Color.red); } allowPlacement = false; //Debug.LogWarning("Player attempted to place a tower ontop of another tower. cancelling placement"); } } }