void OnMouseDown() { // Gets the cell beneath the sphere Cell sphereCell = tgs.CellGetAtPosition(transform.position, true); Debug.Log("Sphere Cell Row = " + sphereCell.row + ", Col = " + sphereCell.column); // Fade cells around sphere position by row and column const int size = 3; for (int j = sphereCell.row - size; j <= sphereCell.row + size; j++) { for (int k = sphereCell.column - size; k <= sphereCell.column + size; k++) { int cellIndex = tgs.CellGetIndex(j, k, true); tgs.CellFadeOut(cellIndex, Color.blue, 1.0f); } } // Get cell neighbours List <Cell> neighbours = tgs.CellGetNeighbours(sphereCell); foreach (Cell cell in neighbours) { tgs.CellFadeOut(cell, Color.red, 2.0f); } }
void BuildPath(int clickedCellIndex) { Debug.Log("Clicked on cell# " + clickedCellIndex); DestroyLOSMarkers(); if (isSelectingStart) { // Selects start cell cellStartIndex = clickedCellIndex; tgs.CellToggleRegionSurface(cellStartIndex, true, Color.yellow); } else { // Clicked on the end cell, then show the path // First clear color of start cell tgs.CellToggleRegionSurface(cellStartIndex, false, Color.white); // Get Path List <int> path = tgs.FindPath(cellStartIndex, clickedCellIndex, 0, 0, 1); // Color the path if (path != null) { for (int k = 0; k < path.Count; k++) { tgs.CellFadeOut(path [k], Color.green, 1f); } } } isSelectingStart = !isSelectingStart; }
// Update is called once per frame void Update() { // Blinks the new territory under the sphere Territory terr = tgs.TerritoryGetAtPosition(transform.position, true); int terrIndex = tgs.TerritoryGetIndex(terr); if (terrIndex != lastTerrIndex) { lastTerrIndex = terrIndex; tgs.TerritoryBlink(terrIndex, Color.red, 0.5f); } // Check ball state switch (state) { case State.IDLE: break; case State.MOVING: if (moveCounter < moveList.Count) { Move(tgs.CellGetPosition(moveList [moveCounter])); } else { moveCounter = 0; state = State.MOVESELECT; } break; case State.MOVESELECT: if (Input.GetMouseButtonUp(0)) //gets path when left mouse is released and over terrain { int t_cell = tgs.cellHighlightedIndex; tgs.CellFadeOut(t_cell, Color.red, 50); if (t_cell != -1) //checks if we selected a cell { int startCell = tgs.CellGetIndex(tgs.CellGetAtPosition(transform.position, true)); float totalCost; moveList = tgs.FindPath(startCell, t_cell, out totalCost); if (moveList == null) { return; } Debug.Log("Total move cost: " + totalCost); tgs.CellFadeOut(moveList, Color.green, 5f); moveCounter = 0; state = State.MOVING; } else { Debug.Log("NULL_CELL"); } } break; } }
void Start() { tgs = TerrainGridSystem.instance; // setup GUI styles labelStyle = new GUIStyle(); labelStyle.alignment = TextAnchor.MiddleCenter; labelStyle.normal.textColor = Color.white; tgs.OnCellHighlight += (int cellIndex, ref bool cancelHighlight) => { cancelHighlight = true; tgs.CellFadeOut(cellIndex, Color.yellow, 2f); }; tgs.OnCellClick += (int cellIndex, int buttonIndex) => MergeCell(cellIndex); // Compute sprite scale //float tileSize = Vector3.Distance(tgs.CellGetVertexPosition(0, 0), tgs.CellGetVertexPosition(0, 3)); //float spriteSize = Vector3.Distance(sprite.bounds.max, sprite.bounds.min); //float scale = tileSize / spriteSize; //for (int k=0;k<100;k++) { // GameObject o = Instantiate<GameObject>(sprite.gameObject); // o.transform.position = tgs.CellGetPosition(Random.Range(0, tgs.cellCount)); // o.transform.localScale = new Vector3(scale, scale, 1f); //} for (int k = 0; k < tgs.cellCount; k++) { tgs.CellSetColor(k, Color.yellow); } }
// Highlight cells sequentially on each frame IEnumerator HighlightCell() { currentCol++; // run across the grid row by row if (currentCol >= tgs.columnCount) { currentCol = 0; currentRow++; if (currentRow >= tgs.rowCount) { currentRow = 0; } } // get cell at current grid position and color it with fade out option Cell cell = tgs.CellGetAtPosition(currentCol, currentRow); if (cell != null) { int cellIndex = tgs.CellGetIndex(cell); float duration = Random.value * 2.5f + 0.5f; Color color = new Color(Random.value, Random.value, Random.value); tgs.CellFadeOut(cellIndex, color, duration); } // trigger next iteration after this frame yield return(new WaitForEndOfFrame()); StartCoroutine(HighlightCell()); }
void TriggerRandomCell(Color color) { // We get a random vector from -0.5..0.5 on both X and Y (z is ignored) Vector2 localPosition = Random.onUnitSphere * 0.5f; Cell cell = tgs.CellGetAtPosition(localPosition); if (cell != null) { int cellIndex = tgs.CellGetIndex(cell); float duration = Random.value * 2.5f + 0.5f; tgs.CellFadeOut(cellIndex, color, duration); } }
// Highlight neighbour cells around character posiiton void ShowNeighbours(Vector3 position) { Cell characterCell = tgs.CellGetAtPosition(position, true); List <Cell> neighbours = tgs.CellGetNeighbours(characterCell); if (neighbours != null) { foreach (Cell cell in neighbours) { tgs.CellFadeOut(cell, Color.red, 2.0f); } } }
void Start() { tgs = TerrainGridSystem.instance; // setup GUI styles labelStyle = new GUIStyle(); labelStyle.alignment = TextAnchor.MiddleCenter; labelStyle.normal.textColor = Color.white; tgs.OnCellHighlight += (int cellIndex, ref bool cancelHighlight) => { cancelHighlight = true; tgs.CellFadeOut(cellIndex, Color.yellow, 2f); }; tgs.OnCellClick += (int cellIndex, int buttonIndex) => MergeCell(cellIndex); }
// Orient the ball toward current selected cell and highlight cell beneath the ball void Update() { if (transform.position.y < -10) { Destroy(gameObject); } // Move ball towards the currently selected cell if (tgs.cellHighlightedIndex >= 0) { Vector3 position = tgs.CellGetPosition(tgs.cellHighlightedIndex); Vector3 direction = (position - transform.position).normalized; rb.AddForce(direction * 100); } // Highlight cell under ball Cell terrainCell = tgs.CellGetAtPosition(transform.position, true); int cellIndex = tgs.CellGetIndex(terrainCell); if (cellIndex >= 0) { tgs.CellFadeOut(cellIndex, Color.yellow, 1f); } }