/// <summary> /// Method in charge of setting the state of a character after it has been killed or freed. /// It also calls GUIController.AssignCharacter to show the character in the top right corner. /// When the number of deadCharacters plus the number of aliveCharacters is equal to the number of total characters, /// the game is over. /// <seealso cref="GUIController.AssignCharacter"/> /// </summary> /// <param name="character">Character to set.</param> /// <param name="alive">Indicates if the character has been freed or killed.</param> public void SettleCharacter(MovableEntity character, bool alive) { if (alive) { _savedCharacters++; } else { _deadCharacters++; } character.SetActive(false); guiController.AssignCharacter(character, alive); if (_savedCharacters + _deadCharacters == _spawnManager.TotalCharacters) { CheckGameOver(); } }
/// <summary> /// Method in charge of generating enemies. A random row is chosen to spawn an enemy. /// For that, it casts a ray where the enemy is supposed to be generated. If the ray /// doesn't collide with any other entity, the enemy is taken from the pool if possible and /// spawned in the corresponding position; adding also a new Movement created according to the /// direction and speed of the chosen row indicated in _rowData. /// If the ray collides with something, the method returns false. /// <seealso cref="RowData"/> /// </summary> /// <returns>True if the enemy has been generated. False otherwise.</returns> public bool GenerateEnemy() { int randSpawnPoint = Random.Range(0, _mapRows); Vector3 position = _rowData[randSpawnPoint].SpawnPosition - new Vector3(0, 0, 0.3f); Vector3 rayOrigin = position; Vector3 rayDirection = Vector3.forward; Ray ray = new Ray(rayOrigin, rayDirection); RaycastHit hit; if (!Physics.Raycast(ray, out hit, 15)) { Quaternion rotation = Quaternion.Euler(0, position.x > 0 ? -180 : 0, 0); GameObject enemy = _poolManager.TakeEnemy(); if (enemy == null) { enemy = Instantiate(enemyGenerator); } enemy.transform.position = position; enemy.transform.rotation = rotation; enemy.transform.parent = entityFolder.transform; MovableEntity newEnemy = enemy.GetComponent <MovableEntity>(); Vector2 movement = _rowData[randSpawnPoint].SpawnPosition.x > 0 ? new Vector2(-1.0f, 0.0f) : new Vector2(1.0f, 0.0f); newEnemy.AssignMovement(movement, _rowData[randSpawnPoint].AssignedSpeed); newEnemy.SetActive(true); return(true); } return(false); }
/// <summary> /// Method in charge of spawning Characters. Characters are always followed and preceded by enemies, /// so they also need to be generated. /// For that, it casts a ray where the enemy is supposed to be generated. If the ray /// doesn't collide with any other entity, the proceeding enemy is taken from the pool and generated. /// Then, the character is generated in a position with a different X. Finally, the second enemy is also generated. /// If the ray collides with something, the method returns false. /// <seealso cref="RowData"/> /// </summary> /// <returns>True if the group has been generated. False otherwise.</returns> public bool SpawnCharacter() { int randChar = Random.Range(0, RemainingCharacters); int randSpawnPoint = Random.Range(0, _mapRows); MovableEntity character = _availableCharacters[randChar]; _availableCharacters.Remove(character); MovableEntity[] enemies = new MovableEntity[2]; Vector3 position = _rowData[randSpawnPoint].SpawnPosition - new Vector3(0, 0, 0.3f); Vector3 rayOrigin = position; Vector3 rayDirection = Vector3.forward; Ray ray = new Ray(rayOrigin, rayDirection); if (!Physics.Raycast(ray, 15)) { Quaternion rotation = Quaternion.Euler(0, position.x > 0 ? -180 : 0, 0); Vector2 movement = _rowData[randSpawnPoint].SpawnPosition.x > 0 ? new Vector2(-1.0f, 0.0f) : new Vector2(1.0f, 0.0f); GameObject enemy = _poolManager.TakeEnemy(); if (enemy == null) { enemy = (GameObject)Instantiate(enemyGenerator); } enemy.transform.position = position; enemy.transform.rotation = rotation; MovableEntity newEnemy = enemy.GetComponent <MovableEntity>(); newEnemy.name = enemyGenerator.tag + entityFolder.transform.childCount; newEnemy.transform.parent = entityFolder.transform; newEnemy.AssignMovement(movement, _rowData[randSpawnPoint].AssignedSpeed); newEnemy.SetActive(true); position.x += newEnemy.SpriteData.bounds.size.x * (movement.x * -1); enemies[0] = newEnemy; character.GetComponent <CharController>().AssignEnemy(newEnemy); character.transform.position = position; character.transform.rotation = rotation; character.AssignMovement(movement, _rowData[randSpawnPoint].AssignedSpeed); character.SetActive(true); position.x += character.SpriteData.bounds.size.x * (movement.x * -1); enemy = _poolManager.TakeEnemy(); if (enemy == null) { enemy = (GameObject)Instantiate(enemyGenerator); } enemy.transform.position = position; enemy.transform.rotation = rotation; newEnemy = enemy.GetComponent <MovableEntity>(); newEnemy.name = enemyGenerator.tag + entityFolder.transform.childCount; newEnemy.transform.parent = entityFolder.transform; newEnemy.AssignMovement(movement, _rowData[randSpawnPoint].AssignedSpeed); enemies[1] = newEnemy; character.GetComponent <CharController>().AssignEnemy(newEnemy); newEnemy.SetActive(true); return(true); } return(false); }