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

111 lines
2.6 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
{
2023-01-13 14:11:01 +01:00
private HardLight2D wizardlight;
private HardLight2D[] lights;
2023-01-08 14:08:12 +01:00
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
2023-01-13 14:11:01 +01:00
2023-01-08 14:08:12 +01:00
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);
}
2023-01-13 14:11:01 +01:00
2023-01-08 14:08:12 +01:00
agent.point = targetPosition.position;
2023-01-13 14:11:01 +01:00
StopAllCoroutines();
//Debug.Log("FOLLOWINGPATH STARTED");
2023-01-08 14:08:12 +01:00
agent.FindPoint();
2023-01-13 14:11:01 +01:00
//agent.FindPath();
2023-01-08 14:08:12 +01:00
StartCoroutine(agent.FollowPath());
2023-01-13 14:11:01 +01:00
2023-01-08 14:08:12 +01:00
} else
{
2023-01-14 14:39:29 +01:00
StopAllCoroutines();
2023-01-08 14:08:12 +01:00
gameObject.GetComponent<NPC>().State = NPCStateEnum.Pending;
2023-01-14 14:39:29 +01:00
anim.SetBool("isRunning", false);
2023-01-08 14:08:12 +01:00
}
}
2023-01-13 14:11:01 +01:00
private void OnDrawGizmos()
{
Gizmos.color = Color.magenta;
Gizmos.DrawWireSphere(transform.position,escapingRadius);
}
2023-01-08 14:08:12 +01:00
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")
{
2023-01-13 14:11:01 +01:00
DungeonManager.SetTeleported();
2023-01-08 14:08:12 +01:00
Destroy(gameObject);
}
}
}