Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- public class BuildSystem : MonoBehaviour
- {
- public Demo_ObjectData currentPreview;
- public GameObject selectedPrefab;
- public Material previewMaterial;
- public Material previewDeniedMaterial;
- public Renderer[] renderers;
- public bool allowPlacement = true;
- public GameObject collisionBox;
- void Update()
- {
- UpdatePlacement();
- MovePreview();
- }
- public void UpdatePlacement()
- {
- if (selectedPrefab == null)
- {
- return;
- } else
- {
- if (!PreviewExists())
- {
- CreatePreview(selectedPrefab);
- }
- }
- }
- public void CreatePreview(GameObject prefab)
- {
- currentPreview = Instantiate(prefab).GetComponent<Demo_ObjectData>();
- }
- public void InitializedRenders(GameObject prefab)
- {
- }
- public void MovePreview()
- {
- if (currentPreview != null)
- {
- Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1.0f));
- int layer_mask = LayerMask.GetMask("Room");
- if (Physics.Raycast(ray, out RaycastHit Hit, Mathf.Infinity, layer_mask, QueryTriggerInteraction.Ignore))
- currentPreview.transform.position = Hit.point;
- collisionBox.transform.parent = currentPreview.transform.GetChild(0);
- float WheelAxis = Input.GetAxis("Mouse ScrollWheel");
- if (Input.GetKey(KeyCode.LeftShift))
- {
- if (WheelAxis > 0)
- currentPreview.transform.GetChild(0).eulerAngles += new Vector3(currentPreview.transform.localRotation.x, Rotation(currentPreview.transform.GetChild(0).eulerAngles.y), currentPreview.transform.localRotation.z);
- else if (WheelAxis < 0)
- currentPreview.transform.GetChild(0).eulerAngles += new Vector3(currentPreview.transform.localRotation.x, -NegativeRotation(currentPreview.transform.GetChild(0).eulerAngles.y), currentPreview.transform.localRotation.z);
- }
- else
- {
- if (WheelAxis > 0)
- currentPreview.transform.GetChild(0).eulerAngles += new Vector3(currentPreview.transform.localRotation.x, currentPreview.smoothRotation, currentPreview.transform.localRotation.z);
- else if (WheelAxis < 0)
- currentPreview.transform.GetChild(0).eulerAngles += new Vector3(currentPreview.transform.localRotation.x, -currentPreview.smoothRotation, currentPreview.transform.localRotation.z);
- }
- if (allowPlacement)
- {
- renderers = currentPreview.GetComponentsInChildren<Renderer>();
- foreach (var rend in renderers)
- {
- rend.sharedMaterial = previewMaterial;
- }
- if (Input.GetMouseButtonDown(0))
- {
- PlacePrefab(currentPreview, currentPreview.transform.position, currentPreview.transform.eulerAngles);
- }
- }
- else
- {
- renderers = currentPreview.GetComponentsInChildren<Renderer>();
- foreach (var rend in renderers)
- {
- rend.sharedMaterial = previewDeniedMaterial;
- }
- }
- }
- }
- public bool PreviewExists()
- {
- return currentPreview;
- }
- public void PlacePrefab(Demo_ObjectData part, Vector3 position, Vector3 rotation)
- {
- collisionBox.transform.eulerAngles = Vector3.zero;
- collisionBox.transform.parent = null;
- GameObject PlacedTemp = Instantiate(part.gameObject, position, Quaternion.Euler(rotation));
- renderers = PlacedTemp.GetComponentsInChildren<Renderer>();
- foreach (var rend in renderers)
- {
- rend.sharedMaterial = PlacedTemp.GetComponent<Demo_ObjectData>().initialMaterial;
- }
- PlacedTemp.transform.parent = GameMaster.Instance.BuildingManager.currentBuilding.transform;
- GameMaster.Instance.placedParts.Add(PlacedTemp);
- selectedPrefab = null;
- Destroy(currentPreview.gameObject);
- currentPreview = null;
- }
- IEnumerator RotateObject(float angle)
- {
- Quaternion initialRot = currentPreview.transform.GetChild(0).transform.rotation;
- Quaternion endRot = Quaternion.Euler(initialRot.x, initialRot.y + angle, initialRot.z);
- float elapsed = 0.0f;
- while (elapsed < 1f)
- {
- currentPreview.transform.GetChild(0).transform.rotation = Quaternion.Slerp(initialRot, endRot, elapsed / 1f);
- elapsed += Time.deltaTime;
- yield return null;
- }
- currentPreview.transform.GetChild(0).transform.rotation = endRot;
- }
- public float Rotation(float rotationAngle)
- {
- float angle = Mathf.Abs(rotationAngle);
- if (angle == 180)
- angle = 270 - angle;
- if (angle < 90)
- angle = 90 - angle;
- if (angle > 89 && angle < 180)
- angle = 180 - angle;
- if (angle > 179 && angle < 270)
- angle = 270 - angle;
- if (angle > 269)
- angle = 360 - angle;
- return angle;
- }
- public float NegativeRotation(float rotationAngle)
- {
- if (rotationAngle == 180)
- rotationAngle = 90f;
- if (rotationAngle > 89 && rotationAngle < 180)
- rotationAngle = rotationAngle - 90f;
- if (rotationAngle > 179 && rotationAngle < 270)
- rotationAngle = rotationAngle - 180f;
- if (rotationAngle > 269)
- rotationAngle = rotationAngle - 270;
- return rotationAngle;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment