//Ativado pelo botao Start public void releaseMovement() { canMove = true; if (totalMovements > 0) return; // Pre-computa todos os movimentos possiveis while (grid.getCelula(i,j).getCellType() != Celula.CellType.Saida) { for (int k = 0; k < 4; k++) { int I = i + shift_i[k], J = j + shift_j[k]; if (I >= 0 && I < Grid.MAX_SIZE && J >= 0 && J < Grid.MAX_SIZE && !marked[I,J] && (grid.getCelula(I,J).getCellType() == playerColor || grid.getCelula(I,J).getCellType() == Celula.CellType.Saida)) { marked[I,J] = true; movement[totalMovements] = new Utils.Pair<int, int>(I, J); totalMovements++; i = I; j = J; break; } } } }
//Ativado pelo botao Start public void releaseMovement() { canMove = true; while (grid.getCelula(i, j).getCellType() != Celula.CellType.Saida) { for (int k = 0; k < 4; k++) { int I = i + shift_i[k], J = j + shift_j[k]; if (I >= 0 && I < Grid.MAX_SIZE && J >= 0 && J < Grid.MAX_SIZE && !marked[I, J] && (grid.getCelula(I, J).getCellType() == playerColor || grid.getCelula(I, J).getCellType() == Celula.CellType.Saida)) { marked[I, J] = true; movement[totalMovements] = new Utils.Pair <int, int>(I, J); totalMovements++; i = I; j = J; break; } } } }
void Start() { playerColor = Celula.CellType.R; Utils.Pair <Vector3, float> aux = grid.getPlayerPositionAndRotation(); transform.position = aux.first; transform.Rotate(Vector3.up * aux.second); bool found = false; for (i = 0; i < Grid.MAX_SIZE; i++) { for (j = 0; j < Grid.MAX_SIZE; j++) { if (grid.getCelula(i, j).getCellType() == Celula.CellType.Entrada) { found = true; break; } } if (found) { break; } } marked = new bool[Grid.MAX_SIZE, Grid.MAX_SIZE]; movement = new Utils.Pair <int, int> [Grid.MAX_SIZE * Grid.MAX_SIZE]; totalMovements = 0; currentMov = 0; }
// Update is called once per frame void Update() { if (!canMove || currentMov == totalMovements) { //anim.SetBool ("IsWalking", false); //Debug.Log ("idle"); return; } else { //anim.SetBool ("IsWalking", true); //Debug.Log("walking"); } Transform nextTransform; if (currentMov == totalMovements - 1) { nextTransform = grid.exitPlatform.transform; } else { Utils.Pair <int, int> p = movement[currentMov]; nextTransform = grid.getCelula(p.first, p.second).getGameObject().transform; } if (nextTransform == null) { return; } // Quando chega no pivo da celula if (transform.position == nextTransform.position) { // rotacionar o personagem int p_currentMov; Utils.Pair <int, int> p = movement[currentMov]; if (currentMov == totalMovements - 1) { rotatePlayer(0, 1, 0, 0); } else { p_currentMov = currentMov + 1; Utils.Pair <int, int> p_p = movement[p_currentMov]; pMove = rotatePlayer(p.first, p.second, p_p.first, p_p.second); } // fim rotacao grid.getCelula(p.first, p.second).nextColor(); currentMov++; } //"Animacao" entre duas celulas transform.position = Vector3.MoveTowards(transform.position, nextTransform.position, Time.deltaTime * 3); }
void Start() { playerColor = Celula.CellType.R; Utils.Pair<Vector3, float> aux = grid.getPlayerPositionAndRotation (); transform.position = aux.first; transform.Rotate (Vector3.up * aux.second); bool found = false; for (i = 0; i < Grid.MAX_SIZE; i++) { for (j = 0; j < Grid.MAX_SIZE; j++) { if (grid.getCelula(i, j).getCellType() == Celula.CellType.Entrada) { found = true; break; } } if (found) break; } marked = new bool[Grid.MAX_SIZE, Grid.MAX_SIZE]; movement = new Utils.Pair<int, int>[Grid.MAX_SIZE * Grid.MAX_SIZE]; totalMovements = 0; currentMov = 0; }
// Update is called once per frame /*void Update () { if (!canMove || currentMov == totalMovements) { anim.SetBool ("IsWalking", false); //Debug.Log ("idle"); return; } else { anim.SetBool ("IsWalking", true); //Debug.Log("walking"); } Transform nextTransform; if (currentMov == totalMovements - 1) nextTransform = grid.exitPlatform.transform; else { Utils.Pair<int, int> p = movement[currentMov]; nextTransform = grid.getCelula(p.first, p.second).getGameObject().transform; } if (nextTransform == null) return; // Quando chega no pivo da celula if (transform.position == nextTransform.position) { // rotacionar o personagem int p_currentMov; Utils.Pair<int, int> p = movement[currentMov]; if(currentMov == totalMovements - 1) rotatePlayer(0, 1, 0, 0); else{ p_currentMov = currentMov + 1; Utils.Pair<int, int> p_p = movement[p_currentMov]; rotatePlayer(p.first, p.second, p_p.first, p_p.second); } // fim rotacao grid.getCelula (p.first, p.second).nextColor (); currentMov++; } //"Animacao" entre duas celulas transform.position = Vector3.MoveTowards(transform.position, nextTransform.position, Time.deltaTime * 3); } */ void Update() { if (!canMove || currentMov == totalMovements) return; if (currentMov == 0) { Utils.Pair<int, int> p = movement [0]; Vector3 tmp = grid.getCelula (p.first, p.second).getGameObject ().transform.position; nextPosition = new Vector3 (tmp.x, 0.0f, tmp.z); } else if (canChangeColor) { if (currentMov == totalMovements - 1) nextPosition = grid.exitPlatform.transform.position; else { Utils.Pair<int, int> p = movement [currentMov]; Vector3 tmp = grid.getCelula (p.first, p.second).getGameObject ().transform.position; nextPosition = new Vector3 (tmp.x, 0.0f, tmp.z); } Vector3 prevPosition = grid.getCelula (cellToChange.first, cellToChange.second).getGameObject().transform.position; // ATIVAR AQUI DENTRO A ANIMACAO DA MUDANCA DE COR if (currentMov > 0 && Utils.dist (transform.position, prevPosition) > grid.getCellSide () / 1.5f ) { //Debug.Log ("COLOR CHANGE: " + transform.position.ToString() + " " + nextPosition.ToString()); grid.getCelula (cellToChange.first, cellToChange.second).nextColor (); canChangeColor = false; return; } } // Quando chega no pivo da celula if (transform.position == nextPosition) { cellToChange = movement[currentMov]; canChangeColor = true; currentMov++; if (currentMov == totalMovements) return; Celula c = grid.getCelula(movement[currentMov].first, movement[currentMov].second); //Debug.Log ("NEXT CELL: " + c.getGameObject().transform.position.ToString()); //Debug.Log ("CURR TRANSFORM: " + transform.position.ToString()); } //Interpolaçao do movimento entre duas celulas transform.position = Vector3.MoveTowards(transform.position, nextPosition, Time.deltaTime * 3); }
public void BuildScenario() { Utils.Pair <int, int> entrance = new Utils.Pair <int, int> (0, 0); Utils.Pair <int, int> exit = new Utils.Pair <int, int> (0, 0); Utils.Pair <int, int> size = new Utils.Pair <int, int> (0, 0); Utils.Pair <int, int> tl = new Utils.Pair <int, int> (MAX_SIZE, MAX_SIZE); Utils.Pair <int, int> br = new Utils.Pair <int, int> (0, 0); for (int i = 0; i < MAX_SIZE; i++) { for (int j = 0; j < MAX_SIZE; j++) { if (grid[i, j].isColored()) { tl.first = Utils.min(tl.first, i); tl.second = Utils.min(tl.second, j); br.first = Utils.max(br.first, i); br.second = Utils.max(br.second, j); } if (grid[i, j].getCellType() != Celula.CellType.Vazia) { size.first = Utils.max(size.first, i); size.second = Utils.max(size.second, j); } if (grid[i, j].getCellType() == Celula.CellType.Entrada) { entrance.first = i; entrance.second = j; } if (grid[i, j].getCellType() == Celula.CellType.Saida) { exit.first = i; exit.second = j; } } } /* * Corners */ //Top-left GameObject tmp; tmp = Instantiate(corner, new Vector3(cellSide * tl.first, 0.0f, cellSide * tl.second), Quaternion.identity) as GameObject; tmp.transform.Rotate(Vector3.right * 270); tmp.transform.Rotate(Vector3.forward * 180); tmp.transform.parent = this.transform; //Top-right tmp = Instantiate(corner, new Vector3(cellSide * br.first, 0.0f, cellSide * tl.second), Quaternion.identity) as GameObject; tmp.transform.Rotate(Vector3.right * 270); tmp.transform.Rotate(Vector3.forward * 90); tmp.transform.parent = this.transform; //Bottom-left tmp = Instantiate(corner, new Vector3(cellSide * tl.first, 0.0f, cellSide * br.second), Quaternion.identity) as GameObject; tmp.transform.Rotate(Vector3.right * 270); tmp.transform.Rotate(Vector3.forward * 270); tmp.transform.parent = this.transform; //Bottom-right tmp = Instantiate(corner, new Vector3(cellSide * br.first, 0.0f, cellSide * br.second), Quaternion.identity) as GameObject; tmp.transform.Rotate(Vector3.right * 270); tmp.transform.parent = this.transform; /* * Sides */ for (int i = tl.first + 1; i < br.first; i++) { tmp = Instantiate(side, new Vector3(cellSide * i, 0.0f, cellSide * tl.second), Quaternion.identity) as GameObject; tmp.transform.Rotate(Vector3.right * 270); tmp.transform.Rotate(Vector3.forward * 180); tmp.transform.parent = this.transform; tmp = Instantiate(side, new Vector3(cellSide * i, 0.0f, cellSide * br.second), Quaternion.identity) as GameObject; tmp.transform.Rotate(Vector3.right * 270); tmp.transform.parent = this.transform; } for (int j = tl.second + 1; j < br.second; j++) { tmp = Instantiate(side, new Vector3(cellSide * tl.first, 0.0f, cellSide * j), Quaternion.identity) as GameObject; tmp.transform.Rotate(Vector3.right * 270); tmp.transform.Rotate(Vector3.forward * 270); tmp.transform.parent = this.transform; tmp = Instantiate(side, new Vector3(cellSide * br.first, 0.0f, cellSide * j), Quaternion.identity) as GameObject; tmp.transform.Rotate(Vector3.right * 270); tmp.transform.Rotate(Vector3.forward * 90); tmp.transform.parent = this.transform; } /* * Entrada */ Vector3 entranceBridgePosition; Vector3 entrancePlatformPosition; float entranceRotation; Transform ebt = entranceBridge.transform; Transform ept = entrancePlatform.transform; Bounds bridgeBounds = entranceBridge.GetComponent <MeshFilter> ().mesh.bounds; Bounds platformBounds = entrancePlatform.GetComponent <MeshFilter> ().mesh.bounds; float eX = cellSide * entrance.first; float eZ = cellSide * entrance.second; float x, z; // Esquerda if (entrance.second == 0) { x = eX; z = eZ - 0.3f - (bridgeBounds.size.y - 2.0f) / 2.0f; entranceBridgePosition = new Vector3(x, ebt.position.y, z); z -= (bridgeBounds.extents.y + platformBounds.extents.y) - 0.75f; entrancePlatformPosition = new Vector3(x, ept.position.y, z); entranceRotation = 0; } // Direita else if (entrance.second == size.second) { x = eX; z = eZ + 0.3f + (bridgeBounds.size.y - 2.0f) / 2.0f; entranceBridgePosition = new Vector3(x, ebt.position.y, z); z += (bridgeBounds.extents.y + platformBounds.extents.y) - 0.75f; entrancePlatformPosition = new Vector3(x, ept.position.y, z); entranceRotation = 180; } // Cima else if (entrance.first == 0) { x = eX - 0.3f - (bridgeBounds.size.y - 2.0f) / 2.0f; z = eZ; entranceBridgePosition = new Vector3(x, ebt.position.y, z); x -= (bridgeBounds.extents.y + platformBounds.extents.y) - 0.75f; entrancePlatformPosition = new Vector3(x, ept.position.y, z); entranceRotation = 90; } // Baixo else { x = eX + 0.3f + (bridgeBounds.size.y - 2.0f) / 2.0f; z = eZ; entranceBridgePosition = new Vector3(x, ebt.position.y, z); x += (bridgeBounds.extents.y + platformBounds.extents.y) - 0.75f; entrancePlatformPosition = new Vector3(x, ept.position.y, z); entranceRotation = 270; } entranceBridge.transform.position = entranceBridgePosition; entranceBridge.transform.Rotate(Vector3.forward * entranceRotation); entrancePlatform.transform.position = entrancePlatformPosition; entrancePlatform.transform.Rotate(Vector3.forward * entranceRotation); playerPosition = entrancePlatformPosition; playerRotation = entranceRotation; /* * Saida */ Vector3 exitBridgePosition; Vector3 exitPlatformPosition; float exitRotation; Transform xbt = exitBridge.transform; Transform xpt = exitPlatform.transform; bridgeBounds = exitBridge.GetComponent <MeshFilter> ().mesh.bounds; platformBounds = exitPlatform.GetComponent <MeshFilter> ().mesh.bounds; eX = cellSide * exit.first; eZ = cellSide * exit.second; // Esquerda if (exit.second == 0) { x = eX; z = eZ - 0.3f - (bridgeBounds.size.y - 2.0f) / 2.0f; exitBridgePosition = new Vector3(x, xbt.position.y, z); z -= (bridgeBounds.extents.y + platformBounds.extents.y) - 0.5f; exitPlatformPosition = new Vector3(x, xpt.position.y, z); exitRotation = 0; } // Direita else if (exit.second == size.second) { x = eX; z = eZ + 0.3f + (bridgeBounds.size.y - 2.0f) / 2.0f; exitBridgePosition = new Vector3(x, xbt.position.y, z); z += (bridgeBounds.extents.y + platformBounds.extents.y) - 0.5f; exitPlatformPosition = new Vector3(x, xpt.position.y, z); exitRotation = 180; } // Cima else if (exit.first == 0) { x = eX - 0.3f - (bridgeBounds.size.y - 2.0f) / 2.0f; z = eZ; exitBridgePosition = new Vector3(x, xbt.position.y, z); x -= (bridgeBounds.extents.y + platformBounds.extents.y) - 0.5f; exitPlatformPosition = new Vector3(x, xpt.position.y, z); exitRotation = 90; } // Baixo else { x = eX + 0.3f + (bridgeBounds.size.y - 2.0f) / 2.0f; z = eZ; exitBridgePosition = new Vector3(x, xbt.position.y, z); x += (bridgeBounds.extents.y + platformBounds.extents.y) - 0.5f; exitPlatformPosition = new Vector3(x, xpt.position.y, z); exitRotation = 270; } exitBridge.transform.position = exitBridgePosition; exitBridge.transform.Rotate(Vector3.forward * exitRotation); exitPlatform.transform.position = exitPlatformPosition; exitPlatform.transform.Rotate(Vector3.forward * exitRotation); }