private void TowerMouseSelection() { MouseHit?mouseHit = null; bool mouseClick = false; if (!Utils.MouseOverUI()) { mouseHit = cameraController.Scan(Input.mousePosition); mouseClick = Input.GetMouseButtonDown(0); } if (!mouseHit.HasValue) { towerSpotHighlighter.Disable(); if (mouseClick) { SetState(State.Idle); } return; } TowerSpotGrid towerSpot = mouseHit.Value.hitObject.GetComponent <TowerSpotGrid>(); if (towerSpot != null) { Vector3 hitPoint = mouseHit.Value.hitPoint; Vector2Int grid = towerSpot.WorldToGrid(hitPoint); hitPoint = towerSpot.GridToWorld(grid); Tower tower = towerSpot.GetTower(grid); if (tower != null) { towerSpotHighlighter.Enable(hitPoint, Color.green); if (mouseClick) { SetState(State.TowerSelected, tower); } } else { if (mouseClick) { SetState(State.Idle); } towerSpotHighlighter.Disable(); } } else { towerSpotHighlighter.Disable(); } }
private void PlaceTower_Update() { if (Input.GetKeyDown(KeyCode.Escape)) { Destroy(towerInHand.gameObject); towerInHand = null; SetState(State.Idle); return; } var mouseHit = cameraController.ScanFor <TowerSpotGrid>(Input.mousePosition, towerSpotLayer); if (mouseHit.HasValue) { TowerSpotGrid towerSpot = mouseHit.Value.hitObject; Vector3 hitPoint = mouseHit.Value.hitPoint; Vector2Int grid = towerSpot.WorldToGrid(hitPoint); bool available = towerSpot.IsAvailable(grid); hitPoint = towerSpot.GridToWorld(grid); towerSpotHighlighter.Enable(hitPoint, available ? Color.green : Color.red); towerInHand.transform.position = hitPoint; towerRangeIndicator.Enable(towerInHand.transform.position, towerInHand.CurrentLevel.attackRange, towerInHand.CurrentLevel.auraRange); if (available && Input.GetMouseButtonDown(0)) { player.SubtractCurrency(towerInHand.CurrentLevel.cost); towerSpot.Occupy(grid, towerInHand); EventManager.Raise(new TowerSpawnedEvent(towerInHand)); SetState(State.Idle); } } else { towerInHand.transform.position = cameraController.ScreenToWorld(Input.mousePosition); towerSpotHighlighter.Disable(); towerRangeIndicator.Enable(towerInHand.transform.position, towerInHand.CurrentLevel.attackRange, towerInHand.CurrentLevel.auraRange); } }