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

104 lines
3.6 KiB
C#
Raw Permalink Normal View History

2018-04-21 14:29:29 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HatTower : MonoBehaviour {
2018-04-22 13:02:46 +00:00
public Sprite standardSprite;
public Sprite alternateSprite;
2018-04-23 18:09:13 +00:00
public GameObject projectile;
public float maxRange = 1.5f;
public float maxDamage = 4f;
2018-04-22 13:02:46 +00:00
2018-04-21 14:29:29 +00:00
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;
2018-04-23 18:09:13 +00:00
private bool diagonal = false;
private Vector3[] diagDirections =
{
(Vector3.up + Vector3.left) / 1.414f,
(Vector3.up + Vector3.right) / 1.414f,
(Vector3.down + Vector3.left) / 1.414f,
(Vector3.down + Vector3.right) / 1.414f,
};
2018-04-21 14:29:29 +00:00
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;
2018-04-22 21:05:11 +00:00
}
if (Input.GetKeyDown(KeyCode.Z))
{
//Calculate the power of the
//power = Sqrt(Cos(pi * t))
2018-04-23 18:09:13 +00:00
float power = Mathf.Abs(Mathf.Cos(songData.songTime * Mathf.PI * songData.bpm / 30)) * ((cooldown - cooldownRemaining) / cooldown);
2018-04-22 21:05:11 +00:00
//Play the sound
hatSource.volume = power;
hatSource.Play();
2018-04-23 18:09:13 +00:00
//Instantiate the projectiles
if(diagonal)
{
foreach(Vector3 direction in diagDirections)
{
GameObject proj = Instantiate(projectile, transform.position, transform.rotation);
proj.transform.localScale = new Vector3(0.5f * power, 0.5f * power, 1);
SnareCollision sc = proj.GetComponent<SnareCollision>();
sc.direction = direction;
sc.maxRange = maxRange * power;
sc.damage = Mathf.CeilToInt(maxDamage * power);
}
}
else
{
foreach(Vector3 direction in new Vector3[] { Vector3.up, Vector3.down, Vector3.left, Vector3.right })
{
GameObject proj = Instantiate(projectile, transform.position, transform.rotation);
proj.transform.localScale = new Vector3(0.5f * power, 0.5f * power, 1);
SnareCollision sc = proj.GetComponent<SnareCollision>();
sc.direction = direction;
sc.maxRange = maxRange * power;
sc.damage = Mathf.CeilToInt(maxDamage * power);
}
}
diagonal = !diagonal;
2018-04-22 21:05:11 +00:00
//Start the cooldown
cooldownRemaining = cooldown;
//Change the sprite
spriteRenderer.sprite = alternateSprite;
Invoke("OnEndAnimation", Time.fixedDeltaTime * 6);
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
}