106 lines
2.5 KiB
C#
106 lines
2.5 KiB
C#
using System.Linq;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
using DG.Tweening;
|
|
using UnityEngine;
|
|
|
|
public enum TrainType
|
|
{
|
|
EP2D,
|
|
Ivolga2,
|
|
Ivolga3
|
|
}
|
|
|
|
public class LocomotionHandler : MonoBehaviour
|
|
{
|
|
[SerializeField] private Train[] trains;
|
|
[SerializeField] private Material[] materials;
|
|
[SerializeField] private float effectDuration;
|
|
|
|
public float trainSpeed;
|
|
public float pullUpPositionBefore;
|
|
public float pullUpPositionAfter;
|
|
|
|
public readonly float pullUpPosition = -12;
|
|
private Coroutine c_effect;
|
|
|
|
void Update()
|
|
{
|
|
if(Input.GetKeyDown(KeyCode.Alpha1))
|
|
ShowTrain(TrainType.EP2D);
|
|
|
|
if(Input.GetKeyDown(KeyCode.Alpha2))
|
|
ShowTrain(TrainType.Ivolga2);
|
|
|
|
if(Input.GetKeyDown(KeyCode.Alpha3))
|
|
ShowTrain(TrainType.Ivolga3);
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
foreach(var train in trains)
|
|
{
|
|
train.gameObject.SetActive(false);
|
|
}
|
|
|
|
foreach(var material in materials)
|
|
{
|
|
material.SetFloat("_EffectPosition", 5);
|
|
}
|
|
}
|
|
|
|
public void ShowTrain(TrainType trainType)
|
|
{
|
|
var activeTrain = trains.FirstOrDefault(x => x.gameObject.activeSelf);
|
|
if(activeTrain != null)
|
|
activeTrain.Hide();
|
|
|
|
trains.First(x => x.type == trainType)
|
|
.gameObject.SetActive(true);
|
|
}
|
|
|
|
// 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 = materials[0].GetFloat("_EffectPosition");
|
|
float currentValue = startPosition;
|
|
float endPosition = 0;
|
|
|
|
if(isShow) endPosition = 5;
|
|
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);
|
|
}
|
|
}
|
|
} |