﻿using UnityEngine;

public class ObjectSpawner : SpawnerOnSpline {

    [SerializeField]
    Transform objectPrefab;
    [SerializeField]
    int amount = 5;


    protected override string ObjectsParentName { get { return objectPrefab.name + " parent"; } }


    protected override void Awake()
    {
        base.Awake();
        // Spawn the objects when the environment is setup.
        if (environment != null)
            environment.SplineUdpated += SpawnObjects;
    } 


    public void SpawnObjects()
    {
        SetupVars();
        DestroyObjects();
        if (objectPrefab == null || objectsParent == null)
            return;
        // Spawn objects.
        for (int i = 0; i < amount; i++)
            SpawnObjectOnSpline(objectPrefab, Random.value);
    }


    public void DestroyObjects()
    {
        SetupVars();
        if (environment == null)
            return;
        // Destroy every child of objects parent.
        objectsParent.DestroyChidren();
    }
}
