2022-10-24 12:31:09 +02:00
|
|
|
using System;
|
2022-05-26 13:30:59 +02:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
2022-10-24 12:31:09 +02:00
|
|
|
using UnityEngine.PlayerLoop;
|
2022-05-26 13:30:59 +02:00
|
|
|
|
|
|
|
public class FollowingEnemy : Enemy
|
|
|
|
{
|
|
|
|
public Transform target;
|
|
|
|
public float chaseRadius;
|
|
|
|
public float attackRadius;
|
|
|
|
public Transform homePosition;
|
2022-10-24 12:31:09 +02:00
|
|
|
private Vector2 movement;
|
|
|
|
public float speed;
|
|
|
|
public bool shouldRotate;
|
|
|
|
public Vector3 dir;
|
|
|
|
public LayerMask whatisplayer;
|
2022-05-26 13:30:59 +02:00
|
|
|
|
|
|
|
public float roundingDistance;
|
2022-10-24 12:31:09 +02:00
|
|
|
private Rigidbody2D rb;
|
2022-05-26 13:30:59 +02:00
|
|
|
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 isKilled2;
|
|
|
|
|
2022-10-02 18:45:58 +02:00
|
|
|
public bool isPanelEnabled=true;
|
|
|
|
|
2022-10-23 00:21:54 +02:00
|
|
|
public float expValue;
|
2022-10-16 21:35:56 +02:00
|
|
|
|
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-10-24 12:31:09 +02:00
|
|
|
//used for animation of sprites
|
|
|
|
rb = GetComponent<Rigidbody2D>();
|
|
|
|
anim = GetComponent<Animator>();
|
|
|
|
|
2022-10-02 18:45:58 +02:00
|
|
|
// if new game set as alive
|
2022-12-06 01:20:01 +01:00
|
|
|
if (OnMapAppearanceMethod.IsNewGame())
|
2022-06-15 19:54:44 +02:00
|
|
|
{
|
2022-10-02 18:45:58 +02:00
|
|
|
isKilled = 0;
|
2022-06-15 19:54:44 +02:00
|
|
|
}
|
2022-10-02 18:45:58 +02:00
|
|
|
|
|
|
|
// But if something pre-set healt as less than 0 then change status to "killed"
|
|
|
|
if (health <= 0)
|
|
|
|
isKilled = 1;
|
|
|
|
else // Else read saved value
|
|
|
|
isKilled = PlayerPrefs.GetInt(enemyName + "-S");
|
|
|
|
|
|
|
|
|
2022-06-15 16:32:14 +02:00
|
|
|
if (isKilled == 1)
|
|
|
|
{
|
|
|
|
gameObject.SetActive(false);
|
2022-10-02 18:45:58 +02:00
|
|
|
health = 0;
|
2022-06-15 16:32:14 +02:00
|
|
|
}
|
2022-10-24 12:31:09 +02:00
|
|
|
//rb = GetComponent<Rigidbody2D>();
|
|
|
|
//anim = GetComponent<Animator>();
|
2022-10-01 15:39:24 +02:00
|
|
|
|
2022-05-26 13:30:59 +02:00
|
|
|
target = GameObject.FindWithTag("Player").transform;
|
2022-10-01 15:39:24 +02:00
|
|
|
other = GameObject.FindWithTag("Player");
|
2022-05-26 13:30:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
2022-10-24 12:31:09 +02:00
|
|
|
//animation of Sprites
|
|
|
|
if (Vector2.Distance(target.position, transform.position) <= chaseRadius)
|
|
|
|
{
|
|
|
|
dir = target.position - transform.position;
|
|
|
|
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
|
|
|
|
dir.Normalize();
|
|
|
|
movement = dir;
|
|
|
|
anim.SetBool("isRunning", movement != Vector2.zero);
|
|
|
|
if (shouldRotate)
|
|
|
|
{
|
|
|
|
anim.SetFloat("Xinfo", dir.x);
|
|
|
|
anim.SetFloat("Yinfo", dir.y);
|
|
|
|
}
|
|
|
|
}
|
2022-11-07 22:18:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
if (CheckDistance())
|
|
|
|
{
|
|
|
|
MoveCharacter();
|
|
|
|
}
|
|
|
|
//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;
|
2022-10-02 18:45:58 +02:00
|
|
|
other.GetComponent<Player>().TakeDamage(baseAttack, isPanelEnabled);
|
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-10-02 18:45:58 +02:00
|
|
|
other.GetComponent<Player>().TakeDamage(baseAttack, isPanelEnabled);
|
2022-06-06 16:21:26 +02:00
|
|
|
}
|
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-11-07 22:18:28 +01:00
|
|
|
|
2022-10-24 12:31:09 +02:00
|
|
|
private void FixedUpdate()
|
|
|
|
{
|
2022-11-07 22:18:28 +01:00
|
|
|
/*if (Physics2D.OverlapCircle(transform.position, chaseRadius, whatisplayer))
|
2022-10-24 12:31:09 +02:00
|
|
|
{
|
2022-11-07 22:18:28 +01:00
|
|
|
MoveCharacter();
|
2022-10-24 12:31:09 +02:00
|
|
|
}
|
|
|
|
if (Physics2D.OverlapCircle(transform.position, attackRadius, whatisplayer))
|
|
|
|
{
|
|
|
|
rb.velocity = Vector2.zero;
|
2022-11-07 22:18:28 +01:00
|
|
|
}*/
|
2022-10-24 12:31:09 +02:00
|
|
|
}
|
|
|
|
|
2022-11-07 22:18:28 +01:00
|
|
|
public AStarPathfindingAgent agent;
|
|
|
|
private void MoveCharacter()
|
2022-10-24 12:31:09 +02:00
|
|
|
{
|
2022-11-07 22:18:28 +01:00
|
|
|
StopAllCoroutines();
|
|
|
|
//rb.MovePosition((Vector2)transform.position + (dir * moveSpeed * Time.deltaTime));
|
|
|
|
agent.FindPath();
|
|
|
|
StartCoroutine(agent.FollowPath());
|
2022-10-24 12:31:09 +02:00
|
|
|
}
|
2022-11-07 22:18:28 +01:00
|
|
|
bool CheckDistance()
|
2022-05-26 13:30:59 +02:00
|
|
|
{
|
2022-06-21 01:09:06 +02:00
|
|
|
if(Vector2.Distance(target.position, transform.position) <= chaseRadius && Vector2.Distance(target.position, transform.position) > attackRadius)
|
2022-05-26 13:30:59 +02:00
|
|
|
{
|
2022-11-07 22:18:28 +01:00
|
|
|
//transform.position = Vector2.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
|
|
|
|
return true;
|
2022-05-26 13:30:59 +02:00
|
|
|
}
|
2022-11-07 22:18:28 +01:00
|
|
|
return false;
|
2022-05-26 13:30:59 +02:00
|
|
|
}
|
2022-06-04 13:59:18 +02:00
|
|
|
|
2022-06-06 16:21:26 +02:00
|
|
|
|
2022-11-07 22:18:28 +01:00
|
|
|
private void OnDrawGizmos()
|
|
|
|
{
|
|
|
|
Gizmos.color = Color.magenta;
|
|
|
|
Gizmos.DrawWireSphere(transform.position,chaseRadius);
|
|
|
|
}
|
|
|
|
|
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-10-02 18:45:58 +02:00
|
|
|
isKilled = 1;
|
2022-10-16 21:35:56 +02:00
|
|
|
other.GetComponent<Player>().GetExp(expValue);
|
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);
|
2022-11-10 16:00:57 +01:00
|
|
|
//StartCoroutine(KnockCo(enemy));
|
2022-06-11 17:49:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2022-10-02 18:45:58 +02:00
|
|
|
PlayerPrefs.SetInt(enemyName + "-S", isKilled);
|
|
|
|
PlayerPrefs.SetFloat(enemyName + "-S.health", health);
|
2022-06-15 19:54:44 +02:00
|
|
|
}
|
2022-05-26 13:30:59 +02:00
|
|
|
}
|