Advertisement
evelynshilosky

ConstructionManager - Part 20

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