Skip to content

Instantly share code, notes, and snippets.

@joshalanwagner
Created September 21, 2015 00:30
Show Gist options
  • Select an option

  • Save joshalanwagner/3ac0d850c498a5ad06be to your computer and use it in GitHub Desktop.

Select an option

Save joshalanwagner/3ac0d850c498a5ad06be to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ChooseDefenderButton : MonoBehaviour {
public float cooldownLength = 3.0f;
public bool timerRunning = false;
public GameObject defenderPrefab;
public static GameObject selectedDefender;
private GameObject defenders;
private float cooldownTimer;
Transform cover;
void Awake ()
{
if (gameObject.transform.childCount > 0)
{
cover = gameObject.transform.GetChild(0);
}
defenders = GameObject.Find("Defenders");
}
void Start ()
{
StartTimer();
}
void Update ()
{
if (timerRunning)
{
cooldownTimer -= Time.deltaTime;
UpdateCoverSize();
if (cooldownTimer <= 0)
{
timerRunning = false; // then we're done.
}
}
}
public void SpawnDefender ()
{
if (timerRunning) {return;}
// call all spawners - they only spawn if they're selected.
foreach (Transform spawner in defenders.transform)
{
spawner.GetComponent<Spawner>().SpawnDefender(defenderPrefab);
}
StartTimer();
}
void UpdateCoverSize ()
{
// set the scale based on the ratio of coolDownTimer to coolDownLength.
if (cooldownTimer > 0) // can't divide by zero
{
cover.transform.localScale = new Vector2 (1.0f, cooldownTimer / cooldownLength);
}
}
void StartTimer()
{
cooldownTimer = cooldownLength;
timerRunning = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment