1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Events;
- public class QuestAction : MonoBehaviour
- {
- public QuestScriptable questData;
- bool isRegistered = false;
- public UnityEvent OnComplete;
- public bool isFinalAction = true;
- public List<QuestCompleteResourceCheckEntry> resourceCheckEntries;
- private void OnTriggerEnter2D(Collider2D other)
- {
- if (other.CompareTag("Player") && other.transform == playerNetwork.localPlayerTransform)
- {
- foreach(QuestCompleteResourceCheckEntry entry in resourceCheckEntries) //Check if has all resources
- {
- int stockCount = playerNetwork.localPlayerTransform.GetComponent<Inventory>().inventoryManager.GetStock(entry.resource.type);
- if(stockCount < entry.amount)
- {
- Debug.Log($"Not enough {entry.resource.type}, need {entry.amount}, found {stockCount}");
- return;
- }
- }
-
- foreach(QuestCompleteResourceCheckEntry entry in resourceCheckEntries){ //Remove resources
- if(playerNetwork.localPlayerTransform.GetComponent<Inventory>().inventoryManager.GetStock(entry.resource.type) < entry.amount){
- }else{
- for(int i=0; i < entry.amount; i++)
- {
- playerNetwork.localPlayerTransform.GetComponent<Inventory>().inventoryManager.RemoveItem(entry.resource.type);
- }
- }
- }
- OnComplete.Invoke();
- Debug.Log("QuestAction: QuestAction completed");
- if (isFinalAction)
- {
-
- playerNetwork.localPlayerTransform.GetComponent<playerNetwork>().CompleteQuest(questData);
- }
- gameObject.SetActive(false);
- }
- }
- public void activate()
- {
- gameObject.SetActive(true);
- }
- void Update()
- {
- if (playerNetwork.localPlayerTransform != null && !isRegistered)
- {
- Register();
- }
- }
- void Register()
- {
- playerNetwork.registerQuestAction(this);
- isRegistered = true;
- gameObject.SetActive(false);
- }
- }
- [Serializable ]
- public class QuestCompleteResourceCheckEntry
- {
- public item resource;
- public int amount;
- }
|