1
Fork 0
mirror of https://github.com/Steffo99/beat-td.git synced 2024-11-26 17:14:17 +00:00
beat-td/Assets/Scripts/HatTower.cs

56 lines
1.7 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 HatTower : MonoBehaviour {
private AudioSource hatSource;
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()
{
hatSource = 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 30 / bpm
cooldown = 11 / songData.bpm;
2018-04-22 12:07:55 +00:00
//Find next beat
float nextBeatIn = 30 / songData.bpm - (songData.songTime - (Mathf.Floor(songData.songTime / 30 * songData.bpm) * 30 / songData.bpm));
InvokeRepeating("OnBeat", nextBeatIn, 30 / songData.bpm);
2018-04-21 14:29:29 +00:00
}
void Update () {
cooldownRemaining -= Time.deltaTime;
if (cooldownRemaining <= 0)
{
cooldownRemaining = 0;
if (Input.GetKeyDown(KeyCode.Z))
{
//Calculate the power of the
//power = Sqrt(Cos(pi * t))
float power = Mathf.Pow(Mathf.Abs(Mathf.Cos(songData.songTime * Mathf.PI * songData.bpm / 30)), 2);
//Play the sound
hatSource.Play();
//Log the power to console
Debug.Log(power.ToString("0.00"));
//Start the cooldown
cooldownRemaining = cooldown;
}
}
}
2018-04-22 12:07:55 +00:00
void OnBeat()
{
spriteRenderer.color = Color.black;
Invoke("ResetColor", Time.fixedDeltaTime * 2);
}
void ResetColor()
{
spriteRenderer.color = Color.white;
}
2018-04-21 14:29:29 +00:00
}