예제 #1
0
    /// <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);
    }