Advertisement
evelynshilosky

PlacementSystem - Part 33

Mar 7th, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class PlacementSystem : MonoBehaviour
  7. {
  8.     public static PlacementSystem Instance { get; set; }
  9.  
  10.     public GameObject placementHoldingSpot; // Drag our construcionHoldingSpot or a new placementHoldingSpot
  11.     public GameObject enviromentPlaceables;
  12.  
  13.  
  14.     public bool inPlacementMode;
  15.     [SerializeField] bool isValidPlacement;
  16.  
  17.     [SerializeField] GameObject itemToBePlaced;
  18.     public GameObject inventoryItemToDestory;
  19.  
  20.     [SerializeField] GameObject placementModeUI;
  21.  
  22.     private void Awake()
  23.     {
  24.         if (Instance != null && Instance != this)
  25.         {
  26.             Destroy(gameObject);
  27.         }
  28.         else
  29.         {
  30.             Instance = this;
  31.         }
  32.     }
  33.  
  34.     public void ActivatePlacementMode(string itemToPlace)
  35.     {
  36.         GameObject item = Instantiate(Resources.Load<GameObject>(itemToPlace));
  37.  
  38.         // Changing the name of the gameobject so it will not be (clone)
  39.         item.name = itemToPlace;
  40.  
  41.         // Setting the item to be a child of our placement holding spot
  42.         item.transform.SetParent(placementHoldingSpot.transform, false);
  43.  
  44.         // Saving a reference to the item we want to place
  45.         itemToBePlaced = item;
  46.  
  47.         // Actiavting Construction mode
  48.         inPlacementMode = true;
  49.     }
  50.  
  51.  
  52.  
  53.     private void Update()
  54.     {
  55.  
  56.         if (inPlacementMode)
  57.         {
  58.             placementModeUI.SetActive(true);
  59.         }
  60.         else
  61.         {
  62.             placementModeUI.SetActive(false);
  63.         }
  64.  
  65.         if (itemToBePlaced != null && inPlacementMode)
  66.         {
  67.             if (IsCheckValidPlacement())
  68.             {
  69.                 isValidPlacement = true;
  70.                 itemToBePlaced.GetComponent<PlacebleItem>().SetValidColor();
  71.             }
  72.             else
  73.             {
  74.                 isValidPlacement = false;
  75.                 itemToBePlaced.GetComponent<PlacebleItem>().SetInvalidColor();
  76.             }
  77.         }
  78.  
  79.         // Left Mouse Click to Place item
  80.         if (Input.GetMouseButtonDown(0) && inPlacementMode && isValidPlacement)
  81.         {
  82.             PlaceItemFreeStyle();
  83.             DestroyItem(inventoryItemToDestory);
  84.         }
  85.  
  86.         // Cancel Placement                     //TODO - don't destroy the ui item until you actually placed it.
  87.         if (Input.GetKeyDown(KeyCode.X))
  88.         {
  89.             inventoryItemToDestory.SetActive(true);
  90.             inventoryItemToDestory = null;
  91.             DestroyItem(itemToBePlaced);
  92.             itemToBePlaced = null;
  93.             inPlacementMode = false;
  94.         }
  95.     }
  96.  
  97.     private bool IsCheckValidPlacement()
  98.     {
  99.         if (itemToBePlaced != null)
  100.         {
  101.             return itemToBePlaced.GetComponent<PlacebleItem>().isValidToBeBuilt;
  102.         }
  103.  
  104.         return false;
  105.     }
  106.  
  107.     private void PlaceItemFreeStyle()
  108.     {
  109.         // Setting the parent to be the root of our scene
  110.         itemToBePlaced.transform.SetParent(enviromentPlaceables.transform, true);
  111.  
  112.         // Setting the default color/material
  113.         itemToBePlaced.GetComponent<PlacebleItem>().SetDefaultColor();
  114.         itemToBePlaced.GetComponent<PlacebleItem>().enabled = false;
  115.  
  116.         itemToBePlaced = null;
  117.  
  118.         StartCoroutine(Delay());
  119.     }
  120.  
  121.     IEnumerator Delay()
  122.     {
  123.         yield return new WaitForSeconds(1f);
  124.         inPlacementMode = false;
  125.     }
  126.  
  127.  
  128.     private void DestroyItem(GameObject item)
  129.     {
  130.         DestroyImmediate(item);
  131.         InventorySystem.Instance.ReCalculateList();
  132.         CraftingSystem.Instance.RefreshNeededItems();
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement