QuestAction.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. public class QuestAction : MonoBehaviour
  7. {
  8. public QuestScriptable questData;
  9. bool isRegistered = false;
  10. public UnityEvent OnComplete;
  11. public bool isFinalAction = true;
  12. public List<QuestCompleteResourceCheckEntry> resourceCheckEntries;
  13. private void OnTriggerEnter2D(Collider2D other)
  14. {
  15. if (other.CompareTag("Player") && other.transform == playerNetwork.localPlayerTransform)
  16. {
  17. OnComplete.Invoke();
  18. Debug.Log("QuestAction: QuestAction completed");
  19. if (isFinalAction)
  20. {
  21. foreach(QuestCompleteResourceCheckEntry entry in resourceCheckEntries)
  22. {
  23. if(playerNetwork.localPlayerTransform.GetComponent<Inventory>().GetStock(entry.resourceName) < entry.amount)
  24. {
  25. Debug.Log("QuestAction: Resource check failed");
  26. return;
  27. }else{
  28. for(int i=0; i < entry.amount; i++)
  29. {
  30. playerNetwork.localPlayerTransform.GetComponent<Inventory>().RemoveItem(entry.resourceName);
  31. }
  32. }
  33. }
  34. playerNetwork.localPlayerTransform.GetComponent<playerNetwork>().CompleteQuest(questData);
  35. }
  36. gameObject.SetActive(false);
  37. }
  38. }
  39. public void activate()
  40. {
  41. gameObject.SetActive(true);
  42. }
  43. void Update()
  44. {
  45. if (playerNetwork.localPlayerTransform != null && !isRegistered)
  46. {
  47. Register();
  48. }
  49. }
  50. void Register()
  51. {
  52. playerNetwork.registerQuestAction(this);
  53. isRegistered = true;
  54. gameObject.SetActive(false);
  55. }
  56. }
  57. [Serializable ]
  58. public class QuestCompleteResourceCheckEntry
  59. {
  60. public string resourceName;
  61. public int amount;
  62. }