void Start () { switch (State) { case TetrimoState.Spawning: // when the start of the game is made, you automatically start a spawning tetrimo NextFall = FallingCooldown; AgentAction(); // Create shape and translate it at the center top of the game field. // REMARK: Do not use translate here! Instantiate create a copy of the current object. CreateShape(); transform.position = new Vector3((int)(FieldSize.Right / 2), FieldSize.Top - 1, 0); //you create a shape with the 3D position of the field size to the right/2, etc. //3D position that is transformed to the top // Check if the player has lost the game foreach (Transform child in transform) { if (FieldMatrix[(int)child.position.y, (int)child.position.x] != null) { //if the field matrx position of y and x is not equal to the null, meaning that //there is something at these positions, then that means that the game has ended AgentAction(); Application.LoadLevel("GameOver"); } } name = "Tetrimo#" + (TetrimoCount-1); State = TetrimoState.Falling; break; case TetrimoState.Preview: RotationIndex = Random.Range(0, Shapes.GetLength(1)); ShapeIndex = Random.Range(0, Shapes.GetLength(0)); CreateShape(); transform.position = new Vector3(PreviewHUD.x, PreviewHUD.y); NextTetrimo = this; TetrimoCount++; name = "Tetrimo#" + TetrimoCount; break; } }
void Start() { switch (State) { case TetrimoState.Spawning: NextFall = FallingCooldown; // Create shape and translate it at the center top of the game field. // REMARK: Do not use translate here! Instantiate create a copy of the current object. CreateShape(); transform.position = new Vector3((int)(FieldSize.Right / 2), FieldSize.Top - 1, 0); // Check if the player has lost the game foreach (Transform child in transform) { if (FieldMatrix[(int)child.position.y, (int)child.position.x] != null) { Application.LoadLevel("GameOver"); } } name = "Tetrimo#" + (TetrimoCount - 1); State = TetrimoState.Falling; break; case TetrimoState.Preview: RotationIndex = Random.Range(0, Shapes.GetLength(1)); ShapeIndex = Random.Range(0, Shapes.GetLength(0)); CreateShape(); transform.position = new Vector3(PreviewHUD.x, PreviewHUD.y); NextTetrimo = this; TetrimoCount++; name = "Tetrimo#" + TetrimoCount; break; } }
IEnumerator FallingDown() { //transform refers to unity script transformation that each separate entity shape has a fixed number of children orientations and forms. if (this.CanMoveDown) { transform.Translate(Vector3.down, Space.World); } else { if (State == TetrimoState.Falling) { // After the Tetrimo has landed, the player can move it in the first 400ms. this.GetComponent<AudioSource>().Play(); State = TetrimoState.Landed; yield return new WaitForSeconds(0.4f); while (IsMovingHorizontal) // Wait for end of possible MoveHorizontal - calls yield return new WaitForEndOfFrame(); if (this.CanMoveDown) { State = TetrimoState.Falling; } else { State = TetrimoState.Fixed; //after it is done falling, meaning the player has been able to have it reach the bottom and the user has successfully been able to move the shape all the way foreach (Transform child in transform) { //field matrix refers to the field that the shape operates within Tetrimo.FieldMatrix[Mathf.RoundToInt(child.position.y), Mathf.RoundToInt(child.position.x)] = child.gameObject; } ArrayList lines = FindLines(); if (lines.Count > 0) { //lines means how many lines you've been able to score, hence the line bonus it talks about int FourLineBonus = 0; collectObservations(); //destroying a block when you score a bonus // Destruction animation foreach (int line in lines) { int y = Mathf.CeilToInt(line); for (int i = FieldSize.Left; i <= FieldSize.Right; i++) { //dicated by field size Instantiate(TetrimoExplosionPrefab, FieldMatrix[y, i].transform.position, Quaternion.identity); Destroy(FieldMatrix[y, i]); FieldMatrix[y, i] = null; //you destroy a fieldmatrix/delete size when the value at that position is null. when a block occupies it, //it becomes not null, therefore meaning that other different blocks will be able to fall and become fixed on the position AgentAction(); } } if (lines.Count == 4) { FourLineBonus = FieldMatrix.GetLength(1); Instantiate(FourLinesLabelPrefab, new Vector3(transform.position.x, transform.position.y + 1.0f), Quaternion.identity); AgentAction(); } // update logic yield return new WaitForEndOfFrame(); lines.Sort(); lines.Reverse(); foreach (int line in lines) { if (line >= FieldSize.Top) // if the game hasn't ended e.g. the pieces have reached the top continue; LetLinesAboveFalling(line + 1); foreach (int line in lines) { AgentAction(); addReward(-0.51 * lines.FloorToInt + 0.76 * Bumpiness - 0.36 * aggregateHeight - 0.18 * Bumpiness)); } } } // adding scores MatchCamera.Scores += FieldMatrix.GetLength(1) * lines.Count + MatchCamera.Continuous + FourLineBonus; MatchCamera.Continuous++; // check if next level has been reached if (MatchCamera.Scores >= MatchCamera.Level * MatchCamera.ScoresForNextLevel) { MatchCamera.Level = 1 + Mathf.FloorToInt(((float)MatchCamera.Scores) / MatchCamera.ScoresForNextLevel); AgentAction(); FallingSpeed++; } else { MatchCamera.Continuous = 0; } // Create the new Tetrimo as previewed by destroying the previewed Tetrimo, and place a new created Tetrimo // yield return new WaitForSeconds(0.5f); ActivateAndCreateNewPreview(); // Extract children (TetrimoParts) from Tetrimo. We will delete the Tetrimo itself. Transform[] children = GetComponentsInChildren<Transform>(); for (int i = 0; i < children.Length; i++) children[i].parent = null; Destroy(gameObject); } } } }
IEnumerator FallingDown() { if (this.CanMoveDown) { transform.Translate(Vector3.down, Space.World); } else { if (State == TetrimoState.Falling) { // After the Tetrimo has landed, the player can move it in the first 400ms. this.audio.Play(); State = TetrimoState.Landed; yield return new WaitForSeconds(0.4f); while (IsMovingHorizontal) // Wait for end of possible MoveHorizontal - calls yield return new WaitForEndOfFrame(); if (this.CanMoveDown) { State = TetrimoState.Falling; } else { State = TetrimoState.Fixed; foreach (Transform child in transform) { Tetrimo.FieldMatrix[Mathf.RoundToInt(child.position.y), Mathf.RoundToInt(child.position.x)] = child.gameObject; } ArrayList lines = FindLines(); if (lines.Count > 0) { int FourLineBonus = 0; // Destruction animation foreach (int line in lines) { int y = Mathf.CeilToInt(line); for (int i = FieldSize.Left; i <= FieldSize.Right; i++) { Instantiate(TetrimoExplosionPrefab, FieldMatrix[y, i].transform.position, Quaternion.identity); Destroy(FieldMatrix[y, i]); FieldMatrix[y, i] = null; } } if (lines.Count == 4) { FourLineBonus = FieldMatrix.GetLength(1); Instantiate(FourLinesLabelPrefab, new Vector3(transform.position.x, transform.position.y + 1.0f), Quaternion.identity); } // update logic yield return new WaitForEndOfFrame(); lines.Sort(); lines.Reverse(); foreach (int line in lines) { if (line >= FieldSize.Top) continue; LetLinesAboveFalling(line + 1); } // adding scores MatchCamera.Scores += FieldMatrix.GetLength(1) * lines.Count + MatchCamera.Continuous + FourLineBonus; MatchCamera.Continuous++; // check if next level has been reached if (MatchCamera.Scores >= MatchCamera.Level * MatchCamera.ScoresForNextLevel) { MatchCamera.Level = 1 + Mathf.FloorToInt(((float)MatchCamera.Scores) / MatchCamera.ScoresForNextLevel); FallingSpeed++; } } else { MatchCamera.Continuous = 0; } // Create the new Tetrimo as previewed by destroying the previewed Tetrimo, and place a new created Tetrimo // yield return new WaitForSeconds(0.5f); ActivateAndCreateNewPreview(); // Extract children (TetrimoParts) from Tetrimo. We will delete the Tetrimo itself. Transform[] children = GetComponentsInChildren<Transform>(); for (int i = 0; i < children.Length; i++) children[i].parent = null; Destroy(gameObject); } } } }
void Start () { switch (State) { case TetrimoState.Spawning: NextFall = FallingCooldown; // Create shape and translate it at the center top of the game field. // REMARK: Do not use translate here! Instantiate create a copy of the current object. CreateShape(); transform.position = new Vector3((int)(FieldSize.Right / 2), FieldSize.Top - 1, 0); // Check if the player has lost the game foreach (Transform child in transform) { if (FieldMatrix[(int)child.position.y, (int)child.position.x] != null) { Application.LoadLevel("GameOver"); } } name = "Tetrimo#" + (TetrimoCount-1); State = TetrimoState.Falling; break; case TetrimoState.Preview: RotationIndex = Random.Range(0, Shapes.GetLength(1)); ShapeIndex = Random.Range(0, Shapes.GetLength(0)); CreateShape(); transform.position = new Vector3(PreviewHUD.x, PreviewHUD.y); NextTetrimo = this; TetrimoCount++; name = "Tetrimo#" + TetrimoCount; break; } }
IEnumerator FallingDown() { if (this.CanMoveDown) { transform.Translate(Vector3.down, Space.World); } else { if (State == TetrimoState.Falling) { // After the Tetrimo has landed, the player can move it in the first 400ms. this.audio.Play(); State = TetrimoState.Landed; yield return(new WaitForSeconds(0.4f)); while (IsMovingHorizontal) // Wait for end of possible MoveHorizontal - calls { yield return(new WaitForEndOfFrame()); } if (this.CanMoveDown) { State = TetrimoState.Falling; } else { State = TetrimoState.Fixed; foreach (Transform child in transform) { Tetrimo.FieldMatrix[Mathf.RoundToInt(child.position.y), Mathf.RoundToInt(child.position.x)] = child.gameObject; } ArrayList lines = FindLines(); if (lines.Count > 0) { int FourLineBonus = 0; // Destruction animation foreach (int line in lines) { int y = Mathf.CeilToInt(line); for (int i = FieldSize.Left; i <= FieldSize.Right; i++) { Instantiate(TetrimoExplosionPrefab, FieldMatrix[y, i].transform.position, Quaternion.identity); Destroy(FieldMatrix[y, i]); FieldMatrix[y, i] = null; } } if (lines.Count == 4) { FourLineBonus = FieldMatrix.GetLength(1); Instantiate(FourLinesLabelPrefab, new Vector3(transform.position.x, transform.position.y + 1.0f), Quaternion.identity); } // update logic yield return(new WaitForEndOfFrame()); lines.Sort(); lines.Reverse(); foreach (int line in lines) { if (line >= FieldSize.Top) { continue; } LetLinesAboveFalling(line + 1); } // adding scores MatchCamera.Scores += FieldMatrix.GetLength(1) * lines.Count + MatchCamera.Continuous + FourLineBonus; MatchCamera.Continuous++; // check if next level has been reached if (MatchCamera.Scores >= MatchCamera.Level * MatchCamera.ScoresForNextLevel) { MatchCamera.Level = 1 + Mathf.FloorToInt(((float)MatchCamera.Scores) / MatchCamera.ScoresForNextLevel); FallingSpeed++; } } else { MatchCamera.Continuous = 0; } // Create the new Tetrimo as previewed by destroying the previewed Tetrimo, and place a new created Tetrimo // yield return new WaitForSeconds(0.5f); ActivateAndCreateNewPreview(); // Extract children (TetrimoParts) from Tetrimo. We will delete the Tetrimo itself. Transform[] children = GetComponentsInChildren <Transform>(); for (int i = 0; i < children.Length; i++) { children[i].parent = null; } Destroy(gameObject); } } } }