2022-06-21 01:09:06 +02:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class FollowingPatrollingEnemy : Enemy
|
|
|
|
{
|
|
|
|
public Transform[] path;
|
|
|
|
public int currentPoint;
|
|
|
|
public Transform currentGoal;
|
|
|
|
|
2022-11-07 22:18:28 +01:00
|
|
|
public AStarPathfindingAgent agent;
|
|
|
|
|
2022-06-21 01:09:06 +02:00
|
|
|
public Transform target;
|
|
|
|
public float chaseRadius;
|
|
|
|
public float attackRadius;
|
|
|
|
public Transform homePosition;
|
|
|
|
|
|
|
|
public float roundingDistance;
|
|
|
|
private Rigidbody2D myRigidbody;
|
|
|
|
public Animator anim;
|
|
|
|
public GameObject other;
|
|
|
|
|
|
|
|
public bool inRange = false;
|
|
|
|
public bool hit = false;
|
|
|
|
public GameObject player;
|
|
|
|
private bool firstAttack = false;
|
|
|
|
|
|
|
|
private float timerDmg = 0f;
|
|
|
|
private float waitTime = 1.0f;
|
|
|
|
|
|
|
|
private float timerHit = 0f;
|
|
|
|
private float hitWaitTime = 0.55f;
|
|
|
|
|
|
|
|
public float thrust;
|
|
|
|
public float knockTime;
|
|
|
|
|
|
|
|
public int isKilled;
|
|
|
|
//isKilled = 0 - mob ALIVE
|
|
|
|
//isKilled = 1 - mob DEAD
|
|
|
|
|
|
|
|
public int isKilled2;
|
|
|
|
|
2022-10-23 00:21:54 +02:00
|
|
|
public float expValue;
|
2022-10-16 21:35:56 +02:00
|
|
|
|
2022-06-21 01:09:06 +02:00
|
|
|
|
2022-11-07 22:18:28 +01:00
|
|
|
void Awake()
|
|
|
|
{
|
|
|
|
//agent = GetComponent<AStarPathfindingAgent>();
|
|
|
|
}
|
|
|
|
|
2022-06-21 01:09:06 +02:00
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
2022-10-02 18:45:58 +02:00
|
|
|
if (OnMapAppearanceMethod.Gateway == OnMapAppearanceMethodEnum.NewGame)
|
2022-06-21 01:09:06 +02:00
|
|
|
{
|
2022-10-02 18:45:58 +02:00
|
|
|
isKilled = 0;
|
2022-06-21 01:09:06 +02:00
|
|
|
}
|
2022-10-02 18:45:58 +02:00
|
|
|
|
|
|
|
if (health <= 0)
|
|
|
|
isKilled = 1;
|
2022-06-21 01:09:06 +02:00
|
|
|
else
|
2022-10-02 18:45:58 +02:00
|
|
|
isKilled = PlayerPrefs.GetInt(enemyName + "-S");
|
|
|
|
|
2022-06-21 01:09:06 +02:00
|
|
|
if (isKilled == 1)
|
|
|
|
{
|
|
|
|
gameObject.SetActive(false);
|
2022-10-02 18:45:58 +02:00
|
|
|
health = 0;
|
2022-06-21 01:09:06 +02:00
|
|
|
}
|
|
|
|
myRigidbody = GetComponent<Rigidbody2D>();
|
|
|
|
anim = GetComponent<Animator>();
|
|
|
|
target = GameObject.FindWithTag("Player").transform;
|
2022-10-01 15:39:24 +02:00
|
|
|
other = GameObject.FindWithTag("Player");
|
2022-06-21 01:09:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
CheckDistance();
|
|
|
|
//StartCoroutine(Timer());
|
|
|
|
|
|
|
|
//Code below is for dealing damage to player
|
|
|
|
|
|
|
|
if (inRange == true)
|
|
|
|
{
|
|
|
|
if (firstAttack == false)
|
|
|
|
{
|
|
|
|
if (timerDmg >= 0.15f)
|
|
|
|
{
|
|
|
|
firstAttack = true;
|
|
|
|
other.GetComponent<Player>().TakeDamage(baseAttack);
|
|
|
|
timerDmg = 0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (timerDmg >= waitTime)
|
|
|
|
{
|
|
|
|
timerDmg = 0f;
|
|
|
|
other.GetComponent<Player>().TakeDamage(baseAttack);
|
|
|
|
}
|
|
|
|
timerDmg += Time.deltaTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
timerHit += Time.deltaTime;
|
|
|
|
if (hit == true)
|
|
|
|
{
|
|
|
|
if (timerHit >= hitWaitTime)
|
|
|
|
{
|
|
|
|
TakeDamage(1.0f);
|
|
|
|
hit = false;
|
|
|
|
timerHit = 0f;
|
2022-11-07 22:18:28 +01:00
|
|
|
//TakeKnockback();
|
2022-06-21 01:09:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gameObject.active == false)
|
|
|
|
{
|
|
|
|
isKilled = 1;
|
|
|
|
PlayerPrefs.SetInt(enemyName, isKilled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CheckDistance()
|
|
|
|
{
|
2022-11-07 22:18:28 +01:00
|
|
|
StopAllCoroutines();
|
2022-06-21 01:09:06 +02:00
|
|
|
if (Vector2.Distance(target.position, transform.position) <= chaseRadius && Vector2.Distance(target.position, transform.position) > attackRadius)
|
|
|
|
{
|
2022-11-07 22:18:28 +01:00
|
|
|
|
|
|
|
//Debug.Log(agent);
|
|
|
|
agent.FindPath();
|
|
|
|
//transform.position = Vector2.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
|
|
|
|
StartCoroutine(agent.FollowPath());
|
2022-06-21 01:09:06 +02:00
|
|
|
}
|
|
|
|
else if (Vector2.Distance(target.position, transform.position) > chaseRadius)
|
|
|
|
{
|
2022-11-07 22:18:28 +01:00
|
|
|
|
2022-09-26 20:30:46 +02:00
|
|
|
//Debug.Log(Vector2.Distance(transform.position, path[currentPoint].position));
|
2022-11-07 22:18:28 +01:00
|
|
|
|
2022-06-21 01:09:06 +02:00
|
|
|
if (Vector2.Distance(transform.position, path[currentPoint].position) > roundingDistance)
|
|
|
|
{
|
2022-11-07 22:18:28 +01:00
|
|
|
StopAllCoroutines();
|
2022-07-22 20:40:41 +02:00
|
|
|
transform.position = Vector2.MoveTowards(transform.position, path[currentPoint].position, moveSpeed * Time.deltaTime);
|
2022-06-21 01:09:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
changeGoal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gameObject.active == false)
|
|
|
|
{
|
|
|
|
isKilled = 1;
|
|
|
|
PlayerPrefs.SetInt(enemyName, isKilled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OnTriggerEnter2D(Collider2D collision)
|
|
|
|
{
|
|
|
|
///For attacking player
|
|
|
|
if (collision.tag == "PlayerHitbox")
|
|
|
|
{
|
|
|
|
inRange = true;
|
|
|
|
firstAttack = false;
|
|
|
|
}
|
|
|
|
|
2022-10-16 21:35:56 +02:00
|
|
|
if (collision.tag == "AttackHitbox" || collision.tag == "PickaxeHitbox")
|
2022-06-21 01:09:06 +02:00
|
|
|
{
|
|
|
|
hit = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnTriggerStay2D(Collider2D collision)
|
|
|
|
{
|
|
|
|
///For attacking player
|
|
|
|
if (collision.tag == "PlayerHitbox")
|
|
|
|
{
|
|
|
|
inRange = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnTriggerExit2D(Collider2D collision)
|
|
|
|
{
|
|
|
|
///For attacking player
|
|
|
|
inRange = false;
|
|
|
|
timerDmg = 0f;
|
|
|
|
hit = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void TakeDamage(float damage)
|
|
|
|
{
|
|
|
|
health = health - damage;
|
|
|
|
if (health <= 0)
|
|
|
|
{
|
|
|
|
gameObject.SetActive(false);
|
2022-10-02 18:45:58 +02:00
|
|
|
isKilled = 1;
|
2022-10-16 21:35:56 +02:00
|
|
|
other.GetComponent<Player>().GetExp(expValue);
|
2022-06-21 01:09:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void TakeKnockback()
|
|
|
|
{
|
|
|
|
Rigidbody2D enemy = gameObject.GetComponent<Rigidbody2D>();
|
|
|
|
Rigidbody2D player = other.GetComponent<Rigidbody2D>();
|
2022-10-02 18:45:58 +02:00
|
|
|
if (enemy != null && gameObject.active)
|
2022-06-21 01:09:06 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SaveCheckpoint()
|
|
|
|
{
|
2022-10-02 18:45:58 +02:00
|
|
|
PlayerPrefs.SetInt(enemyName + "-S", isKilled);
|
2022-06-21 01:09:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void changeGoal()
|
|
|
|
{
|
|
|
|
if (currentPoint == path.Length - 1)
|
|
|
|
{
|
|
|
|
currentPoint = 0;
|
|
|
|
currentGoal = path[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
currentPoint++;
|
|
|
|
currentGoal = path[currentPoint];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|