using System.Collections; using System.Collections.Generic; using UnityEngine; using KairoEngine.Core; using Shapes; namespace KairoEngine.Grids { public class BuildingGhost : MonoBehaviour { private Transform visual; private PlacedObjectType placedObjectTypeSO; private void Start() { RefreshVisual(); GenericEvents.StartListening("OnSelectedChanged", OnSelectedChanged); } private void OnDisable() { GenericEvents.StopListening("OnSelectedChanged", OnSelectedChanged); } private void OnSelectedChanged() { RefreshVisual(); } private void LateUpdate() { Vector3 targetPosition = GridBuildingSystem.Instance.GetMouseWorldSnappedPosition(); //targetPosition.y = 1f; transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * 15f); transform.rotation = Quaternion.Lerp(transform.rotation, GridBuildingSystem.Instance.GetPlacedObjectRotation(), Time.deltaTime * 15f); if(GridBuildingSystem.buildingToolAvailable) visual.gameObject.SetActive(true); else visual.gameObject.SetActive(false); } private void RefreshVisual() { if (visual != null) { Destroy(visual.gameObject); visual = null; } PlacedObjectType placedObjectTypeSO = GridBuildingSystem.Instance.GetPlacedObjectTypeSO(); if (placedObjectTypeSO != null) { visual = Instantiate(placedObjectTypeSO.visual, Vector3.zero, Quaternion.identity); visual.parent = transform; visual.localPosition = Vector3.zero; visual.localEulerAngles = Vector3.zero; SetLayerRecursive(visual.gameObject, 11); } } private void SetLayerRecursive(GameObject targetGameObject, int layer) { targetGameObject.layer = layer; foreach (Transform child in targetGameObject.transform) { SetLayerRecursive(child.gameObject, layer); } } } }