3DTD/Assets/Scripts/PlacementSystem/PlacementManager.cs

115 lines
3.5 KiB
C#
Raw Normal View History

2024-04-20 01:51:12 +02:00
using System;
using System.Collections;
using System.Collections.Generic;
2024-04-20 17:13:17 +02:00
using System.Linq;
2024-04-20 19:22:54 +02:00
using Unity.Mathematics;
2024-04-20 01:51:12 +02:00
using Unity.VisualScripting;
using UnityEngine;
public class TowerPlacementManager : MonoBehaviour
{
/// <summary>
/// Sender
/// </summary>
public static event Action<TowerPlacementManager> OnSpawnGridRequested;
public static TowerPlacementManager Singleton;
2024-04-20 19:22:54 +02:00
// Section: Mouse
private SlotManager CurrentSelectedSlot;
private SlotManager CurrentHovered;
private GameObject SilhouettedObject;
2024-04-20 15:38:21 +02:00
2024-04-20 16:27:58 +02:00
// Section: Debug
public GameObject DebugTowerPrefab;
2024-04-20 17:13:17 +02:00
private bool IsPlacing = true; // Show silluette or nothing
2024-04-20 01:51:12 +02:00
private void Start()
{
if (Singleton != this)
Destroy(Singleton);
Singleton = this;
OnSpawnGridRequested?.Invoke(this);
}
public void OnSlotClicked(SlotManager slot, GridManager grid, int x, int y)
{
2024-04-20 15:38:21 +02:00
slot.gameObject.GetComponentInChildren<Renderer>().material.color = Color.blue;
OnSelectSlot(slot);
2024-04-20 16:27:58 +02:00
SpawnTowerAtSelected(DebugTowerPrefab);
2024-04-20 15:38:21 +02:00
}
// Called when a slot is selected
public void OnSelectSlot(SlotManager slot)
{
// Detects if the selected slot is new
2024-04-20 19:22:54 +02:00
if (CurrentSelectedSlot != null && CurrentSelectedSlot != slot)
OnDeselectSlot(CurrentSelectedSlot, slot);
CurrentSelectedSlot = slot;
2024-04-20 15:38:21 +02:00
slot.gameObject.GetComponentInChildren<Renderer>().material.color = Color.blue;
}
2024-04-20 01:51:12 +02:00
2024-04-20 15:38:21 +02:00
// Called when another slot is getting selected
public void OnDeselectSlot(SlotManager prevSlot, SlotManager newSlot)
{
prevSlot.gameObject.GetComponentInChildren<Renderer>().material.color = Color.white;
2024-04-20 01:51:12 +02:00
}
2024-04-20 16:27:58 +02:00
public void SpawnTowerAtSelected(GameObject towerPrefab)
{
2024-04-20 19:22:54 +02:00
Instantiate(towerPrefab, CurrentSelectedSlot.transform);
}
public void OnSlotHovered(SlotManager slot, GridManager grid, int x, int y)
{
if (SilhouettedObject == null)
SilhouettedObject = ToSilhouette(DebugTowerPrefab);
//SilhouettedObject.SetActive(true);
SilhouettedObject.transform.parent = slot.TowerSpawnPoint.transform;
SilhouettedObject.transform.localPosition = Vector3.zero;
SilhouettedObject.transform.localRotation = quaternion.identity;
2024-04-20 16:27:58 +02:00
}
2024-04-20 17:13:17 +02:00
2024-04-20 19:22:54 +02:00
public void OnSlotUnHovered(SlotManager slot, GridManager grid, int x, int y)
2024-04-20 17:13:17 +02:00
{
2024-04-20 19:22:54 +02:00
//SilhouettedObject.SetActive(false);
SilhouettedObject.transform.parent = null;
SilhouettedObject.transform.position = Vector3.zero;
2024-04-20 17:13:17 +02:00
}
2024-04-20 18:04:55 +02:00
2024-04-20 17:13:17 +02:00
[Space(10)]
public Material SilhouetteMaterial;
public GameObject ToSilhouette(GameObject obj)
{
// if prefab, then spawn it
if (obj.scene.name == null)
obj = Instantiate(obj);
Component[] objScripts = obj.GetComponents<Component>();
objScripts = objScripts.Concat(obj.GetComponentsInChildren<Component>()).ToArray();
for (int i = 0; i < objScripts.Length; i++)
{
var comp = objScripts[i];
if (comp is not Renderer && comp is not Transform && comp is not MeshFilter)
{
Destroy(comp);
}
else if (typeof(Renderer).IsAssignableFrom(comp))
{
var renderer = comp as Renderer;
2024-04-20 17:26:20 +02:00
renderer.materials = new Material[] { SilhouetteMaterial , SilhouetteMaterial , SilhouetteMaterial , SilhouetteMaterial , SilhouetteMaterial };
2024-04-20 17:13:17 +02:00
}
}
return obj;
}
2024-04-20 01:51:12 +02:00
}