Advertisement
evelynshilosky

ConstructionManager - Part 19

Oct 20th, 2023 (edited)
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.54 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class ConstructionManager : MonoBehaviour
  7. {
  8.     public static ConstructionManager Instance { get; set; }
  9.  
  10.     public GameObject itemToBeConstructed;
  11.     public bool inConstructionMode = false;
  12.     public GameObject constructionHoldingSpot;
  13.  
  14.     public bool isValidPlacement;
  15.  
  16.     public bool selectingAGhost;
  17.     public GameObject selectedGhost;
  18.  
  19.     // Materials we store as refereces for the ghosts
  20.     public Material ghostSelectedMat;
  21.     public Material ghostSemiTransparentMat; // Fpr Testing
  22.     public Material ghostFullTransparentMat;
  23.  
  24.     // We keep a reference to all ghosts currently in our world,
  25.     // so the manager can monitor them for various operations
  26.     public List<GameObject> allGhostsInExistence = new List<GameObject>();
  27.  
  28.     public GameObject itemToBeDestroyed;
  29.  
  30.     private void Awake()
  31.     {
  32.         if (Instance != null && Instance != this)
  33.         {
  34.             Destroy(gameObject);
  35.         }
  36.         else
  37.         {
  38.             Instance = this;
  39.         }
  40.     }
  41.  
  42.     public void ActivateConstructionPlacement(string itemToConstruct)
  43.     {
  44.         GameObject item = Instantiate(Resources.Load<GameObject>(itemToConstruct));
  45.  
  46.         //change the name of the gameobject so it will not be (clone)
  47.         item.name = itemToConstruct;
  48.  
  49.         item.transform.SetParent(constructionHoldingSpot.transform, false);
  50.         itemToBeConstructed = item;
  51.         itemToBeConstructed.gameObject.tag = "activeConstructable";
  52.  
  53.         // Disabling the non-trigger collider so our mouse can cast a ray
  54.         itemToBeConstructed.GetComponent<Constructable>().solidCollider.enabled = false;
  55.  
  56.         // Actiavting Construction mode
  57.         inConstructionMode = true;
  58.     }
  59.  
  60.     private void GetAllGhosts(GameObject itemToBeConstructed)
  61.     {
  62.         List<GameObject> ghostlist = itemToBeConstructed.gameObject.GetComponent<Constructable>().ghostList;
  63.  
  64.         foreach (GameObject ghost in ghostlist)
  65.         {
  66.             Debug.Log(ghost);
  67.             allGhostsInExistence.Add(ghost);
  68.         }
  69.     }
  70.  
  71.     private void PerformGhostDeletionScan()
  72.     {
  73.  
  74.         foreach (GameObject ghost in allGhostsInExistence)
  75.         {
  76.             if (ghost != null)
  77.             {
  78.                 if (ghost.GetComponent<GhostItem>().hasSamePosition == false) // if we did not already add a flag
  79.                 {
  80.                     foreach (GameObject ghostX in allGhostsInExistence)
  81.                     {
  82.                         // First we check that it is not the same object
  83.                         if (ghost.gameObject != ghostX.gameObject)
  84.                         {
  85.                             // If its not the same object but they have the same position
  86.                             if (XPositionToAccurateFloat(ghost) == XPositionToAccurateFloat(ghostX) && ZPositionToAccurateFloat(ghost) == ZPositionToAccurateFloat(ghostX))
  87.                             {
  88.                                 if (ghost != null && ghostX != null)
  89.                                 {
  90.                                     // setting the flag
  91.                                     ghostX.GetComponent<GhostItem>().hasSamePosition = true;
  92.                                     break;
  93.                                 }
  94.  
  95.                             }
  96.  
  97.                         }
  98.  
  99.                     }
  100.  
  101.                 }
  102.             }
  103.         }
  104.  
  105.         foreach (GameObject ghost in allGhostsInExistence)
  106.         {
  107.             if (ghost != null)
  108.             {
  109.                 if (ghost.GetComponent<GhostItem>().hasSamePosition)
  110.                 {
  111.                     DestroyImmediate(ghost);
  112.                 }
  113.             }
  114.  
  115.         }
  116.     }
  117.  
  118.     private float XPositionToAccurateFloat(GameObject ghost)
  119.     {
  120.         if (ghost != null)
  121.         {
  122.             // Turning the position to a 2 decimal rounded float
  123.             Vector3 targetPosition = ghost.gameObject.transform.position;
  124.             float pos = targetPosition.x;
  125.             float xFloat = Mathf.Round(pos * 100f) / 100f;
  126.             return xFloat;
  127.         }
  128.         return 0;
  129.     }
  130.  
  131.     private float ZPositionToAccurateFloat(GameObject ghost)
  132.     {
  133.  
  134.         if (ghost != null)
  135.         {
  136.             // Turning the position to a 2 decimal rounded float
  137.             Vector3 targetPosition = ghost.gameObject.transform.position;
  138.             float pos = targetPosition.z;
  139.             float zFloat = Mathf.Round(pos * 100f) / 100f;
  140.             return zFloat;
  141.  
  142.         }
  143.         return 0;
  144.     }
  145.  
  146.     private void Update()
  147.     {
  148.  
  149.         if (itemToBeConstructed != null && inConstructionMode)
  150.         {
  151.             if (CheckValidConstructionPosition())
  152.             {
  153.                 isValidPlacement = true;
  154.                 itemToBeConstructed.GetComponent<Constructable>().SetValidColor();
  155.             }
  156.             else
  157.             {
  158.                 isValidPlacement = false;
  159.                 itemToBeConstructed.GetComponent<Constructable>().SetInvalidColor();
  160.             }
  161.  
  162.  
  163.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  164.             RaycastHit hit;
  165.             if (Physics.Raycast(ray, out hit))
  166.             {
  167.                 var selectionTransform = hit.transform;
  168.                 if (selectionTransform.gameObject.CompareTag("ghost"))
  169.                 {
  170.                     itemToBeConstructed.SetActive(false);
  171.                     selectingAGhost = true;
  172.                     selectedGhost = selectionTransform.gameObject;
  173.                 }
  174.                 else
  175.                 {
  176.                     itemToBeConstructed.SetActive(true);
  177.                     selectingAGhost = false;
  178.                 }
  179.  
  180.             }
  181.         }
  182.  
  183.         // Left Mouse Click to Place item
  184.         if (Input.GetMouseButtonDown(0) && inConstructionMode)
  185.         {
  186.             if (isValidPlacement && selectedGhost == false) // We don't want the freestyle to be triggered when we select a ghost.
  187.             {
  188.                 PlaceItemFreeStyle();
  189.                 DestroyItem(itemToBeDestroyed);
  190.             }
  191.  
  192.             if (selectingAGhost)
  193.             {
  194.                 PlaceItemInGhostPosition(selectedGhost);
  195.                 DestroyItem(itemToBeDestroyed);
  196.             }
  197.         }
  198.         // Right Mouse Click to Cancel                      //TODO - don't destroy the ui item until you actually placed it.
  199.         if (Input.GetKeyDown(KeyCode.X))
  200.         {
  201.             itemToBeDestroyed.SetActive(true);
  202.             itemToBeDestroyed = null;
  203.             DestroyItem(itemToBeConstructed);
  204.             itemToBeConstructed = null;
  205.             inConstructionMode = false;
  206.         }
  207.     }
  208.  
  209.     private void PlaceItemInGhostPosition(GameObject copyOfGhost)
  210.     {
  211.  
  212.         Vector3 ghostPosition = copyOfGhost.transform.position;
  213.         Quaternion ghostRotation = copyOfGhost.transform.rotation;
  214.  
  215.         selectedGhost.gameObject.SetActive(false);
  216.  
  217.         // Setting the item to be active again (after we disabled it in the ray cast)
  218.         itemToBeConstructed.gameObject.SetActive(true);
  219.         // Setting the parent to be the root of our scene
  220.         itemToBeConstructed.transform.SetParent(transform.parent.transform.parent, true);
  221.  
  222.         itemToBeConstructed.transform.position = ghostPosition;
  223.         itemToBeConstructed.transform.rotation = ghostRotation;
  224.  
  225.         // Making the Ghost Children to no longer be children of this item
  226.         itemToBeConstructed.GetComponent<Constructable>().ExtractGhostMembers();
  227.         // Setting the default color/material
  228.         itemToBeConstructed.GetComponent<Constructable>().SetDefaultColor();
  229.         itemToBeConstructed.tag = "placedFoundation";
  230.  
  231.         // Enabling back the solider collider that we disabled earlier
  232.         itemToBeConstructed.GetComponent<Constructable>().solidCollider.enabled = true;
  233.  
  234.         //Adding all the ghosts of this item into the manager's ghost bank
  235.         GetAllGhosts(itemToBeConstructed);
  236.         PerformGhostDeletionScan();
  237.  
  238.         itemToBeConstructed = null;
  239.  
  240.         inConstructionMode = false;
  241.     }
  242.  
  243.  
  244.     private void PlaceItemFreeStyle()
  245.     {
  246.         // Setting the parent to be the root of our scene
  247.         itemToBeConstructed.transform.SetParent(transform.parent.transform.parent, true);
  248.  
  249.         // Making the Ghost Children to no longer be children of this item
  250.         itemToBeConstructed.GetComponent<Constructable>().ExtractGhostMembers();
  251.         // Setting the default color/material
  252.         itemToBeConstructed.GetComponent<Constructable>().SetDefaultColor();
  253.         itemToBeConstructed.tag = "placedFoundation";
  254.         itemToBeConstructed.GetComponent<Constructable>().enabled = false;
  255.         // Enabling back the solider collider that we disabled earlier
  256.         itemToBeConstructed.GetComponent<Constructable>().solidCollider.enabled = true;
  257.  
  258.         //Adding all the ghosts of this item into the manager's ghost bank
  259.         GetAllGhosts(itemToBeConstructed);
  260.         PerformGhostDeletionScan();
  261.  
  262.         itemToBeConstructed = null;
  263.  
  264.         inConstructionMode = false;
  265.     }
  266.      
  267.      
  268.     void DestroyItem(GameObject item)
  269.     {
  270.         DestroyImmediate(item);
  271.         InventorySystem.Instance.ReCalculateList();
  272.         CraftingSystem.Instance.RefreshNeededItems();
  273.     }
  274.      
  275.     private bool CheckValidConstructionPosition()
  276.     {
  277.         if (itemToBeConstructed != null)
  278.         {
  279.             return itemToBeConstructed.GetComponent<Constructable>().isValidToBeBuilt;
  280.         }
  281.  
  282.         return false;
  283.     }
  284. }
  285.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement