Advertisement
TinyPurpleCat

another example

Feb 28th, 2024 (edited)
1,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Networking;
  6. using UnityEngine.SceneManagement;
  7. using System.Text.RegularExpressions;
  8. using System.Text;
  9. using System;
  10. using System.IO;
  11. using System.Threading;
  12. using Unity.VisualScripting;
  13. using Palmmedia.ReportGenerator.Core;
  14.  
  15.  
  16. public class ResourceManager : MonoBehaviour
  17. {
  18.  
  19.     private bool isBeingGathered = false;
  20.     public int resNodeId;
  21.     public int resAmount;
  22.     public float gatherTime = 5f; // made is so there is a defult value.
  23.     public int resId;
  24.     public string resType;
  25.     public int itemId;
  26.  
  27.     private float LocalTime = 0f;
  28.     private float CurrentWaitTime = 0f;
  29.  
  30.     // storing the player for later use.
  31.     private Transform player;
  32.  
  33.     public void Start()
  34.     {
  35.  
  36.         StartCoroutine(GetResNode(resNodeId));
  37.     }
  38.  
  39.     public void StartGathering(Transform player)
  40.     {
  41.  
  42.         if (!isBeingGathered)
  43.         {
  44.             isBeingGathered = true;
  45.             //GatherResource(player);
  46.             this.player = player
  47.         }
  48.     }
  49.  
  50.     public void StopGathering()
  51.     {
  52.  
  53.         if (!isBeingGathered)
  54.         {
  55.             isBeingGathered = false;
  56.             //GatherResource(player);
  57.         }
  58.     }
  59.  
  60.     void Update()
  61.     {
  62.         // this will tick down when the plauer is gather
  63.         LocalTime += Time.deltaTime;
  64.  
  65.         if (isBeingGathered) GatherResource(player);
  66.     }
  67.  
  68.  
  69.  
  70.     //Gathering Resources
  71.     private void GatherResource(Transform player)
  72.     {
  73.         PlayerController playCont = player.GetComponent<PlayerController>();
  74.         if (resAmount > 0 && playCont.inResArea)
  75.         {
  76.  
  77.             // guard if statement. reduces nesting.
  78.             if (LocalTime < CurrentWaitTime) return;
  79.            
  80.             // delay the code
  81.             CurrentWaitTime = LocalTime + gatherTime;
  82.  
  83.             Debug.Log("Gathering...");
  84.             // Play the gathering animation
  85.  
  86.             resAmount--;
  87.             playCont.playerInventory++;
  88.             playCont.AddItem(itemId);
  89.  
  90.             if (resAmount <= 0)
  91.             {
  92.                 // Resource depleted
  93.                 Destroy(gameObject);
  94.                 Debug.Log("Gathering Finished");
  95.                 isBeingGathered = false;
  96.                 return;
  97.             }
  98.             else if (playCont.playerInventory == playCont.carryAmount)
  99.             {
  100.                 Debug.Log("Inventory Full");
  101.                 isBeingGathered = false;
  102.                 return;
  103.             }
  104.         }
  105.     }
  106.  
  107.     IEnumerator GetResNode(int resNodeId)
  108.     {
  109.         using (UnityWebRequest www = UnityWebRequest.Get($"http://localhost:8002/resource_node/get-resource_node-by-id?resource_node_id={resNodeId}"))
  110.         {
  111.             www.SetRequestHeader("key", "1");
  112.             yield return www.SendWebRequest();
  113.  
  114.             if (www.result != UnityWebRequest.Result.Success)
  115.             {
  116.                 Debug.Log(www.error);
  117.             }
  118.             else
  119.             {
  120.                 ResourceNodes resNodes = new ResourceNodes();
  121.                 string dH = www.downloadHandler.text;
  122.                 Debug.Log(dH);
  123.                 resNodes = JsonUtility.FromJson<ResourceNodes>(dH);
  124.                 resAmount = resNodes.data[0].resource_amount;
  125.                 gatherTime = resNodes.data[0].gathering_time;
  126.                 resId = resNodes.data[0].resource_id;
  127.                 StartCoroutine(GetResourceData(resId));
  128.             }
  129.         }
  130.     }
  131.     IEnumerator GetResourceData(int resId)
  132.     {
  133.         using (UnityWebRequest www = UnityWebRequest.Get($"http://localhost:8002/resource/get-resource-by-id?resource_id={resId}"))
  134.         {
  135.             www.SetRequestHeader("key", "1");
  136.             yield return www.SendWebRequest();
  137.  
  138.             if (www.result != UnityWebRequest.Result.Success)
  139.             {
  140.                 Debug.Log(www.error);
  141.             }
  142.             else
  143.             {
  144.                 string dH = www.downloadHandler.text;
  145.                 Resource res = new Resource();
  146.                 res = JsonUtility.FromJson<Resource>(dH);
  147.                 resType = res.data[0].resource_type;
  148.                 StartCoroutine(GetItemData(resType));
  149.             }
  150.         }
  151.     }
  152.     IEnumerator GetItemData(string resType)
  153.     {
  154.         using (UnityWebRequest www = UnityWebRequest.Get($"http://localhost:8002/item/get-item-by-type?item_type={resType}"))
  155.         {
  156.             www.SetRequestHeader("key", "1");
  157.             yield return www.SendWebRequest();
  158.  
  159.             if (www.result != UnityWebRequest.Result.Success)
  160.             {
  161.                 Debug.Log(www.error);
  162.             }
  163.             else
  164.             {
  165.                 string dH = www.downloadHandler.text;
  166.                 Inventory inv = new Inventory();
  167.                 inv = JsonUtility.FromJson<Inventory>(dH);
  168.                 itemId = inv.data[0].item_id;
  169.             }
  170.         }
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement