Beispiel #1
0
    /**
     * Method to recovery a enemy when need a new one. First get the enemy of the enmiesDied list,
     * in case theres in no one instantiate a new enemy.
     *
     * Very Impotant: The enemy prefab has to have the same name as the class.
     *
     * This is the most beatifull method that I made.
     *
     */
    public void RecoveryEnemy <T>(float x, float y) where T : Enemy
    {
        int index = GetEnemy <T>();

        if (index == -1)
        {
            //Look this is so nice, remember that the enemy prefab has to have the same name as the class.
            string     pathPrefab = $"Prefabs/Enemys/{typeof(T)}";
            GameObject enemy      = Instantiate(Resources.Load(pathPrefab, typeof(GameObject)), gameObject.transform) as GameObject;
            Enemy      e          = enemy.GetComponent <Enemy>();
            //save enemy when is alive
            enemiesAlive.Add(e);
            enemy.GetComponent <Enemy>().Relocate(x, y);
        }
        else
        {
            Enemy e = enemiesDied[index];
            e.Relocate(x, y);
            e.RestartHealth();
            e.ActiveEnemey();
            enemiesDied.RemoveAt(index);
            enemiesAlive.Add(e);
        }
    }