Esempio n. 1
0
    /// <summary>
    /// Chooses a random row with its corresponding information.
    /// Then casts a ray where the Enemy is supposed to be relocated. If the ray
    /// doesn't collide with any other entity, the Enemy is relocated and removed from the _enemyToRearrange list.
    /// </summary>
    /// <param name="enemy">Enemy to rearrange</param>
    /// <returns>True if the Enemy is relocated. False otherwise.</returns>
    private bool TryToRearrangeEnemy(MovableEntity enemy)
    {
        int     randSpawnPoint = (int)Random.Range(0, _mapRows);
        Vector3 position       = _rowData[randSpawnPoint].SpawnPosition - new Vector3(0, 0, 0.3f);;
        Vector3 rayOrigin      = position - Vector3.back;
        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);

            float xAdjust = _rowData[randSpawnPoint].SpawnPosition.x > 0 ? enemy.SpriteData.bounds.size.x : -enemy.SpriteData.bounds.size.x;
            enemy.transform.position = position + new Vector3(xAdjust, 0, 0);


            enemy.transform.rotation = rotation;
            enemy.AssignMovement(movement, _rowData[randSpawnPoint].AssignedSpeed);

            return(true);
        }

        return(false);
    }
Esempio n. 2
0
    /// <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);
    }
Esempio n. 3
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);
    }