QuestAction.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. foreach(QuestCompleteResourceCheckEntry entry in resourceCheckEntries) //Check if has all resources
  18. {
  19. int stockCount = playerNetwork.localPlayerTransform.GetComponent<Inventory>().inventoryManager.GetStock(entry.resource.type);
  20. if(stockCount < entry.amount)
  21. {
  22. Debug.Log($"Not enough {entry.resource.type}, need {entry.amount}, found {stockCount}");
  23. return;
  24. }
  25. }
  26. foreach(QuestCompleteResourceCheckEntry entry in resourceCheckEntries){ //Remove resources
  27. if(playerNetwork.localPlayerTransform.GetComponent<Inventory>().inventoryManager.GetStock(entry.resource.type) < entry.amount){
  28. }else{
  29. for(int i=0; i < entry.amount; i++)
  30. {
  31. playerNetwork.localPlayerTransform.GetComponent<Inventory>().inventoryManager.RemoveItem(entry.resource.type);
  32. }
  33. }
  34. }
  35. OnComplete.Invoke();
  36. Debug.Log("QuestAction: QuestAction completed");
  37. if (isFinalAction)
  38. {
  39. playerNetwork.localPlayerTransform.GetComponent<playerNetwork>().CompleteQuest(questData);
  40. }
  41. gameObject.SetActive(false);
  42. }
  43. }
  44. public void activate()
  45. {
  46. gameObject.SetActive(true);
  47. }
  48. void Update()
  49. {
  50. if (playerNetwork.localPlayerTransform != null && !isRegistered)
  51. {
  52. Register();
  53. }
  54. }
  55. void Register()
  56. {
  57. playerNetwork.registerQuestAction(this);
  58. isRegistered = true;
  59. gameObject.SetActive(false);
  60. }
  61. }
  62. [Serializable ]
  63. public class QuestCompleteResourceCheckEntry
  64. {
  65. public item resource;
  66. public int amount;
  67. }