2021-01-13 18:58:14 +01:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class EnemyFollowPlayer : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
private Transform target;
|
|
|
|
|
public float speed = 4;
|
|
|
|
|
private SpriteRenderer enemySprite;
|
|
|
|
|
public int maxHealth = 5;
|
|
|
|
|
public int currentHealth;
|
|
|
|
|
public GameObject enemy;
|
|
|
|
|
public GameObject deathEffect;
|
2021-01-25 22:40:51 +01:00
|
|
|
|
public GameObject text;
|
|
|
|
|
private PlayerScore score;
|
2021-01-13 18:58:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
target = GameObject.FindGameObjectWithTag("Player").transform;
|
|
|
|
|
enemySprite = GetComponent<SpriteRenderer>();
|
|
|
|
|
currentHealth = maxHealth;
|
2021-01-25 22:40:51 +01:00
|
|
|
|
text = GameObject.FindWithTag("Score");
|
|
|
|
|
score = text.GetComponent<PlayerScore>();
|
2021-01-13 18:58:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
transform.position = Vector2.MoveTowards(transform.position, target.position, Time.deltaTime * speed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnTriggerEnter2D(Collider2D other)
|
|
|
|
|
{
|
|
|
|
|
if (other.CompareTag("Player"))
|
|
|
|
|
{
|
|
|
|
|
Vector2 difference = transform.position - other.transform.position;
|
|
|
|
|
transform.position = new Vector2(transform.position.x + difference.x, transform.position.y + difference.y);
|
|
|
|
|
Debug.Log("Player damage taken");
|
2021-01-25 22:40:51 +01:00
|
|
|
|
|
2021-01-13 18:58:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (other.CompareTag("Bullet"))
|
|
|
|
|
{
|
|
|
|
|
Vector2 difference = transform.position - other.transform.position;
|
|
|
|
|
transform.position = new Vector2(transform.position.x + difference.x, transform.position.y + difference.y);
|
|
|
|
|
StartCoroutine(Flash());
|
|
|
|
|
TakeDamage();
|
|
|
|
|
Debug.Log("Enemy hit by bullet");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (other.CompareTag("Enemy"))
|
|
|
|
|
{
|
|
|
|
|
Vector2 difference = transform.position - other.transform.position;
|
|
|
|
|
transform.position = new Vector2(transform.position.x + difference.x, transform.position.y + difference.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TakeDamage()
|
|
|
|
|
{
|
|
|
|
|
currentHealth -= 1;
|
|
|
|
|
Debug.Log("Health decreased");
|
|
|
|
|
if (currentHealth <= 0)
|
|
|
|
|
{
|
|
|
|
|
Die();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Die()
|
|
|
|
|
{
|
|
|
|
|
Instantiate(deathEffect, transform.position, Quaternion.identity);
|
|
|
|
|
Destroy(gameObject);
|
2021-01-25 22:40:51 +01:00
|
|
|
|
score.killedEnemy += 1;
|
2021-01-13 18:58:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerator Flash()
|
|
|
|
|
{
|
|
|
|
|
for (int n = 0; n < 3; n++)
|
|
|
|
|
{
|
|
|
|
|
enemySprite.color = new Color(enemySprite.color.r, enemySprite.color.g, enemySprite.color.b, 0f);
|
|
|
|
|
yield return new WaitForSeconds(0.1f);
|
|
|
|
|
enemySprite.color = new Color(enemySprite.color.r, enemySprite.color.g, enemySprite.color.b, 1f);
|
|
|
|
|
yield return new WaitForSeconds(0.1f);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-27 21:27:05 +01:00
|
|
|
|
|
|
|
|
|
void OnBecameInvisible()
|
|
|
|
|
{
|
|
|
|
|
Destroy(gameObject);
|
|
|
|
|
}
|
2021-01-13 18:58:14 +01:00
|
|
|
|
}
|