LostNpc.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /// <summary>
  18. /// Start is called on the frame when a script is enabled just before
  19. /// any of the Update methods is called the first time.
  20. /// </summary>
  21. void Start()
  22. {
  23. character.AnimationManager.SetState(CharacterState.Dance);
  24. }
  25. void Update()
  26. {
  27. if (_isQuestActive)
  28. {
  29. FollowPlayer();
  30. }
  31. // if (player != null)
  32. // {
  33. // isPlayerInRange = Vector3.Distance(transform.position, player.transform.position) < followRadius;
  34. // }
  35. // else
  36. // {
  37. // isPlayerInRange = false;
  38. // }
  39. // if (player != null)
  40. // {
  41. // FollowPlayer();
  42. // }
  43. }
  44. // check if the activequest is for finding the lost npc
  45. // if the player is close to the npc - the npc will start following the player
  46. // once the player go back to quest npc - the quest will be completed and npc will stop follow / destroy after few minutes
  47. private void OnTriggerEnter2D(Collider2D other)
  48. {
  49. if (other.CompareTag("Player"))
  50. {
  51. if (other.transform == playerNetwork.localPlayerTransform)
  52. {
  53. player = playerNetwork.localPlayerTransform.GetComponent<playerNetwork>();
  54. //check if the quest match for finding lost npc
  55. if (player.currentQuest == questData[0])
  56. {
  57. _isQuestActive = true;
  58. _isFollowing = true;
  59. npcFinalCollider.isTrigger = true;
  60. }
  61. }
  62. }
  63. }
  64. float endTimer = 5f;
  65. public void FollowPlayer()
  66. {
  67. float distance = Vector2.Distance(transform.position, player.transform.position);
  68. if (distance <= stopDistance || distance >= maxDistance)
  69. {
  70. character.AnimationManager.SetState(CharacterState.Idle);
  71. _isFollowing = false; // Stop following
  72. return;
  73. }
  74. if(player.currentQuest==null){
  75. if(endTimer > 0 ){
  76. endTimer -= Time.deltaTime;
  77. }else{
  78. Destroy(gameObject);
  79. }
  80. character.AnimationManager.SetState(CharacterState.Idle);
  81. _isFollowing = false;
  82. return;
  83. }
  84. // Move
  85. Vector2 direction = (player.transform.position - transform.position).normalized;
  86. transform.position = Vector2.MoveTowards(transform.position, player.transform.position, followSpeed * Time.deltaTime);
  87. //
  88. if (direction.x > 0) character.SetDirection(Vector2.right);
  89. else if (direction.x < 0) character.SetDirection(Vector2.left);
  90. else if (direction.y > 0) character.SetDirection(Vector2.up);
  91. else if (direction.y < 0) character.SetDirection(Vector2.down);
  92. //
  93. character.AnimationManager.SetState(CharacterState.Walk);
  94. }
  95. public void StopFollowing()
  96. {
  97. _isFollowing = false;
  98. character.AnimationManager.SetState(CharacterState.Idle);
  99. }
  100. public BoxCollider2D npcFinalCollider;
  101. public void SetFinalQuestAction()
  102. {
  103. //enable npc is trigger
  104. npcFinalCollider.isTrigger = true;
  105. //StopFollowing();
  106. //destroy npc after few minutes
  107. }
  108. }