BuildingGhost.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.Core;
  5. using Shapes;
  6. namespace KairoEngine.Grids
  7. {
  8. public class BuildingGhost : MonoBehaviour {
  9. private Transform visual;
  10. private PlacedObjectType placedObjectTypeSO;
  11. private void Start() {
  12. RefreshVisual();
  13. GenericEvents.StartListening("OnSelectedChanged", OnSelectedChanged);
  14. }
  15. private void OnDisable()
  16. {
  17. GenericEvents.StopListening("OnSelectedChanged", OnSelectedChanged);
  18. }
  19. private void OnSelectedChanged() {
  20. RefreshVisual();
  21. }
  22. private void LateUpdate() {
  23. Vector3 targetPosition = GridBuildingSystem.Instance.GetMouseWorldSnappedPosition();
  24. //targetPosition.y = 1f;
  25. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * 15f);
  26. transform.rotation = Quaternion.Lerp(transform.rotation, GridBuildingSystem.Instance.GetPlacedObjectRotation(), Time.deltaTime * 15f);
  27. if(GridBuildingSystem.buildingToolAvailable) visual.gameObject.SetActive(true);
  28. else visual.gameObject.SetActive(false);
  29. }
  30. private void RefreshVisual() {
  31. if (visual != null) {
  32. Destroy(visual.gameObject);
  33. visual = null;
  34. }
  35. PlacedObjectType placedObjectTypeSO = GridBuildingSystem.Instance.GetPlacedObjectTypeSO();
  36. if (placedObjectTypeSO != null) {
  37. visual = Instantiate(placedObjectTypeSO.visual, Vector3.zero, Quaternion.identity);
  38. visual.parent = transform;
  39. visual.localPosition = Vector3.zero;
  40. visual.localEulerAngles = Vector3.zero;
  41. SetLayerRecursive(visual.gameObject, 11);
  42. }
  43. }
  44. private void SetLayerRecursive(GameObject targetGameObject, int layer) {
  45. targetGameObject.layer = layer;
  46. foreach (Transform child in targetGameObject.transform) {
  47. SetLayerRecursive(child.gameObject, layer);
  48. }
  49. }
  50. }
  51. }