using System.Collections;
using System.Collections.Generic;
using Assets.HeroEditor4D.Common.Scripts.CharacterScripts;
using Assets.HeroEditor4D.Common.Scripts.Enums;
using UnityEngine;
public class LostNpc : MonoBehaviour
{
//check if the quest is active on local player
public Character4D character;
[SerializeField] private bool _isQuestActive;
public QuestScriptable[] questData;
private playerNetwork player;
[SerializeField] private float followSpeed = 2f;
[SerializeField] private float stopDistance = 1f;
[SerializeField] private float maxDistance = 15f;
public bool _isFollowing;
///
/// Start is called on the frame when a script is enabled just before
/// any of the Update methods is called the first time.
///
void Start()
{
character.AnimationManager.SetState(CharacterState.Dance);
}
void Update()
{
if (_isQuestActive)
{
FollowPlayer();
}
// if (player != null)
// {
// isPlayerInRange = Vector3.Distance(transform.position, player.transform.position) < followRadius;
// }
// else
// {
// isPlayerInRange = false;
// }
// if (player != null)
// {
// FollowPlayer();
// }
}
// check if the activequest is for finding the lost npc
// if the player is close to the npc - the npc will start following the player
// once the player go back to quest npc - the quest will be completed and npc will stop follow / destroy after few minutes
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
if (other.transform == playerNetwork.localPlayerTransform)
{
player = playerNetwork.localPlayerTransform.GetComponent();
//check if the quest match for finding lost npc
if (player.currentQuest == questData[0])
{
_isQuestActive = true;
_isFollowing = true;
npcFinalCollider.isTrigger = true;
}
}
}
}
float endTimer = 5f;
public void FollowPlayer()
{
float distance = Vector2.Distance(transform.position, player.transform.position);
if (distance <= stopDistance || distance >= maxDistance)
{
character.AnimationManager.SetState(CharacterState.Idle);
_isFollowing = false; // Stop following
return;
}
if(player.currentQuest==null){
if(endTimer > 0 ){
endTimer -= Time.deltaTime;
}else{
Destroy(gameObject);
}
character.AnimationManager.SetState(CharacterState.Idle);
_isFollowing = false;
return;
}
// Move
Vector2 direction = (player.transform.position - transform.position).normalized;
transform.position = Vector2.MoveTowards(transform.position, player.transform.position, followSpeed * Time.deltaTime);
//
if (direction.x > 0) character.SetDirection(Vector2.right);
else if (direction.x < 0) character.SetDirection(Vector2.left);
else if (direction.y > 0) character.SetDirection(Vector2.up);
else if (direction.y < 0) character.SetDirection(Vector2.down);
//
character.AnimationManager.SetState(CharacterState.Walk);
}
public void StopFollowing()
{
_isFollowing = false;
character.AnimationManager.SetState(CharacterState.Idle);
}
public BoxCollider2D npcFinalCollider;
public void SetFinalQuestAction()
{
//enable npc is trigger
npcFinalCollider.isTrigger = true;
//StopFollowing();
//destroy npc after few minutes
}
}