| using System; |
| using System.Collections; |
| using System.Collections.Generic; |
| using UnityEngine; |
|
|
| public class ConstructionManager : MonoBehaviour |
| { |
| public static ConstructionManager Instance { get; set; } |
|
|
| public GameObject itemToBeConstructed; |
| public bool inConstructionMode = false; |
| public GameObject constructionHoldingSpot; |
|
|
| public bool isValidPlacement; |
|
|
| public bool selectingAGhost; |
| public GameObject selectedGhost; |
|
|
| |
| public Material ghostSelectedMat; |
| public Material ghostSemiTransparentMat; |
| public Material ghostFullTransparentMat; |
|
|
| public GameObject itemToBeDestroyed; |
|
|
| |
| |
| public List<GameObject> allGhostsInExistence = new List<GameObject>(); |
|
|
| private void Awake() |
| { |
| if (Instance != null && Instance != this) |
| { |
| Destroy(gameObject); |
| } |
| else |
| { |
| Instance = this; |
| } |
| } |
|
|
| public void ActivateConstructionPlacement(string itemToConstruct) |
| { |
| GameObject item = Instantiate(Resources.Load<GameObject>(itemToConstruct)); |
|
|
| |
| item.name = itemToConstruct; |
|
|
| item.transform.SetParent(constructionHoldingSpot.transform, false); |
| itemToBeConstructed = item; |
| itemToBeConstructed.gameObject.tag = "activeConstructable"; |
|
|
| |
| itemToBeConstructed.GetComponent<Constructable>().solidCollider.enabled = false; |
|
|
| |
| inConstructionMode = true; |
| } |
|
|
| private void GetAllGhosts(GameObject itemToBeConstructed) |
| { |
| List<GameObject> ghostlist = itemToBeConstructed.gameObject.GetComponent<Constructable>().ghostList; |
|
|
| foreach (GameObject ghost in ghostlist) |
| { |
| Debug.Log(ghost); |
| allGhostsInExistence.Add(ghost); |
| } |
| } |
|
|
| private void PerformGhostDeletionScan() |
| { |
|
|
| foreach (GameObject ghost in allGhostsInExistence) |
| { |
| if (ghost != null) |
| { |
| if (ghost.GetComponent<GhostItem>().hasSamePosition == false) |
| { |
| foreach (GameObject ghostX in allGhostsInExistence) |
| { |
| |
| if (ghost.gameObject != ghostX.gameObject) |
| { |
| |
| if (XPositionToAccurateFloat(ghost) == XPositionToAccurateFloat(ghostX) && ZPositionToAccurateFloat(ghost) == ZPositionToAccurateFloat(ghostX)) |
| { |
| if (ghost != null && ghostX != null) |
| { |
| |
| ghostX.GetComponent<GhostItem>().hasSamePosition = true; |
| break; |
| } |
|
|
| } |
|
|
| } |
|
|
| } |
|
|
| } |
| } |
| } |
|
|
| foreach (GameObject ghost in allGhostsInExistence) |
| { |
| if (ghost != null) |
| { |
| if (ghost.GetComponent<GhostItem>().hasSamePosition) |
| { |
| DestroyImmediate(ghost); |
| } |
| } |
|
|
| } |
| } |
|
|
| private float XPositionToAccurateFloat(GameObject ghost) |
| { |
| if (ghost != null) |
| { |
| |
| Vector3 targetPosition = ghost.gameObject.transform.position; |
| float pos = targetPosition.x; |
| float xFloat = Mathf.Round(pos * 100f) / 100f; |
| return xFloat; |
| } |
| return 0; |
| } |
|
|
| private float ZPositionToAccurateFloat(GameObject ghost) |
| { |
|
|
| if (ghost != null) |
| { |
| |
| Vector3 targetPosition = ghost.gameObject.transform.position; |
| float pos = targetPosition.z; |
| float zFloat = Mathf.Round(pos * 100f) / 100f; |
| return zFloat; |
|
|
| } |
| return 0; |
| } |
|
|
| private void Update() |
| { |
|
|
| if (itemToBeConstructed != null && inConstructionMode) |
| { |
| if (CheckValidConstructionPosition()) |
| { |
| isValidPlacement = true; |
| itemToBeConstructed.GetComponent<Constructable>().SetValidColor(); |
| } |
| else |
| { |
| isValidPlacement = false; |
| itemToBeConstructed.GetComponent<Constructable>().SetInvalidColor(); |
| } |
|
|
|
|
| Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); |
| RaycastHit hit; |
| if (Physics.Raycast(ray, out hit)) |
| { |
| var selectionTransform = hit.transform; |
| if (selectionTransform.gameObject.CompareTag("ghost")) |
| { |
| itemToBeConstructed.SetActive(false); |
| selectingAGhost = true; |
| selectedGhost = selectionTransform.gameObject; |
| } |
| else |
| { |
| itemToBeConstructed.SetActive(true); |
| selectingAGhost = false; |
| } |
|
|
| } |
| } |
|
|
| |
| if (Input.GetMouseButtonDown(0) && inConstructionMode) |
| { |
| if (isValidPlacement && selectedGhost == false) |
| { |
| PlaceItemFreeStyle(); |
| } |
|
|
| if (selectingAGhost) |
| { |
| PlaceItemInGhostPosition(selectedGhost); |
| } |
| } |
| |
| if (Input.GetMouseButtonDown(0) && isValidPlacement) |
| { |
|
|
|
|
|
|
| } |
| } |
|
|
| private void PlaceItemInGhostPosition(GameObject copyOfGhost) |
| { |
|
|
| Vector3 ghostPosition = copyOfGhost.transform.position; |
| Quaternion ghostRotation = copyOfGhost.transform.rotation; |
|
|
| selectedGhost.gameObject.SetActive(false); |
|
|
| |
| itemToBeConstructed.gameObject.SetActive(true); |
| |
| itemToBeConstructed.transform.SetParent(transform.parent.transform.parent, true); |
|
|
| itemToBeConstructed.transform.position = ghostPosition; |
| itemToBeConstructed.transform.rotation = ghostRotation; |
|
|
| |
| itemToBeConstructed.GetComponent<Constructable>().ExtractGhostMembers(); |
| |
| itemToBeConstructed.GetComponent<Constructable>().SetDefaultColor(); |
| itemToBeConstructed.tag = "placedFoundation"; |
|
|
| |
| itemToBeConstructed.GetComponent<Constructable>().solidCollider.enabled = true; |
|
|
| |
| GetAllGhosts(itemToBeConstructed); |
| PerformGhostDeletionScan(); |
|
|
| itemToBeConstructed = null; |
|
|
| inConstructionMode = false; |
| } |
|
|
|
|
| private void PlaceItemFreeStyle() |
| { |
| |
| itemToBeConstructed.transform.SetParent(transform.parent.transform.parent, true); |
|
|
| |
| itemToBeConstructed.GetComponent<Constructable>().ExtractGhostMembers(); |
| |
| itemToBeConstructed.GetComponent<Constructable>().SetDefaultColor(); |
| itemToBeConstructed.tag = "placedFoundation"; |
| itemToBeConstructed.GetComponent<Constructable>().enabled = false; |
| |
| itemToBeConstructed.GetComponent<Constructable>().solidCollider.enabled = true; |
|
|
| |
| GetAllGhosts(itemToBeConstructed); |
| PerformGhostDeletionScan(); |
|
|
| itemToBeConstructed = null; |
|
|
| inConstructionMode = false; |
| } |
|
|
| private bool CheckValidConstructionPosition() |
| { |
| if (itemToBeConstructed != null) |
| { |
| return itemToBeConstructed.GetComponent<Constructable>().isValidToBeBuilt; |
| } |
|
|
| return false; |
| } |
|
|
| void DestroyItem(GameObject item) |
| { |
| DestroyImmediate(item); |
| InventorySystem.Instance.ReCalculateList(); |
| CraftingSystem.Instance.RefreshNeededItems(); |
| } |
| } |