using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Spawner : MonoBehaviour {

	public Transform SpawnPrefab;
	public float SpawnDelay = 1.0f;
	public float EnergyCost;
	public float EnergyIncomePerSec;
	public float EnergyStartingPool;

	private float energyPool;
	private float lastSpawnTime;

	// Use this for initialization
	void Start () {
		energyPool = EnergyStartingPool;
		lastSpawnTime = Time.fixedTime;
	}
	
	void FixedUpdate () {
		energyPool += Time.fixedDeltaTime*EnergyIncomePerSec;

		if(Time.fixedTime - lastSpawnTime > SpawnDelay)
		{
			if(energyPool >= EnergyCost)
			{
				energyPool -= EnergyCost;
				DoSpawn();
			}
		}
	}

	private void DoSpawn()
	{
		lastSpawnTime = Time.fixedTime;

		Transform s = (Transform) GameObject.Instantiate(SpawnPrefab);
		s.position = transform.position;
		s.localEulerAngles = transform.localEulerAngles;
	}

}
