99 lines
2.5 KiB
C#
99 lines
2.5 KiB
C#
using System.Collections;
|
|
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 Material[] materials;
|
|
private float effectDuration = .4f;
|
|
|
|
void OnEnable()
|
|
{
|
|
if(materials == null)
|
|
materials = GetComponent<MeshRenderer>().materials;
|
|
|
|
if(!useEffect)
|
|
{
|
|
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);
|
|
|
|
transform.DOLocalMoveZ(-16, speed)
|
|
.SetEase(Ease.OutSine);
|
|
}
|
|
else SetEffect(true);
|
|
}
|
|
|
|
public void Hide()
|
|
{
|
|
if(!useEffect)
|
|
{
|
|
var endPosition = locomotionHandler.pullUpPositionAfter;
|
|
var speed = locomotionHandler.trainSpeed;
|
|
|
|
transform.DOLocalMoveZ(endPosition, speed)
|
|
.SetEase(Ease.InSine)
|
|
.OnComplete(() => gameObject.SetActive(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 : 2;
|
|
float currentValue = startPosition;
|
|
float endPosition = 0;
|
|
|
|
if(isShow) endPosition = 2;
|
|
else endPosition = -5;
|
|
|
|
float progress = 0;
|
|
float duration = effectDuration;
|
|
|
|
while(progress < 1)
|
|
{
|
|
currentValue = Mathf.Lerp(startPosition, endPosition, progress);
|
|
|
|
for(int i = 0; i < materials.Length; i++)
|
|
{
|
|
materials[i]
|
|
.SetFloat("_EffectPosition", currentValue);
|
|
}
|
|
|
|
progress += duration * Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
for(int i = 0; i < materials.Length; i++)
|
|
{
|
|
materials[i]
|
|
.SetFloat("_EffectPosition", endPosition);
|
|
}
|
|
|
|
if(!isShow)
|
|
gameObject.SetActive(false);
|
|
}
|
|
} |