mirror of
https://github.com/Steffo99/beat-td.git
synced 2024-11-26 17:14:17 +00:00
64 lines
1.9 KiB
C#
64 lines
1.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class SnareTower : MonoBehaviour {
|
|
|
|
public Sprite standardSprite;
|
|
public Sprite alternateSprite;
|
|
|
|
private AudioSource snareSource;
|
|
private SongData songData;
|
|
private SpriteRenderer spriteRenderer;
|
|
private float cooldown;
|
|
private float cooldownRemaining = 0;
|
|
|
|
void Start()
|
|
{
|
|
snareSource = gameObject.GetComponent<AudioSource>();
|
|
songData = GameObject.FindGameObjectWithTag("GameController").GetComponent<SongData>();
|
|
spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
|
|
//The period is 120 / bpm
|
|
cooldown = 44 / songData.bpm;
|
|
//Find next beat
|
|
float nextBeatIn = 180 / songData.bpm - (songData.songTime - (Mathf.Floor(songData.songTime / 120 * songData.bpm) * 120 / songData.bpm));
|
|
InvokeRepeating("OnBeat", nextBeatIn, 120 / songData.bpm);
|
|
}
|
|
|
|
void Update () {
|
|
cooldownRemaining -= Time.deltaTime;
|
|
if (cooldownRemaining <= 0)
|
|
{
|
|
cooldownRemaining = 0;
|
|
if (Input.GetKeyDown(KeyCode.C))
|
|
{
|
|
//Calculate the power of the
|
|
//power = Sqrt(Cos(pi * t))
|
|
float power = Mathf.Pow(Mathf.Abs(Mathf.Sin(songData.songTime * Mathf.PI * songData.bpm / 120)), 2);
|
|
//Play the sound
|
|
snareSource.Play();
|
|
//Start the cooldown
|
|
cooldownRemaining = cooldown;
|
|
//Change the sprite
|
|
spriteRenderer.sprite = alternateSprite;
|
|
Invoke("OnEndAnimation", Time.fixedDeltaTime * 10);
|
|
}
|
|
}
|
|
}
|
|
|
|
void OnEndAnimation()
|
|
{
|
|
spriteRenderer.sprite = standardSprite;
|
|
}
|
|
|
|
void OnBeat()
|
|
{
|
|
spriteRenderer.color = Color.yellow;
|
|
Invoke("ResetColor", Time.fixedDeltaTime * 2);
|
|
}
|
|
|
|
void ResetColor()
|
|
{
|
|
spriteRenderer.color = Color.white;
|
|
}
|
|
}
|