Advertisement
evelynshilosky

Campfire - Part 35

Mar 19th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.31 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class Campfire : MonoBehaviour
  7. {
  8.     public bool playerInRange;
  9.  
  10.     public bool isCooking;
  11.     public float cookingTimer;
  12.  
  13.     public CookableFood foodBeingCooked;
  14.     public string readyFood;
  15.  
  16.     public GameObject fire;
  17.  
  18.  
  19.     private void Update()
  20.     {
  21.         float distance = Vector3.Distance(PlayerState.Instance.playerBody.transform.position, transform.position);
  22.  
  23.         if (distance < 10f)
  24.         {
  25.             playerInRange = true;
  26.         }
  27.         else
  28.         {
  29.             playerInRange = false;
  30.         }
  31.  
  32.         if (isCooking)
  33.         {
  34.             cookingTimer -= Time.deltaTime;
  35.             fire.SetActive(true);
  36.         }
  37.         else
  38.         {
  39.             fire.SetActive(false);
  40.         }
  41.  
  42.         if (cookingTimer <= 0 && isCooking)
  43.         {
  44.             isCooking = false;
  45.             readyFood = GetCookedFood(foodBeingCooked);
  46.         }
  47.  
  48.     }
  49.  
  50.     private string GetCookedFood(CookableFood food)
  51.     {
  52.         return food.cookedFoodName;
  53.     }
  54.  
  55.     public void OpenUI()
  56.     {
  57.         CampfireUIManager.Instance.OpenUI();
  58.         CampfireUIManager.Instance.selectedCampfire = this;
  59.  
  60.         if (readyFood != "")
  61.         {
  62.             GameObject rf = Instantiate(Resources.Load<GameObject>(readyFood),
  63.                 CampfireUIManager.Instance.foodSlot.transform.position,
  64.                 CampfireUIManager.Instance.foodSlot.transform.rotation);
  65.  
  66.             rf.transform.SetParent(CampfireUIManager.Instance.foodSlot.transform);
  67.  
  68.             readyFood = "";
  69.         }
  70.     }
  71.  
  72.     public void StartCooking(InventoryItem food)
  73.     {
  74.         foodBeingCooked = ConvertIntoCookable(food);
  75.  
  76.         isCooking = true;
  77.  
  78.         cookingTimer = TimeToCookFood(foodBeingCooked);
  79.     }
  80.  
  81.     private CookableFood ConvertIntoCookable(InventoryItem food)
  82.     {
  83.         foreach (CookableFood cookable in CampfireUIManager.Instance.cookingData.validFoods)
  84.         {
  85.             if (cookable.name == food.thisName)
  86.             {
  87.                 return cookable;
  88.             }
  89.         }
  90.  
  91.         return new CookableFood(); // This will never happen
  92.     }
  93.  
  94.     private float TimeToCookFood(CookableFood food)
  95.     {
  96.         return food.timeToCook;
  97.     }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement