KitchenChaos/Assets/Scripts/Counters/PlatesCounterVisual.cs

38 lines
1.1 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
public class PlatesCounterVisual : MonoBehaviour
{
[SerializeField] private PlatesCounter platesCounter;
[SerializeField] private Transform counterTopPoint;
[SerializeField] private Transform plateVisualPrefab;
private List<GameObject> plateVisualGameObjectList;
private void Awake()
{
plateVisualGameObjectList = new();
}
private void Start()
{
platesCounter.OnPlateSpawned += PlatesCounter_OnPlateSpawned;
platesCounter.OnPlateRemoved += PlatesCounter_OnPlateRemoved;
}
private void PlatesCounter_OnPlateRemoved(object sender, System.EventArgs e)
{
GameObject plateGameObject = plateVisualGameObjectList[^1];
_ = plateVisualGameObjectList.Remove(plateGameObject);
Destroy(plateGameObject);
}
private void PlatesCounter_OnPlateSpawned(object sender, System.EventArgs e)
{
Transform plateVisualTransform = Instantiate(plateVisualPrefab, counterTopPoint);
const float plateOffsetY = .1f;
plateVisualTransform.localPosition = new(0, plateOffsetY * plateVisualGameObjectList.Count, 0);
plateVisualGameObjectList.Add(plateVisualTransform.gameObject);
}
}