2022-05-26 13:30:59 +02:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class FollowingEnemy : Enemy
|
|
|
|
{
|
2022-06-15 19:54:44 +02:00
|
|
|
public string name2;
|
|
|
|
|
2022-05-26 13:30:59 +02:00
|
|
|
public Transform target;
|
|
|
|
public float chaseRadius;
|
|
|
|
public float attackRadius;
|
|
|
|
public Transform homePosition;
|
|
|
|
|
|
|
|
public float roundingDistance;
|
|
|
|
private Rigidbody2D myRigidbody;
|
|
|
|
public Animator anim;
|
2022-06-04 13:59:18 +02:00
|
|
|
public GameObject other;
|
2022-05-26 13:30:59 +02:00
|
|
|
|
2022-06-06 16:21:26 +02:00
|
|
|
public bool inRange = false;
|
2022-06-11 17:49:19 +02:00
|
|
|
public bool hit = false;
|
2022-06-06 16:21:26 +02:00
|
|
|
public GameObject player;
|
|
|
|
private bool firstAttack = false;
|
|
|
|
|
2022-06-11 17:49:19 +02:00
|
|
|
private float timerDmg = 0f;
|
2022-06-06 16:21:26 +02:00
|
|
|
private float waitTime = 1.0f;
|
|
|
|
|
2022-06-11 17:49:19 +02:00
|
|
|
private float timerHit = 0f;
|
2022-06-12 11:28:14 +02:00
|
|
|
private float hitWaitTime = 0.55f;
|
2022-06-11 17:49:19 +02:00
|
|
|
|
|
|
|
public float thrust;
|
|
|
|
public float knockTime;
|
|
|
|
|
2022-06-15 16:32:14 +02:00
|
|
|
public int isKilled;
|
|
|
|
//isKilled = 0 - mob ALIVE
|
|
|
|
//isKilled = 1 - mob DEAD
|
|
|
|
|
2022-06-15 19:54:44 +02:00
|
|
|
public int continued;
|
|
|
|
|
|
|
|
public int isKilled2;
|
|
|
|
|
2022-06-09 21:51:48 +02:00
|
|
|
|
2022-05-26 13:30:59 +02:00
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
2022-06-15 19:54:44 +02:00
|
|
|
continued = PlayerPrefs.GetInt("continued");
|
|
|
|
if (continued == 1)
|
|
|
|
{
|
|
|
|
isKilled = PlayerPrefs.GetInt(name2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
isKilled = PlayerPrefs.GetInt(enemyName);
|
|
|
|
}
|
2022-06-15 16:32:14 +02:00
|
|
|
if (isKilled == 1)
|
|
|
|
{
|
|
|
|
gameObject.SetActive(false);
|
|
|
|
}
|
2022-05-26 13:30:59 +02:00
|
|
|
myRigidbody = GetComponent<Rigidbody2D>();
|
|
|
|
anim = GetComponent<Animator>();
|
|
|
|
target = GameObject.FindWithTag("Player").transform;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
CheckDistance();
|
2022-06-04 13:59:18 +02:00
|
|
|
//StartCoroutine(Timer());
|
2022-06-09 21:51:48 +02:00
|
|
|
|
|
|
|
//Code below is for dealing damage to player
|
|
|
|
|
2022-06-06 16:21:26 +02:00
|
|
|
if (inRange == true)
|
|
|
|
{
|
|
|
|
if (firstAttack == false)
|
|
|
|
{
|
2022-06-11 17:49:19 +02:00
|
|
|
if (timerDmg >= 0.15f)
|
2022-06-06 16:21:26 +02:00
|
|
|
{
|
|
|
|
firstAttack = true;
|
|
|
|
other.GetComponent<Player>().TakeDamage(baseAttack);
|
2022-06-11 17:49:19 +02:00
|
|
|
timerDmg = 0f;
|
2022-06-06 16:21:26 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-11 17:49:19 +02:00
|
|
|
if (timerDmg >= waitTime)
|
2022-06-06 16:21:26 +02:00
|
|
|
{
|
2022-06-11 17:49:19 +02:00
|
|
|
timerDmg = 0f;
|
2022-06-06 16:21:26 +02:00
|
|
|
other.GetComponent<Player>().TakeDamage(baseAttack);
|
|
|
|
}
|
2022-06-11 17:49:19 +02:00
|
|
|
timerDmg += Time.deltaTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
timerHit += Time.deltaTime;
|
|
|
|
if(hit == true)
|
|
|
|
{
|
|
|
|
if(timerHit >= hitWaitTime)
|
|
|
|
{
|
|
|
|
TakeDamage(1.0f);
|
|
|
|
hit = false;
|
|
|
|
timerHit = 0f;
|
|
|
|
TakeKnockback();
|
|
|
|
}
|
2022-06-06 16:21:26 +02:00
|
|
|
}
|
2022-06-15 16:32:14 +02:00
|
|
|
if (gameObject.active == false)
|
|
|
|
{
|
|
|
|
isKilled = 1;
|
|
|
|
PlayerPrefs.SetInt(enemyName, isKilled);
|
|
|
|
}
|
2022-05-26 13:30:59 +02:00
|
|
|
}
|
|
|
|
|
2022-06-06 16:21:26 +02:00
|
|
|
|
2022-05-26 13:30:59 +02:00
|
|
|
void CheckDistance()
|
|
|
|
{
|
2022-06-04 13:59:18 +02:00
|
|
|
if(Vector3.Distance(target.position, transform.position) <= chaseRadius && Vector3.Distance(target.position, transform.position) > attackRadius)
|
2022-05-26 13:30:59 +02:00
|
|
|
{
|
|
|
|
transform.position = Vector3.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
|
|
|
|
}
|
|
|
|
}
|
2022-06-04 13:59:18 +02:00
|
|
|
|
2022-06-06 16:21:26 +02:00
|
|
|
|
|
|
|
void OnTriggerEnter2D(Collider2D collision)
|
2022-06-04 13:59:18 +02:00
|
|
|
{
|
2022-06-11 17:49:19 +02:00
|
|
|
///For attacking player
|
2022-06-06 16:21:26 +02:00
|
|
|
if (collision.tag == "PlayerHitbox")
|
|
|
|
{
|
|
|
|
inRange = true;
|
|
|
|
firstAttack = false;
|
|
|
|
}
|
2022-06-09 21:51:48 +02:00
|
|
|
|
|
|
|
if (collision.tag == "AttackHitbox")
|
|
|
|
{
|
2022-06-11 17:49:19 +02:00
|
|
|
hit = true;
|
2022-06-09 21:51:48 +02:00
|
|
|
}
|
2022-06-04 13:59:18 +02:00
|
|
|
}
|
|
|
|
|
2022-06-06 16:21:26 +02:00
|
|
|
void OnTriggerStay2D(Collider2D collision)
|
2022-06-04 13:59:18 +02:00
|
|
|
{
|
2022-06-11 17:49:19 +02:00
|
|
|
///For attacking player
|
2022-06-06 16:21:26 +02:00
|
|
|
if (collision.tag == "PlayerHitbox")
|
2022-06-04 13:59:18 +02:00
|
|
|
{
|
2022-06-06 16:21:26 +02:00
|
|
|
inRange = true;
|
2022-06-04 13:59:18 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-06 16:21:26 +02:00
|
|
|
|
|
|
|
void OnTriggerExit2D(Collider2D collision)
|
|
|
|
{
|
2022-06-11 17:49:19 +02:00
|
|
|
///For attacking player
|
2022-06-06 16:21:26 +02:00
|
|
|
inRange = false;
|
2022-06-11 17:49:19 +02:00
|
|
|
timerDmg = 0f;
|
2022-06-12 11:28:14 +02:00
|
|
|
hit = false;
|
2022-06-06 16:21:26 +02:00
|
|
|
}
|
2022-06-09 21:51:48 +02:00
|
|
|
|
|
|
|
public void TakeDamage(float damage)
|
|
|
|
{
|
|
|
|
health = health - damage;
|
|
|
|
if (health <= 0)
|
|
|
|
{
|
2022-06-12 11:28:14 +02:00
|
|
|
gameObject.SetActive(false);
|
2022-06-09 21:51:48 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-11 17:49:19 +02:00
|
|
|
|
|
|
|
public void TakeKnockback()
|
|
|
|
{
|
|
|
|
Rigidbody2D enemy = gameObject.GetComponent<Rigidbody2D>();
|
|
|
|
Rigidbody2D player = other.GetComponent<Rigidbody2D>();
|
|
|
|
if (enemy != null)
|
|
|
|
{
|
|
|
|
enemy.isKinematic = false;
|
|
|
|
Vector2 difference = enemy.transform.position - player.transform.position;
|
|
|
|
difference = difference.normalized * thrust;
|
|
|
|
enemy.AddForce(difference, ForceMode2D.Impulse);
|
|
|
|
StartCoroutine(KnockCo(enemy));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerator KnockCo(Rigidbody2D enemy)
|
|
|
|
{
|
|
|
|
if (enemy != null)
|
|
|
|
{
|
|
|
|
yield return new WaitForSeconds(knockTime);
|
|
|
|
enemy.velocity = Vector2.zero;
|
|
|
|
enemy.isKinematic = true;
|
|
|
|
}
|
|
|
|
}
|
2022-06-15 19:54:44 +02:00
|
|
|
|
|
|
|
public void SaveCheckpoint()
|
|
|
|
{
|
|
|
|
PlayerPrefs.SetInt(name2, isKilled);
|
|
|
|
}
|
2022-05-26 13:30:59 +02:00
|
|
|
}
|