1
Fork 0
mirror of https://github.com/Steffo99/beat-td.git synced 2024-11-25 16:54:18 +00:00
beat-td/Assets/Scripts/SnareTower.cs

74 lines
2.4 KiB
C#
Raw Normal View History

2018-04-21 14:29:29 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SnareTower : MonoBehaviour {
2018-04-22 13:02:46 +00:00
public Sprite standardSprite;
public Sprite alternateSprite;
2018-04-23 11:50:24 +00:00
public GameObject projectileObject;
public float maxRange;
public float maxDamage;
2018-04-22 13:02:46 +00:00
2018-04-21 14:29:29 +00:00
private AudioSource snareSource;
private SongData songData;
2018-04-22 12:07:55 +00:00
private SpriteRenderer spriteRenderer;
2018-04-21 14:29:29 +00:00
private float cooldown;
private float cooldownRemaining = 0;
void Start()
{
snareSource = gameObject.GetComponent<AudioSource>();
songData = GameObject.FindGameObjectWithTag("GameController").GetComponent<SongData>();
2018-04-22 12:07:55 +00:00
spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
2018-04-21 14:29:29 +00:00
//The period is 120 / bpm
cooldown = 44 / songData.bpm;
2018-04-22 12:07:55 +00:00
//Find next beat
2018-04-22 13:02:46 +00:00
float nextBeatIn = 180 / songData.bpm - (songData.songTime - (Mathf.Floor(songData.songTime / 120 * songData.bpm) * 120 / songData.bpm));
2018-04-22 12:07:55 +00:00
InvokeRepeating("OnBeat", nextBeatIn, 120 / songData.bpm);
2018-04-21 14:29:29 +00:00
}
void Update () {
cooldownRemaining -= Time.deltaTime;
if (cooldownRemaining <= 0)
{
cooldownRemaining = 0;
2018-04-22 21:05:11 +00:00
}
if (Input.GetKeyDown(KeyCode.C))
{
//Calculate the power
float power = Mathf.Pow(Mathf.Abs(Mathf.Sin(songData.songTime * Mathf.PI * songData.bpm / 120)) * ((cooldown - cooldownRemaining) / cooldown), 2);
//Play the sound
snareSource.volume = power;
snareSource.Play();
2018-04-23 11:50:24 +00:00
//Instantiate the projectile
GameObject projectile = Instantiate(projectileObject, transform.position, transform.rotation);
SnareCollision sc = projectile.GetComponent<SnareCollision>();
sc.maxRange = power * maxRange;
sc.damage = Mathf.CeilToInt(power * maxDamage);
projectile.transform.localScale = new Vector3(power, power, 1);
2018-04-22 21:05:11 +00:00
//Start the cooldown
cooldownRemaining = cooldown;
//Change the sprite
spriteRenderer.sprite = alternateSprite;
Invoke("OnEndAnimation", Time.fixedDeltaTime * 10);
2018-04-21 14:29:29 +00:00
}
}
2018-04-22 12:07:55 +00:00
2018-04-22 13:02:46 +00:00
void OnEndAnimation()
{
spriteRenderer.sprite = standardSprite;
}
2018-04-22 12:07:55 +00:00
void OnBeat()
{
2018-04-22 13:02:46 +00:00
spriteRenderer.color = Color.yellow;
2018-04-22 12:07:55 +00:00
Invoke("ResetColor", Time.fixedDeltaTime * 2);
}
void ResetColor()
{
spriteRenderer.color = Color.white;
}
2018-04-21 14:29:29 +00:00
}