146 lines
3.8 KiB
C#
146 lines
3.8 KiB
C#
using System.Linq;
|
|
using System.Collections;
|
|
using System.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
|
|
using DG.Tweening;
|
|
using UnityEngine;
|
|
|
|
public class Train : MonoBehaviour
|
|
{
|
|
public TrainType type;
|
|
public bool useEffect;
|
|
private LocomotionHandler locomotionHandler;
|
|
private Coroutine c_effect;
|
|
private List<Material> materials = new();
|
|
private float effectDuration = .4f;
|
|
|
|
public Tween anim;
|
|
|
|
void OnEnable()
|
|
{
|
|
var uiMenu = FindObjectOfType<UIMainMenu>();
|
|
uiMenu.currentTrain = this;
|
|
uiMenu.AbleToUseTrainButtons(false);
|
|
|
|
if(materials.Count == 0)
|
|
{
|
|
materials = GetComponent<MeshRenderer>().materials.ToList();
|
|
var materialsInChildren = new List<Material>();
|
|
for(int i = 0; i < transform.childCount; i++)
|
|
{
|
|
var childMaterials = transform.GetChild(i)
|
|
.GetComponent<MeshRenderer>()
|
|
.materials;
|
|
|
|
foreach(var item in childMaterials)
|
|
{
|
|
materials.Add(item);
|
|
}
|
|
}
|
|
|
|
foreach(var mat in materialsInChildren)
|
|
materials.Add(mat);
|
|
}
|
|
|
|
foreach(var mat in materials)
|
|
mat.SetFloat("_EffectPosition", -5);
|
|
|
|
if(!useEffect)
|
|
{
|
|
SetEffect(true);
|
|
|
|
if(locomotionHandler == null)
|
|
locomotionHandler = FindObjectOfType<LocomotionHandler>();
|
|
|
|
var startPosition = locomotionHandler.pullUpPositionBefore;
|
|
var speed = locomotionHandler.trainSpeed;
|
|
|
|
transform.localPosition = new Vector3(transform.localPosition.x,
|
|
transform.localPosition.y,
|
|
startPosition);
|
|
|
|
anim = transform.DOLocalMoveZ(-16, speed)
|
|
.SetEase(Ease.OutSine)
|
|
.OnComplete(() =>
|
|
{
|
|
Debug.Log("tut now");
|
|
uiMenu.AbleToUseTrainButtons(true);
|
|
});
|
|
}
|
|
else
|
|
{
|
|
SetEffect(true);
|
|
}
|
|
}
|
|
|
|
public async void Hide()
|
|
{
|
|
if(!useEffect)
|
|
{
|
|
var endPosition = locomotionHandler.pullUpPositionAfter;
|
|
var speed = locomotionHandler.trainSpeed;
|
|
|
|
transform.DOLocalMoveZ(endPosition, speed)
|
|
.SetEase(Ease.InSine)
|
|
.OnComplete(() => gameObject.SetActive(false));
|
|
|
|
await Task.Delay(3000);
|
|
SetEffect(false);
|
|
}
|
|
else SetEffect(false);
|
|
}
|
|
|
|
void SetEffect(bool isShow)
|
|
{
|
|
if(c_effect != null)
|
|
{
|
|
StopCoroutine(c_effect);
|
|
c_effect = null;
|
|
}
|
|
|
|
c_effect = StartCoroutine(SetEffectRoutine(isShow));
|
|
}
|
|
|
|
IEnumerator SetEffectRoutine(bool isShow)
|
|
{
|
|
float startPosition = isShow ? -5 : 4;
|
|
float currentValue = startPosition;
|
|
float endPosition = 0;
|
|
|
|
if(isShow) endPosition = 4;
|
|
else endPosition = -5;
|
|
|
|
float progress = 0;
|
|
float duration = effectDuration;
|
|
|
|
while(progress < 1)
|
|
{
|
|
currentValue = Mathf.Lerp(startPosition, endPosition, progress);
|
|
|
|
for(int i = 0; i < materials.Count; i++)
|
|
{
|
|
materials[i]
|
|
.SetFloat("_EffectPosition", currentValue);
|
|
}
|
|
|
|
progress += duration * Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
for(int i = 0; i < materials.Count; i++)
|
|
{
|
|
materials[i]
|
|
.SetFloat("_EffectPosition", endPosition);
|
|
}
|
|
|
|
if(isShow && useEffect)
|
|
{
|
|
FindObjectOfType<UIMainMenu>()
|
|
.AbleToUseTrainButtons(true);
|
|
}
|
|
|
|
if(!isShow && useEffect)
|
|
gameObject.SetActive(false);
|
|
}
|
|
} |