public void Tick() { if (m_Grid.HasSelectedNode() && Input.GetMouseButtonDown(0)) { Node selectedNode = m_Grid.GetSelectedNode(); bool canOccupy = false; m_Grid.TryOccupyNode(selectedNode, ref canOccupy); if (!canOccupy) { return; } TurretAsset asset = m_Market.ChosenTurret; if (asset != null) { m_Market.BuyTurret(asset); SpawnTurret(asset, selectedNode); m_Grid.UpdatePathFinding(); } else { Debug.Log("Not enough money"); } } }
private void SpawnTurret(TurretAsset asset, Node node) { TurretView view = Object.Instantiate(asset.ViewPrefab); TurretData data = new TurretData(asset, node); data.AttachView(view); Game.Player.TurretSpawned(data); m_Grid.TryOccupyNode(node.PositionOnGrid, true); //node.IsOccupied = true; // TryOccupy() m_Grid.UpdatePathfinding(); }
private void SpawnTurret(TurretAsset asset, Vector2Int coordinate, Node selectedNode) { TurretView view = Object.Instantiate(asset.ViewPrefab); TurretData data = new TurretData(asset, selectedNode); data.AttachView(view); Game.Player.TurretSpawned(data); if (!m_Grid.TryOccupyNode(coordinate, true)) { Debug.Log("Something has gone wrong"); } m_Grid.UpdatePathFinding(); }
private void Update() { if (m_Grid == null || m_Camera == null) { return; } Vector3 mousePosition = Input.mousePosition; Ray ray = m_Camera.ScreenPointToRay(mousePosition); if (Physics.Raycast(ray, out RaycastHit hit)) { if (hit.transform != transform) { return; } Vector3 hitPosition = hit.point; Vector3 difference = hitPosition - m_Offset; int x = (int)(difference.x / m_NodeSize); int y = (int)(difference.z / m_NodeSize); if (Input.GetMouseButtonDown(0)) { if (m_Grid.GetNode(x, y).IsOccupied) { m_Grid.FreeNode(new Vector2Int(x, y)); } else { m_Grid.TryOccupyNode(new Vector2Int(x, y)); } } } }