Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class Decorating : MonoBehaviour
- {
- public CoursePieceAsset[] coursePieces;
- public DecorationAsset[] decorations;
- private DecorationAsset selectedDecoration;
- private CoursePieceAsset selectedCoursePiece;
- private NotificationManager notifications;
- [SerializeField] private GameObject holeObj;
- [SerializeField] private GameObject buildingPosObj;
- [SerializeField] private GameObject updateGraphObject;
- public GameObject pendingObject;
- private GameObject decorationsParent;
- [HideInInspector] public GameObject selectedHole;
- [SerializeField] private Toggle gridToggle;
- [SerializeField] private Toggle rotateToggle;
- [SerializeField] float rotateAmount;
- public float gridSize = 1.5f;
- RaycastHit hit;
- Quaternion faceRotation;
- [SerializeField] private LayerMask layerMask;
- [HideInInspector] public bool gridOn = true;
- [HideInInspector] public bool isRotating = false;
- [HideInInspector] public bool smoothRotateOn = false;
- bool coursePieceIsSelected = false;
- [SerializeField] private Material[] materials; //0 = master, 1 = valid, 2 = invalid;
- public Vector3 n;
- private void Update()
- {
- if (pendingObject != null)
- {
- Building();
- }
- }
- private void Start()
- {
- decorationsParent = GameObject.Find("Decorations");
- notifications = GameObject.Find("Notifications").GetComponent<NotificationManager>();
- }
- void Building()
- {
- //Snapping position to grid
- if (gridOn)
- {
- buildingPosObj.transform.position = new Vector3(
- RoundToNearestGrid(buildingPosObj.transform.position.x),
- buildingPosObj.transform.position.y,
- RoundToNearestGrid(buildingPosObj.transform.position.z));
- }
- //Setting obj pos
- SetObjectPosition();
- //Building Actions
- if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Backspace)) //Canceling placement
- {
- DestroyObject();
- }
- if (NonUIInput.GetKey(KeyCode.R) && !rotateToggle.isOn) //Rotate Object
- {
- SmoothRotateObject();
- }
- else if(NonUIInput.GetKeyDown(KeyCode.R) && rotateToggle.isOn) {
- SharpRotateObject();
- }
- if (NonUIInput.GetKeyUp(KeyCode.R)) { isRotating = false; }
- if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Return)) //Placing Object
- {
- PlaceObject();
- }
- }
- void FixedUpdate()
- {
- //setting the buildingPosObj position
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- if (Physics.Raycast(ray, out hit, 1000, layerMask))
- {
- buildingPosObj.transform.position = hit.point;
- }
- }
- void SetObjectPosition()
- {
- if (!isRotating)
- {
- //Movement
- pendingObject.transform.position = buildingPosObj.transform.position;
- //Rotation
- Vector3 up = hit.normal;
- Vector3 forward = Vector3.Cross(Vector3.up, up);
- 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);
- pendingObject.transform.eulerAngles = finalRot;
- }
- }
- void PlaceObject()
- {
- pendingObject.tag = "Decoration";
- pendingObject.GetComponent<MeshRenderer>().material = materials[0];
- pendingObject.GetComponent<Collider>().enabled = true;
- //UpdatingGraph
- GameObject graphUpdate = Instantiate(updateGraphObject, pendingObject.transform.position, transform.rotation);
- graphUpdate.GetComponent<UpdateGraph>().UpdateSurroundingGraph();
- Destroy(graphUpdate);
- pendingObject = null;
- if (Input.GetKey(KeyCode.LeftShift))
- {
- if (coursePieceIsSelected) { SelectCoursePiece(selectedCoursePiece); }
- else { SelectDecoration(selectedDecoration); }
- }
- }
- void SmoothRotateObject()
- {
- isRotating = true;
- Vector3 targetPos = new Vector3(buildingPosObj.transform.position.x, pendingObject.transform.position.y, buildingPosObj.transform.position.z);
- pendingObject.transform.LookAt(targetPos);
- }
- void SharpRotateObject()
- {
- Vector3 rotation = pendingObject.transform.eulerAngles;
- rotation.y += rotateAmount;
- pendingObject.transform.eulerAngles = new Vector3(pendingObject.transform.eulerAngles.x, rotation.y, pendingObject.transform.eulerAngles.z);
- }
- void DestroyObject()
- {
- Destroy(pendingObject);
- }
- public void SelectDecoration(DecorationAsset decorationToSelect)
- {
- if (pendingObject != null) { Destroy(pendingObject); }
- selectedDecoration = decorationToSelect;
- pendingObject = Instantiate(selectedDecoration.prefab, buildingPosObj.transform.position, selectedDecoration.prefab.transform.rotation, decorationsParent.transform);
- pendingObject.name = selectedDecoration.objectName;
- coursePieceIsSelected = false;
- pendingObject.GetComponent<Collider>().enabled = false;
- isRotating = false;
- }
- public void SelectCoursePiece(CoursePieceAsset pieceToSelect)
- {
- isRotating = false;
- if (selectedHole != null)
- {
- if (pendingObject != null) { Destroy(pendingObject); }
- selectedCoursePiece = pieceToSelect;
- pendingObject = Instantiate(selectedCoursePiece.variants[0], buildingPosObj.transform.position, selectedCoursePiece.variants[0].transform.rotation, selectedHole.transform.parent.parent);
- pendingObject.name = selectedCoursePiece.objectName;
- pendingObject.GetComponent<Collider>().enabled = false;
- coursePieceIsSelected = true;
- }
- else { notifications.Notification("Please select a hole first!"); }
- }
- public void PlaceHole()
- {
- if (selectedHole != null)
- {
- if (pendingObject != null) { Destroy(pendingObject); };
- pendingObject = Instantiate(holeObj, buildingPosObj.transform.position, holeObj.transform.rotation, selectedHole.transform.parent.parent);
- pendingObject.name = "Hole";
- }
- else { notifications.Notification("Please select a hole first!"); }
- }
- public void ToggleGrid()
- {
- if (gridToggle.isOn) {
- gridOn = true;
- }
- else { gridOn = false; }
- }
- float RoundToNearestGrid(float pos)
- {
- //Changeable grid system
- float xDiff = pos % gridSize;
- pos -= xDiff;
- if (xDiff > (gridSize / 2))
- {
- pos += gridSize;
- }
- return pos;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement