Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Networking;
- using UnityEngine.SceneManagement;
- using System.Text.RegularExpressions;
- using System.Text;
- using System;
- using System.IO;
- using System.Threading;
- using Unity.VisualScripting;
- using Palmmedia.ReportGenerator.Core;
- public class ResourceManager : MonoBehaviour
- {
- private bool isBeingGathered = false;
- public int resNodeId;
- public int resAmount;
- public float gatherTime = 5f; // made is so there is a defult value.
- public int resId;
- public string resType;
- public int itemId;
- private float LocalTime = 0f;
- private float CurrentWaitTime = 0f;
- // storing the player for later use.
- private Transform player;
- public void Start()
- {
- StartCoroutine(GetResNode(resNodeId));
- }
- public void StartGathering(Transform player)
- {
- if (!isBeingGathered)
- {
- isBeingGathered = true;
- //GatherResource(player);
- this.player = player
- }
- }
- public void StopGathering()
- {
- if (!isBeingGathered)
- {
- isBeingGathered = false;
- //GatherResource(player);
- }
- }
- void Update()
- {
- // this will tick down when the plauer is gather
- LocalTime += Time.deltaTime;
- if (isBeingGathered) GatherResource(player);
- }
- //Gathering Resources
- private void GatherResource(Transform player)
- {
- PlayerController playCont = player.GetComponent<PlayerController>();
- if (resAmount > 0 && playCont.inResArea)
- {
- // guard if statement. reduces nesting.
- if (LocalTime < CurrentWaitTime) return;
- // delay the code
- CurrentWaitTime = LocalTime + gatherTime;
- Debug.Log("Gathering...");
- // Play the gathering animation
- resAmount--;
- playCont.playerInventory++;
- playCont.AddItem(itemId);
- if (resAmount <= 0)
- {
- // Resource depleted
- Destroy(gameObject);
- Debug.Log("Gathering Finished");
- isBeingGathered = false;
- return;
- }
- else if (playCont.playerInventory == playCont.carryAmount)
- {
- Debug.Log("Inventory Full");
- isBeingGathered = false;
- return;
- }
- }
- }
- IEnumerator GetResNode(int resNodeId)
- {
- using (UnityWebRequest www = UnityWebRequest.Get($"http://localhost:8002/resource_node/get-resource_node-by-id?resource_node_id={resNodeId}"))
- {
- www.SetRequestHeader("key", "1");
- yield return www.SendWebRequest();
- if (www.result != UnityWebRequest.Result.Success)
- {
- Debug.Log(www.error);
- }
- else
- {
- ResourceNodes resNodes = new ResourceNodes();
- string dH = www.downloadHandler.text;
- Debug.Log(dH);
- resNodes = JsonUtility.FromJson<ResourceNodes>(dH);
- resAmount = resNodes.data[0].resource_amount;
- gatherTime = resNodes.data[0].gathering_time;
- resId = resNodes.data[0].resource_id;
- StartCoroutine(GetResourceData(resId));
- }
- }
- }
- IEnumerator GetResourceData(int resId)
- {
- using (UnityWebRequest www = UnityWebRequest.Get($"http://localhost:8002/resource/get-resource-by-id?resource_id={resId}"))
- {
- www.SetRequestHeader("key", "1");
- yield return www.SendWebRequest();
- if (www.result != UnityWebRequest.Result.Success)
- {
- Debug.Log(www.error);
- }
- else
- {
- string dH = www.downloadHandler.text;
- Resource res = new Resource();
- res = JsonUtility.FromJson<Resource>(dH);
- resType = res.data[0].resource_type;
- StartCoroutine(GetItemData(resType));
- }
- }
- }
- IEnumerator GetItemData(string resType)
- {
- using (UnityWebRequest www = UnityWebRequest.Get($"http://localhost:8002/item/get-item-by-type?item_type={resType}"))
- {
- www.SetRequestHeader("key", "1");
- yield return www.SendWebRequest();
- if (www.result != UnityWebRequest.Result.Success)
- {
- Debug.Log(www.error);
- }
- else
- {
- string dH = www.downloadHandler.text;
- Inventory inv = new Inventory();
- inv = JsonUtility.FromJson<Inventory>(dH);
- itemId = inv.data[0].item_id;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement