Guest User

Preview

a guest
Apr 7th, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.92 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5.  
  6. public class BuildSystem : MonoBehaviour
  7. {
  8.     public Demo_ObjectData currentPreview;
  9.  
  10.     public GameObject selectedPrefab;
  11.  
  12.     public Material previewMaterial;
  13.     public Material previewDeniedMaterial;
  14.  
  15.     public Renderer[] renderers;
  16.  
  17.     public bool allowPlacement = true;
  18.  
  19.     public GameObject collisionBox;
  20.  
  21.     void Update()
  22.     {
  23.         UpdatePlacement();
  24.         MovePreview();
  25.     }
  26.  
  27.     public void UpdatePlacement()
  28.     {
  29.         if (selectedPrefab == null)
  30.         {
  31.             return;
  32.         } else
  33.         {
  34.             if (!PreviewExists())
  35.             {
  36.                 CreatePreview(selectedPrefab);
  37.             }
  38.         }
  39.     }
  40.  
  41.     public void CreatePreview(GameObject prefab)
  42.     {
  43.         currentPreview = Instantiate(prefab).GetComponent<Demo_ObjectData>();
  44.     }
  45.  
  46.     public void InitializedRenders(GameObject prefab)
  47.     {
  48.        
  49.     }
  50.  
  51.     public void MovePreview()
  52.     {
  53.         if (currentPreview != null)
  54.         {
  55.             Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1.0f));
  56.  
  57.             int layer_mask = LayerMask.GetMask("Room");
  58.  
  59.             if (Physics.Raycast(ray, out RaycastHit Hit, Mathf.Infinity, layer_mask, QueryTriggerInteraction.Ignore))
  60.                 currentPreview.transform.position = Hit.point;
  61.  
  62.             collisionBox.transform.parent = currentPreview.transform.GetChild(0);
  63.  
  64.             float WheelAxis = Input.GetAxis("Mouse ScrollWheel");
  65.            
  66.             if (Input.GetKey(KeyCode.LeftShift))
  67.             {
  68.                 if (WheelAxis > 0)
  69.                     currentPreview.transform.GetChild(0).eulerAngles += new Vector3(currentPreview.transform.localRotation.x, Rotation(currentPreview.transform.GetChild(0).eulerAngles.y), currentPreview.transform.localRotation.z);
  70.                 else if (WheelAxis < 0)
  71.                     currentPreview.transform.GetChild(0).eulerAngles += new Vector3(currentPreview.transform.localRotation.x, -NegativeRotation(currentPreview.transform.GetChild(0).eulerAngles.y), currentPreview.transform.localRotation.z);
  72.             }
  73.             else
  74.             {
  75.                 if (WheelAxis > 0)
  76.                     currentPreview.transform.GetChild(0).eulerAngles += new Vector3(currentPreview.transform.localRotation.x, currentPreview.smoothRotation, currentPreview.transform.localRotation.z);
  77.                 else if (WheelAxis < 0)
  78.                     currentPreview.transform.GetChild(0).eulerAngles += new Vector3(currentPreview.transform.localRotation.x, -currentPreview.smoothRotation, currentPreview.transform.localRotation.z);
  79.             }
  80.            
  81.  
  82.             if (allowPlacement)
  83.             {
  84.                 renderers = currentPreview.GetComponentsInChildren<Renderer>();
  85.  
  86.                 foreach (var rend in renderers)
  87.                 {
  88.                     rend.sharedMaterial = previewMaterial;
  89.                 }
  90.  
  91.                 if (Input.GetMouseButtonDown(0))
  92.                 {
  93.                     PlacePrefab(currentPreview, currentPreview.transform.position, currentPreview.transform.eulerAngles);
  94.                 }
  95.             }
  96.             else
  97.             {
  98.                 renderers = currentPreview.GetComponentsInChildren<Renderer>();
  99.  
  100.                 foreach (var rend in renderers)
  101.                 {
  102.                     rend.sharedMaterial = previewDeniedMaterial;
  103.                 }
  104.             }
  105.         }
  106.     }
  107.  
  108.     public bool PreviewExists()
  109.     {
  110.         return currentPreview;
  111.     }
  112.  
  113.     public void PlacePrefab(Demo_ObjectData part, Vector3 position, Vector3 rotation)
  114.     {
  115.         collisionBox.transform.eulerAngles = Vector3.zero;
  116.  
  117.         collisionBox.transform.parent = null;
  118.  
  119.         GameObject PlacedTemp = Instantiate(part.gameObject, position, Quaternion.Euler(rotation));
  120.  
  121.         renderers = PlacedTemp.GetComponentsInChildren<Renderer>();
  122.  
  123.         foreach (var rend in renderers)
  124.         {
  125.             rend.sharedMaterial = PlacedTemp.GetComponent<Demo_ObjectData>().initialMaterial;
  126.         }
  127.  
  128.         PlacedTemp.transform.parent = GameMaster.Instance.BuildingManager.currentBuilding.transform;
  129.  
  130.         GameMaster.Instance.placedParts.Add(PlacedTemp);
  131.  
  132.         selectedPrefab = null;
  133.  
  134.         Destroy(currentPreview.gameObject);
  135.         currentPreview = null;
  136.     }
  137.  
  138.     IEnumerator RotateObject(float angle)
  139.     {
  140.         Quaternion initialRot = currentPreview.transform.GetChild(0).transform.rotation;
  141.         Quaternion endRot = Quaternion.Euler(initialRot.x, initialRot.y + angle, initialRot.z);
  142.         float elapsed = 0.0f;
  143.         while (elapsed < 1f)
  144.         {
  145.             currentPreview.transform.GetChild(0).transform.rotation = Quaternion.Slerp(initialRot, endRot, elapsed / 1f);
  146.             elapsed += Time.deltaTime;
  147.             yield return null;
  148.         }
  149.         currentPreview.transform.GetChild(0).transform.rotation = endRot;
  150.     }
  151.  
  152.     public float Rotation(float rotationAngle)
  153.     {
  154.         float angle = Mathf.Abs(rotationAngle);
  155.         if (angle == 180)
  156.             angle = 270 - angle;
  157.         if (angle < 90)
  158.             angle = 90 - angle;
  159.         if (angle > 89 && angle < 180)
  160.             angle = 180 - angle;
  161.         if (angle > 179 && angle < 270)
  162.             angle = 270 - angle;
  163.         if (angle > 269)
  164.             angle = 360 - angle;
  165.  
  166.         return angle;
  167.     }
  168.     public float NegativeRotation(float rotationAngle)
  169.     {
  170.         if (rotationAngle == 180)
  171.             rotationAngle = 90f;
  172.         if (rotationAngle > 89 && rotationAngle < 180)
  173.             rotationAngle = rotationAngle - 90f;
  174.         if (rotationAngle > 179 && rotationAngle < 270)
  175.             rotationAngle = rotationAngle - 180f;
  176.         if (rotationAngle > 269)
  177.             rotationAngle = rotationAngle - 270;
  178.  
  179.         return rotationAngle;
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment