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