133 lines
3.9 KiB
C#
133 lines
3.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using AibisDream.FixSystem;
|
|
using AibisDream.Framework;
|
|
using AibisDream.Utility;
|
|
using Cinemachine;
|
|
using DG.Tweening;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class EngineSystem : MonoBehaviour
|
|
{
|
|
private SpriteRenderer _cover;
|
|
|
|
[Header("系统配置")] public EngineConfig engineConfig;
|
|
public bool isAvailable;
|
|
public bool isPullActive;
|
|
private bool _isSuccess;
|
|
|
|
public float curSpeed;
|
|
public float targetSpeed = 0.95f;
|
|
[SerializeField] private float coverMoveDuration = 5f;
|
|
[SerializeField] private float coverMoveDistance = 2.8f;
|
|
[HideInInspector] public UnityEvent<float, float> updateSpeedEvent = new();
|
|
|
|
private void Awake()
|
|
{
|
|
FixSystemCenter.SystemDic.Register(this);
|
|
_cover = transform.Find("Cover").GetComponent<SpriteRenderer>();
|
|
|
|
// 注册相机
|
|
var virtualCam = transform.Find("Engine Camera").GetComponentInChildren<ICinemachineCamera>();
|
|
CameraKit.Instance.RegisterCamera(CameraEnum.Engine, virtualCam);
|
|
}
|
|
|
|
public IEnumerator StepOnGas(float target, float duration)
|
|
{
|
|
// 逐渐加速
|
|
yield return DOTween.To(() => curSpeed, x => { curSpeed = x; }, target, duration).WaitForCompletion();
|
|
}
|
|
|
|
public void AddAirPress(float speedDelta)
|
|
{
|
|
curSpeed = CommonUtil.Limit(curSpeed + speedDelta / engineConfig.pullCount, 1, 0);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
CameraKit.Instance.UnRegisterCamera(CameraEnum.Engine);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
updateSpeedEvent.Invoke(curSpeed, GetSpeedScale());
|
|
if (curSpeed >= targetSpeed && !_isSuccess)
|
|
{
|
|
_isSuccess = true;
|
|
isPullActive = false;
|
|
DialogController.Instance?.StartDialogNode("EngineSuccess");
|
|
}
|
|
}
|
|
|
|
private float GetSpeedScale()
|
|
{
|
|
return 1 + curSpeed * engineConfig.speedRate;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 开启系统交互
|
|
/// </summary>
|
|
private void EnableSystem()
|
|
{
|
|
isAvailable = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭系统交互
|
|
/// </summary>
|
|
private void DisableSystem()
|
|
{
|
|
isAvailable = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打开盖板
|
|
/// 关闭系统交互
|
|
/// </summary>
|
|
/// <returns>动画序列</returns>
|
|
public Tween OpenCover()
|
|
{
|
|
var sq = DOTween.Sequence();
|
|
// 盖板上移
|
|
var moveDis = new Vector3(0, coverMoveDistance, 0);
|
|
sq.Join(_cover.transform.DOBlendableMoveBy(moveDis, coverMoveDuration));
|
|
// 盖板淡出
|
|
sq.Join(_cover.DOFade(0, coverMoveDuration));
|
|
|
|
// 为引擎系统解锁
|
|
sq.OnComplete(EnableSystem);
|
|
|
|
return sq;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭盖板
|
|
/// </summary>
|
|
/// <returns>动画序列</returns>
|
|
public Tween CloseCover()
|
|
{
|
|
var sq = DOTween.Sequence();
|
|
sq.Join(_cover.DOFade(1, coverMoveDuration));
|
|
|
|
var moveDis = new Vector3(0, -coverMoveDistance, 0);
|
|
sq.Join(_cover.transform.DOBlendableMoveBy(moveDis, coverMoveDuration));
|
|
|
|
// 锁定引擎系统
|
|
sq.OnComplete(DisableSystem);
|
|
|
|
return sq;
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public struct EngineConfig
|
|
{
|
|
[Header("齿轮基础速度")] public float gearSpeed;
|
|
[Header("齿轮速度倍率")] public float speedRate;
|
|
[Header("拉满几次到全速")] public float pullCount;
|
|
[Header("拉绳回缩速度")] public float returnSpeed;
|
|
}
|
|
} |