Files
SAMKWANG/Assets/Scripts/Branch/Machine.cs
2025-12-08 09:34:41 +09:00

110 lines
3.2 KiB
C#

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Samkwang
{
public class Machine : MonoBehaviour
{
private Dictionary<Component, MeshRenderer[]> meshGroupCache = new();
private List<ModelItem> itemModels = new();
public ModelItem currentItemModel;
public Animator animator;
public float animationSpeed;
public UI_MachineStatusIcon machineStatusIcon;
public string machineName;
public WarningLight warningLight;
private bool isAlarmActive;
public string[] typeOptions;
public Sprite previewImage;
public Vector3 centerPos;
public float focusDistance;
public float focusAzimuth;
public float focusElevation;
public void Awake()
{
var modelParent = transform.GetComponentInChildren<ModelItemParent>(true);
itemModels = modelParent.transform.GetComponentsInChildren<ModelItem>(true).ToList();
animator = transform.GetComponent<Animator>();
animationSpeed = Random.Range(0.7f, 2f);
animator.speed = 0f;
warningLight = transform.GetComponentInChildren<WarningLight>();
GetComponentInParent<Floor>().machines.Add(this);
centerPos = GetMeshGroupCenter(this);
}
public Vector3 GetMeshGroupCenter(Component root)
{
if (!meshGroupCache.TryGetValue(root, out _))
{
meshGroupCache.Add(root, root.GetComponentsInChildren<MeshRenderer>());
}
var meshs = meshGroupCache[root];
Vector3 total = Vector3.zero;
foreach (var m in meshs)
{
total += m.bounds.center;
}
return total / meshs.Length;
}
public void SetAnimation(string status)
{
switch (status)
{
case "°¡µ¿Áß":
animator.speed = animationSpeed;
break;
default:
animator.speed = 0f;
break;
}
}
public void SetItemModel(string itemName)
{
var itemModel = itemModels.Find(x => x.name == itemName);
var currentModel = itemModel != null ? itemModel : itemModels[0];
if (currentModel != currentItemModel && currentItemModel != null)
{
currentItemModel.gameObject.SetActive(false);
}
currentItemModel = currentModel;
currentItemModel.gameObject.SetActive(true);
}
public void SetAlarm(string state)
{
isAlarmActive = state == "SET" ? true : false;
}
public void SetWaringLight(string status)
{
if (isAlarmActive)
{
warningLight.SetYellowLightActive();
}
else
{
if (status == "°¡µ¿Áß")
{
warningLight.SetGreenLightActvie();
}
else
{
warningLight.SetRedLightActvie();
}
}
}
}
}