Files
Studio/Assets/Scripts/Studio/TwinObject/StaticObject/StackerCrane.cs
2025-05-27 15:24:25 +09:00

187 lines
4.7 KiB
C#

using Studio.AssetTool;
using Studio.VirtualFactory.Info;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.Mathematics;
using UnityEngine;
namespace Studio.Staic.STKC
{
public class StackerCrane : AbstractFunctionObject
{
private Dictionary<string, string> CurrentEntity;
public override Dictionary<string, string> Info
{
get
{
return CurrentEntity;
}
}
public override Dictionary<string, string> Label
{
get
{
return CurrentEntity;
}
}
public override ObjectType ObjectType
{
get
{
return ObjectType.Dynamic;
}
}
public string BANK;
public string BAY;
public string LEVEL;
private float liftSpeed;
private float bodySpeed;
private float bodyProcess;
private float liftProcess;
private float liftStartY;
private float liftEndY;
public Transform lift;
public Vector3 bodyStart;
public Vector3 bodyEnd;
private Action onMotionStart;
private int[] values = new int[3];
private string curType;
public override void AfterAwake()
{
bodySpeed = 0.3f;
liftSpeed = 0.3f;
onMotionStart = StartAnim;
//이벤트 걸어둔다..
//+= OnPlayStart
}
public void OnPlayStart()
{
var type = GetComponent<CustomAssetRenderObject>().topic;
curType = type;
StudioService.instance.AddTypeIdListener(type, transform.name, OnUpdateData);
}
public void AddInit(string type)
{
StudioService.instance.AddTypeIdListener(type, transform.name, OnAddOtherUpdateData);
}
private void OnAddOtherUpdateData(object sender, StudioServiceIdEventArgs e)
{
}
public override void OnUpdateData(object sender, StudioServiceIdEventArgs e)
{
CurrentEntity = e.Entity;
BANK = CurrentEntity.FirstOrDefault(x => x.Key.Equals(nameof(BANK), StringComparison.OrdinalIgnoreCase)).Value;
BAY = CurrentEntity.FirstOrDefault(x => x.Key.Equals(nameof(BAY), StringComparison.OrdinalIgnoreCase)).Value;
LEVEL = CurrentEntity.FirstOrDefault(x => x.Key.Equals(nameof(LEVEL), StringComparison.OrdinalIgnoreCase)).Value;
var x = int.Parse(BANK);
var y = int.Parse(LEVEL);
var z = int.Parse(BAY);
if (IsSameValue(x, y, z))
return;
bodyProcess = 0f;
liftProcess = 0f;
bodyStart = transform.localPosition;
bodyEnd = bodyStart;
bodyEnd.z = z;
liftStartY = lift.position.y;
liftEndY = y;
onMotionStart?.Invoke();
}
public override void ChangeAutoID(bool isAuto)
{
return;
}
private void StartAnim()
{
StopAllCoroutines();
StartCoroutine(MotionStart());
}
IEnumerator MotionStart()
{
while (liftProcess < 1f)
{
Sliding();
Lifiting();
yield return null;
}
}
void Sliding()
{
bodyProcess += Time.deltaTime * bodySpeed;
if (bodyProcess >= 1f)
bodyProcess = 1f;
var pos = math.lerp(bodyStart, bodyEnd, bodyProcess);
transform.localPosition = pos;
}
void Lifiting()
{
liftProcess += Time.deltaTime * liftSpeed;
if (liftProcess >= 1f)
{
liftProcess = 1f;
}
var liftY = math.lerp(liftStartY, liftEndY, liftProcess);
var liftPos = lift.position;
liftPos.y = liftY;
lift.position = liftPos;
}
private bool IsSameValue(int bank, int bay, int level)
{
var sameBank = values[0].Equals(bank);
var sameLevel = values[1].Equals(level);
var sameBay = values[2].Equals(bay);
if (sameBank && sameLevel && sameLevel)
return true;
values[0] = bank;
values[1] = level;
values[2] = bay;
return false;
}
private void OnDestroy()
{
if (curType == null)
return;
StudioService.instance.RemoveTypeIdListener(curType, transform.name);
onMotionStart -= StartAnim;
}
}
}