Advertisement
evelynshilosky

ConstructionManager - Part 33

Mar 7th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.85 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.         // Left Mouse Click to Place item
  209.         if (Input.GetMouseButtonDown(0) && inConstructionMode)
  210.         {
  211.             if (isValidPlacement && selectedGhost == false && itemToBeConstructed.name == "FoundationModel") // We don't want the freestyle to be triggered when we select a ghost.
  212.             {
  213.                 PlaceItemFreeStyle();
  214.                 DestroyItem(itemToBeDestroyed);
  215.             }
  216.  
  217.             if (selectingAGhost)
  218.             {
  219.                 PlaceItemInGhostPosition(selectedGhost);
  220.                 DestroyItem(itemToBeDestroyed);
  221.             }
  222.         }
  223.         // Right Mouse Click to Cancel                      //TODO - don't destroy the ui item until you actually placed it.
  224.         if (Input.GetKeyDown(KeyCode.X) && inConstructionMode)
  225.         {
  226.             itemToBeDestroyed.SetActive(true);
  227.             itemToBeDestroyed = null;
  228.             DestroyItem(itemToBeConstructed);
  229.             itemToBeConstructed = null;
  230.             inConstructionMode = false;
  231.         }
  232.     }
  233.  
  234.     private void PlaceItemInGhostPosition(GameObject copyOfGhost)
  235.     {
  236.  
  237.         Vector3 ghostPosition = copyOfGhost.transform.position;
  238.         Quaternion ghostRotation = copyOfGhost.transform.rotation;
  239.  
  240.         selectedGhost.gameObject.SetActive(false);
  241.  
  242.         // Setting the item to be active again (after we disabled it in the ray cast)
  243.         itemToBeConstructed.gameObject.SetActive(true);
  244.         // Setting the parent to be the root of our scene
  245.         itemToBeConstructed.transform.SetParent(transform.parent.transform.parent, true);
  246.  
  247.         var randomOffset = UnityEngine.Random.Range(0.01f, 0.03f);
  248.  
  249.         itemToBeConstructed.transform.position = new Vector3(ghostPosition.x, ghostPosition.y, ghostPosition.z + randomOffset);
  250.         itemToBeConstructed.transform.rotation = ghostRotation;
  251.  
  252.         // Enabling back the solider collider that we disabled earlier
  253.         itemToBeConstructed.GetComponent<Constructable>().solidCollider.enabled = true;
  254.  
  255.         // Setting the default color/material
  256.         itemToBeConstructed.GetComponent<Constructable>().SetDefaultColor();
  257.  
  258.         if (itemToBeConstructed.name == "FoundationModel")
  259.         {
  260.             // Making the Ghost Children to no longer be children of this item
  261.             itemToBeConstructed.GetComponent<Constructable>().ExtractGhostMembers();
  262.             itemToBeConstructed.tag = "placedFoundation";
  263.             //Adding all the ghosts of this item into the manager's ghost bank
  264.             GetAllGhosts(itemToBeConstructed);
  265.             PerformGhostDeletionScan();
  266.         }
  267.         else
  268.         {
  269.             itemToBeConstructed.tag = "placedWall";
  270.             DestroyItem(selectedGhost); // We delete this wallGhost, because the Manager will not do it
  271.         }
  272.  
  273.         itemToBeConstructed = null;
  274.  
  275.         inConstructionMode = false;
  276.     }
  277.  
  278.  
  279.     private void PlaceItemFreeStyle()
  280.     {
  281.         // Setting the parent to be the root of our scene
  282.         itemToBeConstructed.transform.SetParent(transform.parent.transform.parent, true);
  283.  
  284.         // Making the Ghost Children to no longer be children of this item
  285.         itemToBeConstructed.GetComponent<Constructable>().ExtractGhostMembers();
  286.         // Setting the default color/material
  287.         itemToBeConstructed.GetComponent<Constructable>().SetDefaultColor();
  288.         itemToBeConstructed.tag = "placedFoundation";
  289.         itemToBeConstructed.GetComponent<Constructable>().enabled = false;
  290.         // Enabling back the solider collider that we disabled earlier
  291.         itemToBeConstructed.GetComponent<Constructable>().solidCollider.enabled = true;
  292.  
  293.         //Adding all the ghosts of this item into the manager's ghost bank
  294.         GetAllGhosts(itemToBeConstructed);
  295.         PerformGhostDeletionScan();
  296.  
  297.         itemToBeConstructed = null;
  298.  
  299.         inConstructionMode = false;
  300.     }
  301.      
  302.      
  303.     void DestroyItem(GameObject item)
  304.     {
  305.         DestroyImmediate(item);
  306.         InventorySystem.Instance.ReCalculateList();
  307.         CraftingSystem.Instance.RefreshNeededItems();
  308.     }
  309.      
  310.     private bool CheckValidConstructionPosition()
  311.     {
  312.         if (itemToBeConstructed != null)
  313.         {
  314.             return itemToBeConstructed.GetComponent<Constructable>().isValidToBeBuilt;
  315.         }
  316.  
  317.         return false;
  318.     }
  319. }
  320.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement