MIF_E31222569/Assets/ToothyV/Scripts/Enemy.cs

97 lines
2.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;
public class Enemy : MonoBehaviour
{
public GameManager gameManager;
[SerializeField] private float moveSpeed;
[SerializeField] private Transform target;
public float health;
public float maxHealth;
public float damage;
public bool isBosStage2;
[SerializeField] private GameObject pastaGigi;
private SaveData saveData;
private void Start()
{
if (pastaGigi != null)
{
pastaGigi.SetActive(false);
}
gameManager = FindObjectOfType<GameManager>();
health = maxHealth;
gameObject.tag = "Enemy";
}
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
}
private bool isDead = false;
private async void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Bullet") && !isDead)
{
GameObject bullet = other.gameObject;
bullet.SetActive(false);
if (health > 0)
{
health -= damage;
if (health >= 80 && isBosStage2)
{
pastaGigi.SetActive(true);
}
if (health <= 0)
{
health = 0;
isDead = true;
await ExecuteBosDeath();
}
}
}
}
private async Task ExecuteBosDeath()
{
SaveData saveData = await Cloudsave.LoadData<SaveData>("DataPlayer");
if (isBosStage2)
{
int currentLevel = gameManager.levelCurrentIndex;
if (currentLevel == saveData.unlockLevel)
{
saveData.unlockLevel = currentLevel + 1;
}
await Cloudsave.SaveData(saveData, "DataPlayer");
gameObject.SetActive(false);
WinScreen.Open();
}
else
{
Debug.Log("Enemy Die");
QuizPopUp.Open();
gameObject.SetActive(false);
}
}
}