Advertisement
evelynshilosky

Constructable - Part 31

Feb 1st, 2024
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class Constructable : MonoBehaviour
  7. {
  8.     // Validation
  9.     public bool isGrounded;
  10.     public bool isOverlappingItems;
  11.     public bool isValidToBeBuilt;
  12.     public bool detectedGhostMember;
  13.  
  14.     // Material related
  15.     private Renderer mRenderer;
  16.     public Material redMaterial;
  17.     public Material greenMaterial;
  18.     public Material defaultMaterial;
  19.  
  20.     public List<GameObject> ghostList = new List<GameObject>();
  21.  
  22.     public BoxCollider solidCollider; // We need to drag this collider manualy into the inspector
  23.  
  24.     private void Start()
  25.     {
  26.         mRenderer = GetComponent<Renderer>();
  27.  
  28.         mRenderer.material = defaultMaterial;
  29.         foreach (Transform child in transform)
  30.         {
  31.             ghostList.Add(child.gameObject);
  32.         }
  33.  
  34.     }
  35.     void Update()
  36.     {
  37.         if (isGrounded && isOverlappingItems == false)
  38.         {
  39.             isValidToBeBuilt = true;
  40.         }
  41.         else
  42.         {
  43.             isValidToBeBuilt = false;
  44.         }
  45.     }
  46.  
  47.     private void OnTriggerEnter(Collider other)
  48.     {
  49.         if (other.CompareTag("Ground") && gameObject.CompareTag("activeConstructable"))
  50.         {
  51.             isGrounded = true;
  52.         }
  53.  
  54.         if (other.CompareTag("Tree") || other.CompareTag("pickable") && gameObject.CompareTag("activeConstructable"))
  55.         {
  56.  
  57.             isOverlappingItems = true;
  58.         }
  59.  
  60.         if (other.gameObject.CompareTag("ghost") && gameObject.CompareTag("activeConstructable"))
  61.         {
  62.             detectedGhostMember = true;
  63.         }
  64.     }
  65.  
  66.     private void OnTriggerExit(Collider other)
  67.     {
  68.         if (other.CompareTag("Ground") && gameObject.CompareTag("activeConstructable"))
  69.         {
  70.             isGrounded = false;
  71.         }
  72.  
  73.         if (other.CompareTag("Tree") || other.CompareTag("pickable") && gameObject.CompareTag("activeConstructable"))
  74.         {
  75.             isOverlappingItems = false;
  76.         }
  77.  
  78.         if (other.gameObject.CompareTag("ghost") && gameObject.CompareTag("activeConstructable"))
  79.         {
  80.             detectedGhostMember = false;
  81.         }
  82.     }
  83.  
  84.     public void SetInvalidColor()
  85.     {
  86.         if (mRenderer != null)
  87.         {
  88.             mRenderer.material = redMaterial;
  89.         }
  90.     }
  91.  
  92.     public void SetValidColor()
  93.     {
  94.         mRenderer.material = greenMaterial;
  95.     }
  96.  
  97.     public void SetDefaultColor()
  98.     {
  99.         mRenderer.material = defaultMaterial;
  100.     }
  101.  
  102.     public void ExtractGhostMembers()
  103.     {
  104.         foreach (GameObject item in ghostList)
  105.         {
  106.             item.transform.SetParent(transform.parent, true);
  107.             item.gameObject.GetComponent<GhostItem>().solidCollider.enabled = false;
  108.             item.gameObject.GetComponent<GhostItem>().isPlaced = true;
  109.         }
  110.     }
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement