Scriptum/Assets/Scripts/REFACTORING/Story/Dungeon/Wizard/EscapingWizard.cs

95 lines
2.2 KiB
C#
Raw Normal View History

2023-01-08 14:08:12 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
[RequireComponent(typeof(NPC))]
class EscapingWizard : MonoBehaviour
{
private Rigidbody2D myRigidbody;
public Animator anim;
private Vector2 movement;
public bool shouldRotate;
public Vector3 dir;
[Header("Following Logic")]
public Transform targetPosition;
public AStarPathfindingAgent agent;
public bool isDuringEscaping = false; // var is setted by trigger range
public float escapingRadius = 6f; // radious where Wizard start escaping if player is in
public void Awake()
{
agent = GetComponent<AStarPathfindingAgent>();
}
private void Start()
{
}
private void Update()
{
if(targetPosition == null)
{
targetPosition = GameObject.FindGameObjectWithTag("SceneTransition")?.transform;
}
//StopAllCoroutines();
if (IsInEscapingRadious())
{
gameObject.GetComponent<NPC>().State = NPCStateEnum.Walking;
// Animation config
dir = targetPosition.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);
}
agent.point = targetPosition.position;
agent.FindPoint();
StartCoroutine(agent.FollowPath());
} else
{
anim.SetBool("isRunning", false);
gameObject.GetComponent<NPC>().State = NPCStateEnum.Pending;
}
}
public bool IsInEscapingRadious()
{
if (Vector2.Distance(GameObject.FindGameObjectWithTag("Player").transform.position, transform.position) >= escapingRadius)
return false;
return true;
}
public void OnCollisionEnter2D(Collision2D collision)
{
if(collision.collider.tag == "SceneTransition")
{
Destroy(gameObject);
}
}
}