92 lines
2.9 KiB
C#
92 lines
2.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class PegLegIoEnemyController : MonoBehaviour {
|
|
[SerializeField] AudioClip ouch;
|
|
[SerializeField] int health = 5;
|
|
[SerializeField] float hurtTime = 0.5f;
|
|
[SerializeField] int points = 100;
|
|
[SerializeField] int hitPoints = 10;
|
|
[SerializeField] int healthBlinkThreshold = 0;
|
|
[SerializeField] int healthGlitterThreshold = 0;
|
|
[SerializeField] GameObject deathExplosion;
|
|
[SerializeField] GameObject scoreObject;
|
|
|
|
Animator animator;
|
|
AudioSource audioSource;
|
|
PLIGameController game;
|
|
SpriteRenderer spriteRenderer;
|
|
|
|
bool isHurting = false;
|
|
bool isAnimated = true;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
game = GameObject.FindGameObjectWithTag("PegLegIoGame").GetComponent<PLIGameController>();
|
|
animator = GetComponent<Animator>();
|
|
audioSource = game.GetComponent<AudioSource>();
|
|
spriteRenderer = GetComponent<SpriteRenderer>();
|
|
StartCoroutine(Blink());
|
|
if (animator == null || !animator.isActiveAndEnabled) isAnimated = false;
|
|
}
|
|
|
|
public void Hurt(int damage, bool overrideIFrame = false) {
|
|
StartCoroutine(HurtCoroutine(damage, overrideIFrame));
|
|
}
|
|
|
|
IEnumerator OneBlink() {
|
|
spriteRenderer.color = Color.red;
|
|
yield return new WaitForSeconds(0.05f);
|
|
spriteRenderer.color = Color.white;
|
|
}
|
|
|
|
IEnumerator Blink() {
|
|
int blinkCounter = 0;
|
|
const int maxBlink = 2; // divisor for super-fast blink speed
|
|
while (true) {
|
|
if (health < healthBlinkThreshold) {
|
|
blinkCounter = (blinkCounter + 1) % maxBlink;
|
|
if (health < healthGlitterThreshold
|
|
|| blinkCounter == 0) {
|
|
StartCoroutine(OneBlink());
|
|
}
|
|
}
|
|
yield return new WaitForSeconds(0.2f);
|
|
}
|
|
}
|
|
|
|
public IEnumerator HurtCoroutine(int damage, bool overrideIFrame) {
|
|
if (!isHurting || overrideIFrame) {
|
|
audioSource.PlayOneShot(ouch);
|
|
if (isAnimated) animator.SetBool("isHurting", true);
|
|
health -= damage;
|
|
game.Score(hitPoints);
|
|
if (health < 1) {
|
|
Die();
|
|
} else {
|
|
isHurting = true;
|
|
yield return new WaitForSeconds(hurtTime);
|
|
isHurting = false;
|
|
if (isAnimated) animator.SetBool("isHurting", false);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Die() {
|
|
GameObject e = Instantiate(deathExplosion);
|
|
GameObject s = Instantiate(scoreObject);
|
|
e.transform.position = transform.position;
|
|
s.transform.position = transform.position;
|
|
s.GetComponent<TMPro.TextMeshPro>().SetText(points.ToString());
|
|
Destroy(gameObject);
|
|
game.Score(points);
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
}
|
|
}
|