106 lines
3.2 KiB
C#
106 lines
3.2 KiB
C#
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class Player : Entity
|
|
{
|
|
[Header("Invulnerability")]
|
|
[SerializeField] private float invulnerabilityLength;
|
|
private float currentInvulnerabilityTime;
|
|
[SerializeField] private float invincibilityBlinkLength;
|
|
private float currentInvincibilityBlink;
|
|
[SerializeField] private SpriteRenderer[] playerSprites;
|
|
[SerializeField] private Color invincibilityColor;
|
|
[Header("UI")]
|
|
[SerializeField] private Image[] healthBar;
|
|
[SerializeField] private Sprite heart;
|
|
[SerializeField] private Sprite depletedHeart;
|
|
|
|
[Header("SFX")]
|
|
[SerializeField] private AudioClip hurtSound;
|
|
[SerializeField] private float hurtVolume = 1f;
|
|
[SerializeField] private AudioClip gameOverSound;
|
|
[SerializeField] private float gameOverVolume = 1f;
|
|
[SerializeField] private AudioClip healSound;
|
|
[SerializeField] private float healVolume = 1f;
|
|
private void Update()
|
|
{
|
|
if (!isStalled)
|
|
{
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
abilities[0].TryAbility();
|
|
}
|
|
if (Input.GetMouseButtonDown(1)) //this way kinda sucks but i'll fix it when i feel like it
|
|
{
|
|
abilities[1].TryAbility();
|
|
}
|
|
}
|
|
|
|
if (currentInvulnerabilityTime > 0)
|
|
{
|
|
currentInvulnerabilityTime -= Time.deltaTime;
|
|
if (currentInvincibilityBlink > 0)
|
|
{
|
|
currentInvincibilityBlink -= Time.deltaTime;
|
|
foreach (SpriteRenderer sprite in playerSprites)
|
|
{
|
|
sprite.color = Color.Lerp(invincibilityColor, Color.white, 1f - currentInvincibilityBlink);
|
|
}
|
|
}
|
|
else if (currentInvincibilityBlink <= 0)
|
|
{
|
|
currentInvincibilityBlink = invincibilityBlinkLength;
|
|
}
|
|
if (currentInvulnerabilityTime <= 0)
|
|
{
|
|
foreach (SpriteRenderer sprite in playerSprites)
|
|
{
|
|
sprite.color = Color.white;
|
|
}
|
|
invulnerable = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void Heal(int healing)
|
|
{
|
|
base.Heal(healing);
|
|
UpdateLivesUI();
|
|
WaveManager.instance.audioSource.PlayOneShot(healSound, healVolume);
|
|
}
|
|
|
|
public override void TakeDamage(int damage)
|
|
{
|
|
base.TakeDamage(damage);
|
|
if (!invulnerable)
|
|
{
|
|
WaveManager.instance.audioSource.PlayOneShot(hurtSound, hurtVolume);
|
|
currentInvulnerabilityTime = invulnerabilityLength;
|
|
invulnerable = true;
|
|
}
|
|
UpdateLivesUI();
|
|
GameManager.instance.livesLost++;
|
|
}
|
|
|
|
protected override void OnDeath()
|
|
{
|
|
GameManager.instance.LoseGame();
|
|
WaveManager.instance.audioSource.PlayOneShot(gameOverSound, gameOverVolume);
|
|
}
|
|
private void UpdateLivesUI()
|
|
{
|
|
for (int i = 0; i < healthBar.Length; i++)
|
|
{
|
|
if (i < health)
|
|
{
|
|
healthBar[i].sprite = heart;
|
|
}
|
|
else
|
|
{
|
|
healthBar[i].sprite = depletedHeart;
|
|
}
|
|
}
|
|
}
|
|
}
|