Advertisement
Junglerally

Building Script

Jan 6th, 2022 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.04 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class Decorating : MonoBehaviour
  7. {
  8.     public CoursePieceAsset[] coursePieces;
  9.     public DecorationAsset[] decorations;
  10.     private DecorationAsset selectedDecoration;
  11.     private CoursePieceAsset selectedCoursePiece;
  12.    
  13.     private NotificationManager notifications;
  14.  
  15.     [SerializeField] private GameObject holeObj;
  16.     [SerializeField] private GameObject buildingPosObj;
  17.     [SerializeField] private GameObject updateGraphObject;
  18.     public GameObject pendingObject;
  19.     private GameObject decorationsParent;
  20.     [HideInInspector] public GameObject selectedHole;
  21.  
  22.     [SerializeField] private Toggle gridToggle;
  23.     [SerializeField] private Toggle rotateToggle;
  24.  
  25.     [SerializeField] float rotateAmount;
  26.     public float gridSize = 1.5f;
  27.  
  28.     RaycastHit hit;
  29.     Quaternion faceRotation;
  30.  
  31.     [SerializeField] private LayerMask layerMask;
  32.  
  33.     [HideInInspector] public bool gridOn = true;
  34.     [HideInInspector] public bool isRotating = false;
  35.     [HideInInspector] public bool smoothRotateOn = false;
  36.     bool coursePieceIsSelected = false;
  37.  
  38.     [SerializeField] private Material[] materials; //0 = master, 1 = valid, 2 = invalid;
  39.  
  40.     public Vector3 n;
  41.  
  42.     private void Update()
  43.     {
  44.             if (pendingObject != null)
  45.             {
  46.                 Building();
  47.             }
  48.     }
  49.  
  50.     private void Start()
  51.     {
  52.         decorationsParent = GameObject.Find("Decorations");
  53.         notifications = GameObject.Find("Notifications").GetComponent<NotificationManager>();
  54.     }
  55.  
  56.     void Building()
  57.     {
  58.         //Snapping position to grid
  59.         if (gridOn)
  60.         {
  61.             buildingPosObj.transform.position = new Vector3(
  62.             RoundToNearestGrid(buildingPosObj.transform.position.x),
  63.             buildingPosObj.transform.position.y,
  64.             RoundToNearestGrid(buildingPosObj.transform.position.z));
  65.         }
  66.         //Setting obj pos
  67.         SetObjectPosition();
  68.         //Building Actions
  69.  
  70.         if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Backspace)) //Canceling placement
  71.         {
  72.             DestroyObject();
  73.         }
  74.         if (NonUIInput.GetKey(KeyCode.R) && !rotateToggle.isOn) //Rotate Object
  75.         {
  76.             SmoothRotateObject();
  77.         }
  78.         else if(NonUIInput.GetKeyDown(KeyCode.R) && rotateToggle.isOn) {
  79.             SharpRotateObject();
  80.         }
  81.         if (NonUIInput.GetKeyUp(KeyCode.R)) { isRotating = false; }
  82.         if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Return)) //Placing Object
  83.         {
  84.                 PlaceObject();
  85.         }
  86.     }
  87.     void FixedUpdate()
  88.     {
  89.         //setting the buildingPosObj position
  90.  
  91.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  92.  
  93.         if (Physics.Raycast(ray, out hit, 1000, layerMask))
  94.         {
  95.             buildingPosObj.transform.position = hit.point;
  96.         }
  97.  
  98.     }
  99.  
  100.     void SetObjectPosition()
  101.     {
  102.         if (!isRotating)
  103.         {
  104.             //Movement
  105.             pendingObject.transform.position = buildingPosObj.transform.position;
  106.             //Rotation
  107.             Vector3 up = hit.normal;
  108.             Vector3 forward = Vector3.Cross(Vector3.up, up);
  109.             Vector3 finalRot = new Vector3(Quaternion.LookRotation(forward, up).eulerAngles.x, Quaternion.LookRotation(forward, up).eulerAngles.y + pendingObject.transform.eulerAngles.y, Quaternion.LookRotation(forward, up).eulerAngles.z);
  110.             pendingObject.transform.eulerAngles = finalRot;
  111.         }
  112.  
  113.     }
  114.  
  115.     void PlaceObject()
  116.     {
  117.         pendingObject.tag = "Decoration";
  118.         pendingObject.GetComponent<MeshRenderer>().material = materials[0];
  119.         pendingObject.GetComponent<Collider>().enabled = true;
  120.         //UpdatingGraph
  121.         GameObject graphUpdate = Instantiate(updateGraphObject, pendingObject.transform.position, transform.rotation);
  122.         graphUpdate.GetComponent<UpdateGraph>().UpdateSurroundingGraph();
  123.         Destroy(graphUpdate);
  124.         pendingObject = null;
  125.         if (Input.GetKey(KeyCode.LeftShift))
  126.         {
  127.             if (coursePieceIsSelected) { SelectCoursePiece(selectedCoursePiece); }
  128.             else { SelectDecoration(selectedDecoration); }
  129.         }
  130.     }
  131.  
  132.     void SmoothRotateObject()
  133.     {
  134.         isRotating = true;
  135.         Vector3 targetPos = new Vector3(buildingPosObj.transform.position.x, pendingObject.transform.position.y, buildingPosObj.transform.position.z);
  136.         pendingObject.transform.LookAt(targetPos);
  137.     }
  138.  
  139.     void SharpRotateObject()
  140.     {
  141.         Vector3 rotation = pendingObject.transform.eulerAngles;
  142.         rotation.y += rotateAmount;
  143.         pendingObject.transform.eulerAngles = new Vector3(pendingObject.transform.eulerAngles.x, rotation.y, pendingObject.transform.eulerAngles.z);
  144.     }
  145.  
  146.     void DestroyObject()
  147.     {
  148.             Destroy(pendingObject);
  149.     }
  150.  
  151.     public void SelectDecoration(DecorationAsset decorationToSelect)
  152.     {
  153.         if (pendingObject != null) { Destroy(pendingObject); }
  154.         selectedDecoration = decorationToSelect;
  155.         pendingObject = Instantiate(selectedDecoration.prefab, buildingPosObj.transform.position, selectedDecoration.prefab.transform.rotation, decorationsParent.transform);
  156.         pendingObject.name = selectedDecoration.objectName;
  157.         coursePieceIsSelected = false;
  158.         pendingObject.GetComponent<Collider>().enabled = false;
  159.         isRotating = false;
  160.     }
  161.     public void SelectCoursePiece(CoursePieceAsset pieceToSelect)
  162.     {
  163.         isRotating = false;
  164.         if (selectedHole != null)
  165.         {
  166.             if (pendingObject != null) { Destroy(pendingObject); }
  167.             selectedCoursePiece = pieceToSelect;
  168.             pendingObject = Instantiate(selectedCoursePiece.variants[0], buildingPosObj.transform.position, selectedCoursePiece.variants[0].transform.rotation, selectedHole.transform.parent.parent);
  169.             pendingObject.name = selectedCoursePiece.objectName;
  170.             pendingObject.GetComponent<Collider>().enabled = false;
  171.             coursePieceIsSelected = true;
  172.         }
  173.         else { notifications.Notification("Please select a hole first!"); }
  174.     }
  175.  
  176.     public void PlaceHole()
  177.     {
  178.         if (selectedHole != null)
  179.         {
  180.             if (pendingObject != null) { Destroy(pendingObject); };
  181.             pendingObject = Instantiate(holeObj, buildingPosObj.transform.position, holeObj.transform.rotation, selectedHole.transform.parent.parent);
  182.             pendingObject.name = "Hole";
  183.         }
  184.         else { notifications.Notification("Please select a hole first!"); }
  185.     }
  186.     public void ToggleGrid()
  187.     {
  188.         if (gridToggle.isOn) {
  189.             gridOn = true;
  190.         }
  191.         else { gridOn = false; }
  192.     }
  193.  
  194.     float RoundToNearestGrid(float pos)
  195.     {
  196.         //Changeable grid system
  197.         float xDiff = pos % gridSize;
  198.         pos -= xDiff;
  199.         if (xDiff > (gridSize / 2))
  200.         {
  201.             pos += gridSize;
  202.         }
  203.         return pos;
  204.     }
  205. }
  206.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement