LostNpc.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Assets.HeroEditor4D.Common.Scripts.CharacterScripts;
  4. using Assets.HeroEditor4D.Common.Scripts.Enums;
  5. using UnityEngine;
  6. public class LostNpc : MonoBehaviour
  7. {
  8. //check if the quest is active on local player
  9. public Character4D character;
  10. [SerializeField] private bool _isQuestActive;
  11. public QuestScriptable[] questData;
  12. private playerNetwork player;
  13. [SerializeField] private float followSpeed = 2f;
  14. [SerializeField] private float stopDistance = 1f;
  15. [SerializeField] private float maxDistance = 15f;
  16. public bool _isFollowing;
  17. void Update()
  18. {
  19. if (_isQuestActive)
  20. {
  21. FollowPlayer();
  22. }
  23. // if (player != null)
  24. // {
  25. // isPlayerInRange = Vector3.Distance(transform.position, player.transform.position) < followRadius;
  26. // }
  27. // else
  28. // {
  29. // isPlayerInRange = false;
  30. // }
  31. // if (player != null)
  32. // {
  33. // FollowPlayer();
  34. // }
  35. }
  36. // check if the activequest is for finding the lost npc
  37. // if the player is close to the npc - the npc will start following the player
  38. // once the player go back to quest npc - the quest will be completed and npc will stop follow / destroy after few minutes
  39. private void OnTriggerEnter2D(Collider2D other)
  40. {
  41. if (other.CompareTag("Player"))
  42. {
  43. if (other.transform == playerNetwork.localPlayerTransform)
  44. {
  45. player = playerNetwork.localPlayerTransform.GetComponent<playerNetwork>();
  46. //check if the quest match for finding lost npc
  47. if (player.currentQuest == questData[0])
  48. {
  49. _isQuestActive = true;
  50. _isFollowing = true;
  51. npcFinalCollider.isTrigger = true;
  52. }
  53. }
  54. }
  55. }
  56. float endTimer = 5f;
  57. public void FollowPlayer()
  58. {
  59. float distance = Vector2.Distance(transform.position, player.transform.position);
  60. if (distance <= stopDistance || distance >= maxDistance)
  61. {
  62. character.AnimationManager.SetState(CharacterState.Idle);
  63. _isFollowing = false; // Stop following
  64. return;
  65. }
  66. if(player.currentQuest==null){
  67. if(endTimer > 0 ){
  68. endTimer -= Time.deltaTime;
  69. }else{
  70. Destroy(gameObject);
  71. }
  72. character.AnimationManager.SetState(CharacterState.Idle);
  73. _isFollowing = false;
  74. return;
  75. }
  76. // Move
  77. Vector2 direction = (player.transform.position - transform.position).normalized;
  78. transform.position = Vector2.MoveTowards(transform.position, player.transform.position, followSpeed * Time.deltaTime);
  79. //
  80. if (direction.x > 0) character.SetDirection(Vector2.right);
  81. else if (direction.x < 0) character.SetDirection(Vector2.left);
  82. else if (direction.y > 0) character.SetDirection(Vector2.up);
  83. else if (direction.y < 0) character.SetDirection(Vector2.down);
  84. //
  85. character.AnimationManager.SetState(CharacterState.Walk);
  86. }
  87. public void StopFollowing()
  88. {
  89. _isFollowing = false;
  90. character.AnimationManager.SetState(CharacterState.Idle);
  91. }
  92. public BoxCollider2D npcFinalCollider;
  93. public void SetFinalQuestAction()
  94. {
  95. //enable npc is trigger
  96. npcFinalCollider.isTrigger = true;
  97. //StopFollowing();
  98. //destroy npc after few minutes
  99. }
  100. }